7
7
import shlex
8
8
import subprocess
9
9
import tempfile
10
+ from typing import Any , Iterator , Iterable , List , Optional , Text , Tuple
10
11
11
- from fontTools .ttLib import TTFont
12
+ from fontTools .ttLib import TTFont # type: ignore
12
13
13
- from fdiff .exceptions import AIOError
14
- from fdiff .remote import (
14
+ from .exceptions import AIOError
15
+ from .remote import (
15
16
_get_filepath_from_url ,
16
17
create_async_get_request_session_and_run ,
17
18
)
18
19
19
- from fdiff .utils import get_file_modtime
20
+ from .utils import get_file_modtime
20
21
21
22
22
23
#
26
27
#
27
28
28
29
29
- def _async_fetch_files (dirpath , urls ) :
30
+ def _async_fetch_files (dirpath : Text , urls : List [ Text ]) -> None :
30
31
loop = asyncio .get_event_loop ()
31
32
tasks = loop .run_until_complete (
32
33
create_async_get_request_session_and_run (urls , dirpath )
@@ -45,8 +46,13 @@ def _async_fetch_files(dirpath, urls):
45
46
46
47
47
48
def _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 ]:
50
56
post_pathname , postpath , pre_pathname , prepath = _get_pre_post_paths (
51
57
filepath_a , filepath_b , tmpdirpath
52
58
)
@@ -69,8 +75,12 @@ def _get_fonts_and_save_xml(
69
75
return left_ttxpath , right_ttxpath , pre_pathname , prepath , post_pathname , postpath
70
76
71
77
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 ] = []
74
84
if filepath_a .startswith ("http" ):
75
85
urls .append (filepath_a )
76
86
prepath = _get_filepath_from_url (filepath_a , dirpath )
@@ -94,14 +104,14 @@ def _get_pre_post_paths(filepath_a, filepath_b, dirpath):
94
104
95
105
96
106
def _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 :
105
115
if use_multiprocess and cpu_count () > 1 :
106
116
# Use parallel fontTools.ttLib.TTFont.saveXML dump
107
117
# by default on multi CPU systems. This is a performance
@@ -121,13 +131,20 @@ def _mp_save_ttx_xml(
121
131
_ttfont_save_xml (tt_right , right_ttxpath , include_tables , exclude_tables )
122
132
123
133
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 :
125
140
"""Writes TTX specification formatted XML to disk on filepath."""
126
141
ttf .saveXML (filepath , tables = include_tables , skipTables = exclude_tables )
127
142
return True
128
143
129
144
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 :
131
148
# Validation: exclude_tables request should be for tables that are in one of
132
149
# the two fonts. Mis-specified OT table definitions could otherwise result
133
150
# 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):
140
157
)
141
158
142
159
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 :
144
163
# Validation: include_tables request should be for tables that are in one of
145
164
# the two fonts. This otherwise silently passes with exit status code 0 which
146
165
# 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):
164
183
165
184
166
185
def 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 ] :
174
193
"""Performs a unified diff on a TTX serialized data format dump of font binary data using
175
194
a modified version of the Python standard libary difflib module.
176
195
@@ -230,13 +249,13 @@ def u_diff(
230
249
231
250
232
251
def 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 ]]] :
240
259
"""Performs a unified diff on a TTX serialized data format dump of font binary data using
241
260
an external diff executable that is requested by the caller via `command`
242
261
@@ -285,10 +304,10 @@ def external_diff(
285
304
)
286
305
287
306
while True :
288
- output = process .stdout .readline ()
307
+ output = process .stdout .readline () # type: ignore
289
308
exit_status = process .poll ()
290
309
if len (output ) == 0 and exit_status is not None :
291
- err = process .stderr .read ()
310
+ err = process .stderr .read () # type: ignore
292
311
if err :
293
312
raise IOError (err )
294
313
yield output , exit_status
0 commit comments