1
1
# -*- coding: utf-8 -*-
2
2
from __future__ import absolute_import , print_function , with_statement
3
+ from typing import Iterable
4
+ from typing import Union
3
5
4
6
import logging
5
7
import os
8
10
import sys
9
11
import tempfile
10
12
import textwrap
13
+ from turtle import TurtleScreenBase
11
14
12
15
from .handler import _check_log_handler
13
16
from .pandoc_download import DEFAULT_TARGET_FOLDER , download_pandoc
16
19
__author__ = u'Juho Vepsäläinen'
17
20
__version__ = '1.7.4'
18
21
__license__ = 'MIT'
19
- __all__ = ['convert' , ' convert_file' , 'convert_text' ,
22
+ __all__ = ['convert_file' , 'convert_text' ,
20
23
'get_pandoc_formats' , 'get_pandoc_version' , 'get_pandoc_path' ,
21
24
'download_pandoc' ]
22
25
23
26
# Set up the module level logger
24
27
logger = logging .getLogger (__name__ )
25
28
26
- def convert_text (source , to , format , extra_args = (), encoding = 'utf-8' ,
27
- outputfile = None , filters = None , verify_format = True ,
28
- sandbox = True , cworkdir = None ):
29
+ def convert_text (source : str , to : str , format : str , extra_args : Iterable = (), encoding : str = 'utf-8' ,
30
+ outputfile : Union [ None , str ] = None , filters : Union [ Iterable , None ] = None , verify_format : bool = True ,
31
+ sandbox : bool = True , cworkdir : Union [ str , None ] = None ) -> str :
29
32
"""Converts given `source` from `format` to `to`.
30
33
31
34
:param str source: Unicode string or bytes (see encoding)
@@ -65,9 +68,9 @@ def convert_text(source, to, format, extra_args=(), encoding='utf-8',
65
68
cworkdir = cworkdir )
66
69
67
70
68
- def convert_file (source_file , to , format = None , extra_args = (), encoding = 'utf-8' ,
69
- outputfile = None , filters = None , verify_format = True ,
70
- sandbox = True , cworkdir = None ):
71
+ def convert_file (source_file : str , to : str , format : Union [ str , None ] = None , extra_args : Iterable = (), encoding : str = 'utf-8' ,
72
+ outputfile : Union [ None , str ] = None , filters : Union [ Iterable , None ] = None , verify_format : bool = True ,
73
+ sandbox : bool = True , cworkdir : Union [ str , None ] = None ) -> str :
71
74
"""Converts given `source` from `format` to `to`.
72
75
73
76
:param str source_file: file path (see encoding)
@@ -111,11 +114,7 @@ def convert_file(source_file, to, format=None, extra_args=(), encoding='utf-8',
111
114
cworkdir = cworkdir )
112
115
113
116
114
- def _identify_path (source ):
115
- # guard against problems
116
- if source is None or not isinstance (source , string_types ):
117
- return False
118
-
117
+ def _identify_path (source :str ) -> bool :
119
118
is_path = False
120
119
try :
121
120
is_path = os .path .exists (source )
@@ -126,24 +125,27 @@ def _identify_path(source):
126
125
pass
127
126
128
127
if not is_path :
129
- # check if it's an URL
130
- result = urlparse (source )
131
- if result .scheme in ["http" , "https" ]:
132
- is_path = True
133
- elif result .scheme and result .netloc and result .path :
134
- # complete uri including one with a network path
135
- is_path = True
136
- elif result .scheme == "file" and result .path :
137
- is_path = os .path .exists (url2path (source ))
128
+ try :
129
+ # check if it's an URL
130
+ result = urlparse (source )
131
+ if result .scheme in ["http" , "https" ]:
132
+ is_path = True
133
+ elif result .scheme and result .netloc and result .path :
134
+ # complete uri including one with a network path
135
+ is_path = True
136
+ elif result .scheme == "file" and result .path :
137
+ is_path = os .path .exists (url2path (source ))
138
+ except AttributeError :
139
+ pass
138
140
139
141
return is_path
140
142
141
143
142
- def _identify_format_from_path (sourcefile , format ) :
144
+ def _identify_format_from_path (sourcefile : str , format : str ) -> str :
143
145
return format or os .path .splitext (sourcefile )[1 ].strip ('.' )
144
146
145
147
146
- def _as_unicode (source , encoding ) :
148
+ def _as_unicode (source : any , encoding : str ) -> any :
147
149
if encoding != 'utf-8' :
148
150
# if a source and a different encoding is given, try to decode the the source into a
149
151
# unicode string
@@ -154,7 +156,7 @@ def _as_unicode(source, encoding):
154
156
return source
155
157
156
158
157
- def _identify_input_type (source , format , encoding = 'utf-8' ):
159
+ def _identify_input_type (source : any , format : str , encoding : str = 'utf-8' ):
158
160
path = _identify_path (source )
159
161
if path :
160
162
format = _identify_format_from_path (source , format )
@@ -381,7 +383,7 @@ def _get_base_format(format):
381
383
return re .split (r'\+|-' , format )[0 ]
382
384
383
385
384
- def get_pandoc_formats ():
386
+ def get_pandoc_formats () -> Iterable :
385
387
'''
386
388
Dynamic preprocessor for Pandoc formats.
387
389
Return 2 lists. "from_formats" and "to_formats".
@@ -414,7 +416,7 @@ def get_pandoc_formats():
414
416
return [f .strip () for f in in_ ], [f .strip () for f in out ]
415
417
416
418
417
- def get_pandoc_formats_pre_1_18 ():
419
+ def get_pandoc_formats_pre_1_18 () -> Iterable :
418
420
'''
419
421
Dynamic preprocessor for Pandoc formats for version < 1.18.
420
422
Return 2 lists. "from_formats" and "to_formats".
@@ -443,7 +445,7 @@ def get_pandoc_formats_pre_1_18():
443
445
444
446
# copied and adapted from jupyter_nbconvert/utils/pandoc.py, Modified BSD License
445
447
446
- def _get_pandoc_version (pandoc_path ) :
448
+ def _get_pandoc_version (pandoc_path : str ) -> str :
447
449
new_env = os .environ .copy ()
448
450
creation_flag = 0x08000000 if sys .platform == "win32" else 0 # set creation flag to not open pandoc in new console on windows
449
451
if 'HOME' not in os .environ :
@@ -468,7 +470,7 @@ def _get_pandoc_version(pandoc_path):
468
470
return version
469
471
470
472
471
- def get_pandoc_version ():
473
+ def get_pandoc_version () -> str :
472
474
"""Gets the Pandoc version if Pandoc is installed.
473
475
474
476
It will probe Pandoc for its version, cache it and return that value. If a cached version is
@@ -486,7 +488,7 @@ def get_pandoc_version():
486
488
return __version
487
489
488
490
489
- def get_pandoc_path ():
491
+ def get_pandoc_path () -> str :
490
492
"""Gets the Pandoc path if Pandoc is installed.
491
493
492
494
It will return a path to pandoc which is used by pypandoc.
@@ -506,12 +508,12 @@ def get_pandoc_path():
506
508
_ensure_pandoc_path ()
507
509
return __pandoc_path
508
510
509
- def ensure_pandoc_minimal_version (major , minor = 0 ):
511
+ def ensure_pandoc_minimal_version (major : int , minor : int = 0 ) -> bool :
510
512
"""Check if the used pandoc fulfill a minimal version requirement.
511
513
512
- :param bool major: pandoc major version, such as 1 or 2.
514
+ :param int major: pandoc major version, such as 1 or 2.
513
515
514
- :param bool minor: pandoc minor version, such as 10 or 11.
516
+ :param int minor: pandoc minor version, such as 10 or 11.
515
517
516
518
:returns: True if the installed pandoc is above the minimal version, False otherwise.
517
519
:rtype: bool
@@ -523,12 +525,12 @@ def ensure_pandoc_minimal_version(major, minor=0):
523
525
524
526
525
527
526
- def ensure_pandoc_maximal_version (major , minor = 9999 ):
528
+ def ensure_pandoc_maximal_version (major : int , minor : int = 9999 ) -> bool :
527
529
"""Check if the used pandoc fulfill a maximal version requirement.
528
530
529
- :param bool major: pandoc major version, such as 1 or 2.
531
+ :param int major: pandoc major version, such as 1 or 2.
530
532
531
- :param bool minor: pandoc minor version, such as 10 or 11.
533
+ :param int minor: pandoc minor version, such as 10 or 11.
532
534
533
535
:returns: True if the installed pandoc is below the maximal version, False otherwise.
534
536
:rtype: bool
@@ -539,7 +541,7 @@ def ensure_pandoc_maximal_version(major, minor=9999):
539
541
return version [0 ] <= int (major ) and version [1 ] <= int (minor )
540
542
541
543
542
- def _ensure_pandoc_path ():
544
+ def _ensure_pandoc_path () -> None :
543
545
global __pandoc_path
544
546
545
547
_check_log_handler ()
@@ -636,10 +638,10 @@ def _ensure_pandoc_path():
636
638
"install pypandoc wheels with included pandoc." )
637
639
638
640
639
- def ensure_pandoc_installed (url = None ,
640
- targetfolder = None ,
641
- version = "latest" ,
642
- delete_installer = False ):
641
+ def ensure_pandoc_installed (url : Union [ str , None ] = None ,
642
+ targetfolder : Union [ str , None ] = None ,
643
+ version : str = "latest" ,
644
+ delete_installer : bool = False ) -> None :
643
645
"""Try to install pandoc if it isn't installed.
644
646
645
647
Parameters are passed to download_pandoc()
0 commit comments