Skip to content

Commit 2cfcc1e

Browse files
author
Nicklas Tegner
authored
Python Type Hints implementation (#258)
*Added typehints
1 parent 3e296b7 commit 2cfcc1e

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
lines changed

pypandoc/__init__.py

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, print_function, with_statement
3+
from typing import Iterable
4+
from typing import Union
35

46
import logging
57
import os
@@ -8,6 +10,7 @@
810
import sys
911
import tempfile
1012
import textwrap
13+
from turtle import TurtleScreenBase
1114

1215
from .handler import _check_log_handler
1316
from .pandoc_download import DEFAULT_TARGET_FOLDER, download_pandoc
@@ -16,16 +19,16 @@
1619
__author__ = u'Juho Vepsäläinen'
1720
__version__ = '1.7.4'
1821
__license__ = 'MIT'
19-
__all__ = ['convert', 'convert_file', 'convert_text',
22+
__all__ = ['convert_file', 'convert_text',
2023
'get_pandoc_formats', 'get_pandoc_version', 'get_pandoc_path',
2124
'download_pandoc']
2225

2326
# Set up the module level logger
2427
logger = logging.getLogger(__name__)
2528

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:
2932
"""Converts given `source` from `format` to `to`.
3033
3134
:param str source: Unicode string or bytes (see encoding)
@@ -65,9 +68,9 @@ def convert_text(source, to, format, extra_args=(), encoding='utf-8',
6568
cworkdir=cworkdir)
6669

6770

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:
7174
"""Converts given `source` from `format` to `to`.
7275
7376
: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',
111114
cworkdir=cworkdir)
112115

113116

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:
119118
is_path = False
120119
try:
121120
is_path = os.path.exists(source)
@@ -126,24 +125,27 @@ def _identify_path(source):
126125
pass
127126

128127
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
138140

139141
return is_path
140142

141143

142-
def _identify_format_from_path(sourcefile, format):
144+
def _identify_format_from_path(sourcefile:str, format:str) -> str:
143145
return format or os.path.splitext(sourcefile)[1].strip('.')
144146

145147

146-
def _as_unicode(source, encoding):
148+
def _as_unicode(source:any, encoding:str) -> any:
147149
if encoding != 'utf-8':
148150
# if a source and a different encoding is given, try to decode the the source into a
149151
# unicode string
@@ -154,7 +156,7 @@ def _as_unicode(source, encoding):
154156
return source
155157

156158

157-
def _identify_input_type(source, format, encoding='utf-8'):
159+
def _identify_input_type(source:any, format:str, encoding:str='utf-8'):
158160
path = _identify_path(source)
159161
if path:
160162
format = _identify_format_from_path(source, format)
@@ -381,7 +383,7 @@ def _get_base_format(format):
381383
return re.split(r'\+|-', format)[0]
382384

383385

384-
def get_pandoc_formats():
386+
def get_pandoc_formats() -> Iterable:
385387
'''
386388
Dynamic preprocessor for Pandoc formats.
387389
Return 2 lists. "from_formats" and "to_formats".
@@ -414,7 +416,7 @@ def get_pandoc_formats():
414416
return [f.strip() for f in in_], [f.strip() for f in out]
415417

416418

417-
def get_pandoc_formats_pre_1_18():
419+
def get_pandoc_formats_pre_1_18() -> Iterable:
418420
'''
419421
Dynamic preprocessor for Pandoc formats for version < 1.18.
420422
Return 2 lists. "from_formats" and "to_formats".
@@ -443,7 +445,7 @@ def get_pandoc_formats_pre_1_18():
443445

444446
# copied and adapted from jupyter_nbconvert/utils/pandoc.py, Modified BSD License
445447

446-
def _get_pandoc_version(pandoc_path):
448+
def _get_pandoc_version(pandoc_path:str) -> str:
447449
new_env = os.environ.copy()
448450
creation_flag = 0x08000000 if sys.platform == "win32" else 0 # set creation flag to not open pandoc in new console on windows
449451
if 'HOME' not in os.environ:
@@ -468,7 +470,7 @@ def _get_pandoc_version(pandoc_path):
468470
return version
469471

470472

471-
def get_pandoc_version():
473+
def get_pandoc_version() -> str:
472474
"""Gets the Pandoc version if Pandoc is installed.
473475
474476
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():
486488
return __version
487489

488490

489-
def get_pandoc_path():
491+
def get_pandoc_path() -> str:
490492
"""Gets the Pandoc path if Pandoc is installed.
491493
492494
It will return a path to pandoc which is used by pypandoc.
@@ -506,12 +508,12 @@ def get_pandoc_path():
506508
_ensure_pandoc_path()
507509
return __pandoc_path
508510

509-
def ensure_pandoc_minimal_version(major, minor=0):
511+
def ensure_pandoc_minimal_version(major:int, minor:int=0) -> bool:
510512
"""Check if the used pandoc fulfill a minimal version requirement.
511513
512-
:param bool major: pandoc major version, such as 1 or 2.
514+
:param int major: pandoc major version, such as 1 or 2.
513515
514-
:param bool minor: pandoc minor version, such as 10 or 11.
516+
:param int minor: pandoc minor version, such as 10 or 11.
515517
516518
:returns: True if the installed pandoc is above the minimal version, False otherwise.
517519
:rtype: bool
@@ -523,12 +525,12 @@ def ensure_pandoc_minimal_version(major, minor=0):
523525

524526

525527

526-
def ensure_pandoc_maximal_version(major, minor=9999):
528+
def ensure_pandoc_maximal_version(major:int, minor:int=9999) -> bool:
527529
"""Check if the used pandoc fulfill a maximal version requirement.
528530
529-
:param bool major: pandoc major version, such as 1 or 2.
531+
:param int major: pandoc major version, such as 1 or 2.
530532
531-
:param bool minor: pandoc minor version, such as 10 or 11.
533+
:param int minor: pandoc minor version, such as 10 or 11.
532534
533535
:returns: True if the installed pandoc is below the maximal version, False otherwise.
534536
:rtype: bool
@@ -539,7 +541,7 @@ def ensure_pandoc_maximal_version(major, minor=9999):
539541
return version[0] <= int(major) and version[1] <= int(minor)
540542

541543

542-
def _ensure_pandoc_path():
544+
def _ensure_pandoc_path() -> None:
543545
global __pandoc_path
544546

545547
_check_log_handler()
@@ -636,10 +638,10 @@ def _ensure_pandoc_path():
636638
"install pypandoc wheels with included pandoc.")
637639

638640

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:
643645
"""Try to install pandoc if it isn't installed.
644646
645647
Parameters are passed to download_pandoc()

pypandoc/pandoc_download.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import subprocess
1010
import sys
1111
import tempfile
12+
from typing import Union
1213

1314
try:
1415
from urllib.request import urlopen
@@ -189,11 +190,11 @@ def _handle_win32(filename, targetfolder):
189190
logger.info("Done.")
190191

191192

192-
def download_pandoc(url=None,
193-
targetfolder=None,
194-
version="latest",
195-
delete_installer=False,
196-
download_folder=None):
193+
def download_pandoc(url:Union[str, None]=None,
194+
targetfolder:Union[str, None]=None,
195+
version:str="latest",
196+
delete_installer:bool=False,
197+
download_folder:Union[str, None]=None) -> None:
197198
"""Download and unpack pandoc
198199
199200
Downloads prebuild binaries for pandoc from `url` and unpacks it into

0 commit comments

Comments
 (0)