77import shlex
88import subprocess
99import tempfile
10+ from typing import Any , Iterator , Iterable , List , Optional , Text , Tuple
1011
11- from fontTools .ttLib import TTFont
12+ from fontTools .ttLib import TTFont # type: ignore
1213
13- from fdiff .exceptions import AIOError
14- from fdiff .remote import (
14+ from .exceptions import AIOError
15+ from .remote import (
1516 _get_filepath_from_url ,
1617 create_async_get_request_session_and_run ,
1718)
1819
19- from fdiff .utils import get_file_modtime
20+ from .utils import get_file_modtime
2021
2122
2223#
2627#
2728
2829
29- def _async_fetch_files (dirpath , urls ) :
30+ def _async_fetch_files (dirpath : Text , urls : List [ Text ]) -> None :
3031 loop = asyncio .get_event_loop ()
3132 tasks = loop .run_until_complete (
3233 create_async_get_request_session_and_run (urls , dirpath )
@@ -45,8 +46,13 @@ def _async_fetch_files(dirpath, urls):
4546
4647
4748def _get_fonts_and_save_xml (
48- filepath_a , filepath_b , tmpdirpath , include_tables , exclude_tables , use_multiprocess
49- ):
49+ filepath_a : Text ,
50+ filepath_b : Text ,
51+ tmpdirpath : Text ,
52+ include_tables : Optional [List [Text ]],
53+ exclude_tables : Optional [List [Text ]],
54+ use_multiprocess : bool ,
55+ ) -> Tuple [Text , Text , Text , Text , Text , Text ]:
5056 post_pathname , postpath , pre_pathname , prepath = _get_pre_post_paths (
5157 filepath_a , filepath_b , tmpdirpath
5258 )
@@ -69,8 +75,12 @@ def _get_fonts_and_save_xml(
6975 return left_ttxpath , right_ttxpath , pre_pathname , prepath , post_pathname , postpath
7076
7177
72- def _get_pre_post_paths (filepath_a , filepath_b , dirpath ):
73- urls = []
78+ def _get_pre_post_paths (
79+ filepath_a : Text ,
80+ filepath_b : Text ,
81+ dirpath : Text ,
82+ ) -> Tuple [Text , Text , Text , Text ]:
83+ urls : List [Text ] = []
7484 if filepath_a .startswith ("http" ):
7585 urls .append (filepath_a )
7686 prepath = _get_filepath_from_url (filepath_a , dirpath )
@@ -94,14 +104,14 @@ def _get_pre_post_paths(filepath_a, filepath_b, dirpath):
94104
95105
96106def _mp_save_ttx_xml (
97- tt_left ,
98- tt_right ,
99- left_ttxpath ,
100- right_ttxpath ,
101- exclude_tables ,
102- include_tables ,
103- use_multiprocess ,
104- ):
107+ tt_left : Any ,
108+ tt_right : Any ,
109+ left_ttxpath : Text ,
110+ right_ttxpath : Text ,
111+ exclude_tables : Optional [ List [ Text ]] ,
112+ include_tables : Optional [ List [ Text ]] ,
113+ use_multiprocess : bool ,
114+ ) -> None :
105115 if use_multiprocess and cpu_count () > 1 :
106116 # Use parallel fontTools.ttLib.TTFont.saveXML dump
107117 # by default on multi CPU systems. This is a performance
@@ -121,13 +131,20 @@ def _mp_save_ttx_xml(
121131 _ttfont_save_xml (tt_right , right_ttxpath , include_tables , exclude_tables )
122132
123133
124- def _ttfont_save_xml (ttf , filepath , include_tables , exclude_tables ):
134+ def _ttfont_save_xml (
135+ ttf : Any ,
136+ filepath : Text ,
137+ include_tables : Optional [List [Text ]],
138+ exclude_tables : Optional [List [Text ]],
139+ ) -> bool :
125140 """Writes TTX specification formatted XML to disk on filepath."""
126141 ttf .saveXML (filepath , tables = include_tables , skipTables = exclude_tables )
127142 return True
128143
129144
130- def _validate_table_excludes (exclude_tables , tt_left , tt_right ):
145+ def _validate_table_excludes (
146+ exclude_tables : Optional [List [Text ]], tt_left : Any , tt_right : Any
147+ ) -> None :
131148 # Validation: exclude_tables request should be for tables that are in one of
132149 # the two fonts. Mis-specified OT table definitions could otherwise result
133150 # in the presence of a table in the diff when the request was to exclude it.
@@ -140,7 +157,9 @@ def _validate_table_excludes(exclude_tables, tt_left, tt_right):
140157 )
141158
142159
143- def _validate_table_includes (include_tables , tt_left , tt_right ):
160+ def _validate_table_includes (
161+ include_tables : Optional [List [Text ]], tt_left : Any , tt_right : Any
162+ ) -> None :
144163 # Validation: include_tables request should be for tables that are in one of
145164 # the two fonts. This otherwise silently passes with exit status code 0 which
146165 # could lead to the interpretation of no diff between two files when the table
@@ -164,13 +183,13 @@ def _validate_table_includes(include_tables, tt_left, tt_right):
164183
165184
166185def u_diff (
167- filepath_a ,
168- filepath_b ,
169- context_lines = 3 ,
170- include_tables = None ,
171- exclude_tables = None ,
172- use_multiprocess = True ,
173- ):
186+ filepath_a : Text ,
187+ filepath_b : Text ,
188+ context_lines : int = 3 ,
189+ include_tables : Optional [ List [ Text ]] = None ,
190+ exclude_tables : Optional [ List [ Text ]] = None ,
191+ use_multiprocess : bool = True ,
192+ ) -> Iterator [ Text ] :
174193 """Performs a unified diff on a TTX serialized data format dump of font binary data using
175194 a modified version of the Python standard libary difflib module.
176195
@@ -230,13 +249,13 @@ def u_diff(
230249
231250
232251def external_diff (
233- command ,
234- filepath_a ,
235- filepath_b ,
236- include_tables = None ,
237- exclude_tables = None ,
238- use_multiprocess = True ,
239- ):
252+ command : Text ,
253+ filepath_a : Text ,
254+ filepath_b : Text ,
255+ include_tables : Optional [ List [ Text ]] = None ,
256+ exclude_tables : Optional [ List [ Text ]] = None ,
257+ use_multiprocess : bool = True ,
258+ ) -> Iterable [ Tuple [ Text , Optional [ int ]]] :
240259 """Performs a unified diff on a TTX serialized data format dump of font binary data using
241260 an external diff executable that is requested by the caller via `command`
242261
@@ -285,10 +304,10 @@ def external_diff(
285304 )
286305
287306 while True :
288- output = process .stdout .readline ()
307+ output = process .stdout .readline () # type: ignore
289308 exit_status = process .poll ()
290309 if len (output ) == 0 and exit_status is not None :
291- err = process .stderr .read ()
310+ err = process .stderr .read () # type: ignore
292311 if err :
293312 raise IOError (err )
294313 yield output , exit_status
0 commit comments