12
12
import time
13
13
from collections import OrderedDict
14
14
from functools import cmp_to_key
15
+ from typing import Iterable , Optional , Tuple , Union
15
16
16
17
from . import util
17
18
from .coq_version import (
@@ -184,7 +185,20 @@ def os_path_isfile(filename):
184
185
return os .path .isfile (filename )
185
186
186
187
187
- def get_loadpath (coqc , coqc_args , * , coqc_is_coqtop = False , verbose_base = 1 , base_dir = None , ** kwargs ):
188
+ @memoize
189
+ def Path_isfile (filename : Path ) -> bool :
190
+ return filename .is_file ()
191
+
192
+
193
+ def get_loadpath (
194
+ coqc ,
195
+ coqc_args ,
196
+ * ,
197
+ coqc_is_coqtop : bool = False ,
198
+ verbose_base : int = 1 ,
199
+ base_dir : Optional [Union [str , Path ]] = None ,
200
+ ** kwargs
201
+ ) -> Iterable [Tuple [Path , str ]]:
188
202
kwargs = fill_kwargs (kwargs )
189
203
output , cmds , retcode , runtime = get_coq_output (
190
204
coqc ,
@@ -219,7 +233,7 @@ def get_loadpath(coqc, coqc_args, *, coqc_is_coqtop=False, verbose_base=1, base_
219
233
kwargs ["log" ](f"WARNING: Print LoadPath has extra: { extra !r} " , level = LOG_ALWAYS )
220
234
for logical , physical in reg .findall (output ):
221
235
logical = logical .strip (" \r \n " )
222
- physical = physical .strip (" \r \n " )
236
+ physical = Path ( physical .strip (" \r \n " ) )
223
237
if logical == "<>" :
224
238
logical = ""
225
239
yield (physical , logical )
@@ -230,35 +244,34 @@ def get_loadpath(coqc, coqc_args, *, coqc_is_coqtop=False, verbose_base=1, base_
230
244
yield (physical , "Coq." + logical [len ("Stdlib." ) :])
231
245
232
246
233
- def filenames_of_lib_helper (lib , non_recursive_libnames , ext ):
247
+ def filenames_of_lib_helper (lib : str , non_recursive_libnames : Iterable [ Tuple [ Path , str ]], ext : str ):
234
248
for physical_name , logical_name in list (non_recursive_libnames ):
235
249
# logical_name = libname_with_dot(logical_name)
236
250
* libprefix , lib = lib .split ("." )
237
251
if "." .join (libprefix ) == logical_name :
238
- cur_lib = os .path .join (physical_name , lib )
239
- # TODO HERE USE pathlib
240
- yield fix_path (os .path .relpath (os .path .normpath (cur_lib + ext ), "." ))
252
+ cur_lib = physical_name / lib
253
+ yield cur_lib .with_suffix (ext )
241
254
242
255
243
256
@memoize
244
- def filename_of_lib_helper (lib , non_recursive_libnames , ext , base_dir ):
257
+ def filename_of_lib_helper (
258
+ lib : str , non_recursive_libnames : Iterable [Tuple [Path , str ]], ext : str , base_dir : Optional [Union [Path , str ]]
259
+ ):
245
260
filenames = list (filenames_of_lib_helper (lib , non_recursive_libnames , ext ))
246
- existing_filenames = [f for f in filenames if os_path_isfile (f ) or os_path_isfile ( os . path . splitext ( f )[ 0 ] + ".v" )]
261
+ existing_filenames = [f for f in filenames if Path_isfile (f ) or Path_isfile ( f . with_suffix ( ".v" ) )]
247
262
if len (existing_filenames ) > 0 :
248
263
retval = existing_filenames [0 ]
249
264
if len (set (existing_filenames )) == 1 :
250
265
return retval
251
266
else :
252
267
DEFAULT_LOG (
253
- "WARNING: Multiple physical paths match logical path %s: %s. Selecting %s."
254
- % (lib , ", " .join (sorted (set (existing_filenames ))), retval ),
268
+ f"WARNING: Multiple physical paths match logical path { lib } : { ', ' .join (sorted (set (existing_filenames )))} . Selecting { retval } ." ,
255
269
level = LOG_ALWAYS ,
256
270
)
257
271
return retval
258
272
if len (filenames ) != 0 :
259
273
DEFAULT_LOG (
260
- "WARNING: One or more physical paths match logical path %s, but none of them exist: %s"
261
- % (lib , ", " .join (filenames )),
274
+ f"WARNING: One or more physical paths match logical path { lib } , but none of them exist: { ', ' .join (filenames )} ." ,
262
275
level = LOG_ALWAYS ,
263
276
)
264
277
if len (filenames ) > 0 :
0 commit comments