2222import puremagic
2323
2424if os .getenv ("PUREMAGIC_DEEPSCAN" ) != "0" :
25- from puremagic .scanners import ( zip_scanner , pdf_scanner , text_scanner , json_scanner , python_scanner , sndhdr_scanner )
25+ from puremagic .scanners import zip_scanner , pdf_scanner , text_scanner , json_scanner , python_scanner , sndhdr_scanner
2626
2727__author__ = "Chris Griffith"
2828__version__ = "2.0.0b5"
@@ -68,6 +68,10 @@ class PureError(LookupError):
6868 """Do not have that type of file in our databanks"""
6969
7070
71+ class PureValueError (ValueError ):
72+ """Invalid input"""
73+
74+
7175def magic_data (
7276 filename : os .PathLike | str = os .path .join (here , "magic_data.json" ),
7377) -> tuple [list [PureMagic ], list [PureMagic ], list [PureMagic ], dict [bytes , list [PureMagic ]]]:
@@ -193,7 +197,7 @@ def identify_all(header: bytes, footer: bytes, ext=None) -> list[PureMagicWithCo
193197def perform_magic (header : bytes , footer : bytes , mime : bool , ext = None , filename = None ) -> str :
194198 """Discover what type of file it is based on the incoming string"""
195199 if not header :
196- raise ValueError ("Input was empty" )
200+ raise PureValueError ("Input was empty" )
197201 infos = identify_all (header , footer , ext )
198202 if filename and os .getenv ("PUREMAGIC_DEEPSCAN" ) != "0" :
199203 results = run_deep_scan (infos , filename , header , footer , raise_on_none = True )
@@ -322,7 +326,7 @@ def magic_file(filename: os.PathLike | str) -> list[PureMagicWithConfidence]:
322326 """
323327 head , foot = file_details (filename )
324328 if not head :
325- raise ValueError ("Input was empty" )
329+ raise PureValueError ("Input was empty" )
326330 try :
327331 info = identify_all (head , foot , ext_from_filename (filename ))
328332 except PureError :
@@ -344,7 +348,7 @@ def magic_string(string, filename: os.PathLike | str | None = None) -> list[Pure
344348 :return: list of possible matches, highest confidence first
345349 """
346350 if not string :
347- raise ValueError ("Input was empty" )
351+ raise PureValueError ("Input was empty" )
348352 head , foot = string_details (string )
349353 ext = ext_from_filename (filename ) if filename else None
350354 info = identify_all (head , foot , ext )
@@ -366,7 +370,7 @@ def magic_stream(
366370 """
367371 head , foot = stream_details (stream )
368372 if not head :
369- raise ValueError ("Input was empty" )
373+ raise PureValueError ("Input was empty" )
370374 ext = ext_from_filename (filename ) if filename else None
371375 info = identify_all (head , foot , ext )
372376 info .sort (key = lambda x : x .confidence , reverse = True )
@@ -389,17 +393,13 @@ def single_deep_scan(
389393 return zip_scanner .main (filename , head , foot )
390394 case pdf_scanner .match_bytes :
391395 return pdf_scanner .main (filename , head , foot )
392- case (
393- sndhdr_scanner .hcom_match_bytes
394- | sndhdr_scanner .fssd_match_bytes
395- | sndhdr_scanner .sndr_match_bytes
396- ):
396+ case sndhdr_scanner .hcom_match_bytes | sndhdr_scanner .fssd_match_bytes | sndhdr_scanner .sndr_match_bytes :
397397 # sndr is a loose confidence and other results may be better
398398 result = sndhdr_scanner .main (filename , head , foot )
399399 if result and result .confidence > confidence :
400400 return result
401401
402- # The first match wins, so text_scanner should always be the last
402+ # The first match wins
403403 for scanner in (pdf_scanner , python_scanner , json_scanner ):
404404 result = scanner .main (filename , head , foot )
405405 if result :
@@ -446,7 +446,7 @@ def run_deep_scan(
446446 try :
447447 result = catch_all_deep_scan (filename , head , foot )
448448 except Exception :
449- pass
449+ raise
450450 else :
451451 if result :
452452 return [result ]
@@ -491,19 +491,29 @@ def command_line_entry(*args):
491491 help = "Return the mime type instead of file type" ,
492492 )
493493 parser .add_argument ("-v" , "--verbose" , action = "store_true" , dest = "verbose" , help = "Print verbose output" )
494- parser .add_argument ("files" , nargs = "+" )
494+ parser .add_argument ("files" , nargs = "+" , type = Path )
495495 parser .add_argument ("--version" , action = "version" , version = puremagic .__version__ )
496496 args = parser .parse_args (args if args else sys .argv [1 :])
497497
498498 for fn in args .files :
499- if not os . path . exists (fn ):
499+ if not fn . exists ():
500500 print (f"File '{ fn } ' does not exist!" )
501501 continue
502- try :
503- print (f"'{ fn } ' : { from_file (fn , args .mime )} " )
504- except PureError :
505- print (f"'{ fn } ' : could not be Identified" )
506- continue
502+ if fn .is_dir ():
503+ for file in fn .iterdir ():
504+ if not file .is_file ():
505+ continue
506+ try :
507+ print (f"'{ file } ' : { from_file (file , args .mime )} " )
508+ except (PureError , PureValueError ):
509+ print (f"'{ file } ' : could not be Identified" )
510+ continue
511+ else :
512+ try :
513+ print (f"'{ fn } ' : { from_file (fn , args .mime )} " )
514+ except (PureError , PureValueError ):
515+ print (f"'{ fn } ' : could not be Identified" )
516+ continue
507517 if args .verbose :
508518 matches = magic_file (fn )
509519 print (f"Total Possible Matches: { len (matches )} " )
0 commit comments