@@ -296,13 +296,11 @@ class ParsingStrategy(NamedTuple):
296
296
execute : Callable [[Path ], Iterator [DeclaredDependency ]]
297
297
298
298
299
- def first_applicable_parser (
300
- path : Path ,
301
- ) -> Optional [Tuple [ParserChoice , ParsingStrategy ]]:
299
+ def first_applicable_parser (path : Path ) -> Optional [ParserChoice ]:
302
300
"""Find the first applicable parser choice for given path."""
303
301
return next (
304
302
(
305
- ( choice , parser )
303
+ choice
306
304
for choice , parser in PARSER_CHOICES .items ()
307
305
if parser .applies_to_path (path )
308
306
),
@@ -328,44 +326,31 @@ def first_applicable_parser(
328
326
}
329
327
330
328
331
- def parse_source (
332
- src : DepsSource , parser_choice : Optional [ParserChoice ] = None
333
- ) -> Iterator [DeclaredDependency ]:
329
+ def parse_source (src : DepsSource ) -> Iterator [DeclaredDependency ]:
334
330
"""Extract dependencies (package names) from supported file types.
335
331
336
- Pass a path from which to discover and parse dependency declarations. Pass
337
- a directory to traverse that directory tree to find and automatically parse
338
- any supported files .
332
+ Pass a DepsSource objects which specifies the path to the file containing
333
+ the dependency declarations, as well as a parser choice to select the
334
+ parsing strategy for this file .
339
335
340
336
Generate (i.e. yield) a DeclaredDependency object for each dependency found.
341
337
There is no guaranteed ordering on the generated dependencies.
342
338
"""
343
- assert src .path .is_file () # sanity check
344
- if parser_choice is not None :
345
- parser = PARSER_CHOICES [parser_choice ]
346
- if not parser .applies_to_path (src .path ):
347
- logger .warning (
348
- f"Manually applying parser '{ parser_choice } ' to dependencies: { src .path } "
349
- )
350
- else :
351
- choice_and_parser = first_applicable_parser (src .path )
352
- if choice_and_parser is None : # nothing found. SHOULD NOT HAPPEN!
353
- raise UnparseablePathException (
354
- ctx = "Parsing given dependencies path isn't supported" , path = src .path
355
- )
356
- parser = choice_and_parser [1 ]
339
+ parser = PARSER_CHOICES [src .parser_choice ]
340
+ if not parser .applies_to_path (src .path ):
341
+ logger .warning (
342
+ f"Manually applying parser '{ src .parser_choice } ' to dependencies: { src .path } "
343
+ )
357
344
yield from parser .execute (src .path )
358
345
359
346
360
- def parse_sources (
361
- sources : Iterable [DepsSource ], parser_choice : Optional [ParserChoice ] = None
362
- ) -> Iterator [DeclaredDependency ]:
347
+ def parse_sources (sources : Iterable [DepsSource ]) -> Iterator [DeclaredDependency ]:
363
348
"""Extract dependencies (package names) from supported file types.
364
349
365
350
Pass sources from which to parse dependency declarations.
366
351
"""
367
352
for source in sources :
368
- yield from parse_source (source , parser_choice = parser_choice )
353
+ yield from parse_source (source )
369
354
370
355
371
356
def validate_deps_source (
@@ -381,43 +366,30 @@ def validate_deps_source(
381
366
parseable files within.
382
367
- Raise UnparseablePathException if the given path cannot be parsed.
383
368
384
- The given 'parser_choice' and 'filter_by_parser' change which files we
385
- consider valid sources:
386
-
387
- - If parser_choice is None, we will consider any file path that matches one
388
- of the PARSER_CHOICES above a valid file.
389
- - If parser_choice is not None, and filter_by_parser is False, we assume
390
- that the given file path was explicitly passed by the user, and meant to
391
- be parsed with the given parser_choice, _even_ when the file path does not
392
- nominally match this parser choice: We return the file path as a valid
393
- source regardless of its file name/suffix.
394
- - If parser_choice is not None, and filter_by_parser is True, we assume that
395
- the given file path was encountered while walking a directory passed by
396
- the user. In this case, we will only consider the file valid if it matches
397
- the given parser choice.
369
+ The given 'parser_choice' and 'filter_by_parser' determine which file paths
370
+ we consider valid sources, and how they are parsed: With parser_choice=None,
371
+ a file path will use the first matching parser in PARSER_CHOICES above, if
372
+ any. Otherwise - when parser_choice is specified - the file must either
373
+ match this parser (filter_by_parser=True), or this parser will be forced
374
+ even if the file does not match (filter_by_parser=False).
398
375
"""
399
- if path .is_file ():
400
- choice_and_parser : Optional [Tuple [ParserChoice , ParsingStrategy ]] = None
401
- if parser_choice is not None : # user wants a specific parser
402
- strategy = PARSER_CHOICES [parser_choice ]
403
- if filter_by_parser : # but only if the file matches
404
- if strategy .applies_to_path (path ):
405
- choice_and_parser = (parser_choice , strategy )
406
- else :
407
- raise UnparseablePathException (
408
- ctx = f"Path does not match { parser_choice } parser" , path = path
409
- )
410
- else : # user wants us to use this parser no matter what
411
- choice_and_parser = (parser_choice , strategy )
412
- else : # no parser chosen, automatically determine parser for this path
413
- choice_and_parser = first_applicable_parser (path )
414
- if choice_and_parser is None : # no suitable parser found
415
- raise UnparseablePathException (
416
- ctx = "Parsing given dependencies path isn't supported" , path = path
417
- )
418
- return DepsSource (path )
419
376
if path .is_dir ():
420
377
return None
421
- raise UnparseablePathException (
422
- ctx = "Dependencies declaration path is neither dir nor file" , path = path
423
- )
378
+ if not path .is_file ():
379
+ raise UnparseablePathException (
380
+ ctx = "Dependencies declaration path is neither dir nor file" , path = path
381
+ )
382
+
383
+ if parser_choice is not None : # user wants a specific parser
384
+ if filter_by_parser : # but only if the file matches
385
+ if not PARSER_CHOICES [parser_choice ].applies_to_path (path ):
386
+ raise UnparseablePathException (
387
+ ctx = f"Path does not match { parser_choice } parser" , path = path
388
+ )
389
+ else : # no parser chosen, automatically determine parser for this path
390
+ parser_choice = first_applicable_parser (path )
391
+ if parser_choice is None : # no suitable parser given
392
+ raise UnparseablePathException (
393
+ ctx = "Parsing given dependencies path isn't supported" , path = path
394
+ )
395
+ return DepsSource (path , parser_choice )
0 commit comments