@@ -446,6 +446,8 @@ def list_categories():
446446 for d in os .listdir (root ):
447447 if os .path .isdir (os .path .join (root , d )) and not d .startswith ("__" ):
448448 c .append (d .rstrip ("s" ))
449+ # particular category, hardcoded from base/_base.py
450+ c += ["base-generic" ]
449451 return c
450452
451453
@@ -844,7 +846,8 @@ def generate_strings_from_regex(regex, star_plus_max=STAR_PLUS_MAX, repeat_max=R
844846 - `printables`: checks that every output character is in the set of printables
845847""" )
846848stopfunc .printables = lambda s : all (c in printable for c in ensure_str (s ))
847- stopfunc .regex = lambda p : lambda s : re .search (p , ensure_str (s ), re .I ) is not None
849+ stopfunc .regex = lambda p : lambda s : re .search (p , ensure_str (s )) is not None
850+ stopfunc .text = lambda s : stopfunc .printables (s ) and entropy (s ) < 4.5
848851
849852try :
850853 from langdetect import detect , PROFILES_DIRECTORY
@@ -864,15 +867,14 @@ def _flag(x):
864867
865868
866869def __guess (prev_input , input , stop_func , depth , max_depth , codec_categories , exclude , result , found = (), stop = True ,
867- show = False , scoring_heuristic = False , extended = False ):
870+ show = False , scoring_heuristic = False , extended = False , debug = False ):
868871 """ Perform a breadth-first tree search using a ranking logic to select and prune the list of codecs. """
869872 if depth > 0 and stop_func (input ):
870- if not stop and show :
871- s = "[* ] %s: %s" % (", " .join (found ), ensure_str (input ))
873+ if not stop and show and found not in result :
874+ s = "[+ ] %s: %s" % (", " .join (found ), ensure_str (input ))
872875 print (s if len (s ) <= 80 else s [:77 ] + "..." )
873- result .append ((input , found ))
874- return
875- if depth >= max_depth or len (result ) > 0 :
876+ result [found ] = input
877+ if depth >= max_depth or len (result ) > 0 and stop :
876878 return
877879 # compute included and excluded codecs for this depth
878880 def expand (items , descr = None , transform = None ):
@@ -898,10 +900,14 @@ def expand(items, descr=None, transform=None):
898900 # parse valid encodings, expanding included/excluded codecs
899901 c , e = expand (codec_categories , "codec_categories" , list_encodings ), expand (exclude , "exclude" )
900902 for new_input , encoding in __rank (prev_input , input , c , scoring_heuristic , extended ):
903+ if len (result ) > 0 and stop :
904+ return
901905 if encoding in e :
902906 continue
907+ if debug :
908+ print ("[*] Depth %d/%d ; trying %s" % (depth + 1 , max_depth , encoding ))
903909 __guess (input , new_input , stop_func , depth + 1 , max_depth , codec_categories , exclude , result ,
904- found + (encoding , ), stop , show , extended )
910+ found + (encoding , ), stop , show , scoring_heuristic , extended , debug )
905911
906912
907913def __rank (prev_input , input , codecs , heuristic = False , extended = False ):
@@ -994,8 +1000,8 @@ def __score(prev_input, input, codec, heuristic=False, extended=False):
9941000 yield s , new_input , encoding
9951001
9961002
997- def guess (input , stop_func = stopfunc .printables , max_depth = 5 , codec_categories = None , exclude = None , found = (), stop = True ,
998- show = False , scoring_heuristic = False , extended = False ):
1003+ def guess (input , stop_func = stopfunc .printables , max_depth = 5 , codec_categories = None , exclude = None , result = None , found = () ,
1004+ stop = True , show = False , scoring_heuristic = False , extended = False , debug = False ):
9991005 """ Try decoding without the knowledge of the encoding(s). """
10001006 if max_depth <= 0 :
10011007 raise ValueError ("Depth must be a non-null positive integer" )
@@ -1005,10 +1011,11 @@ def guess(input, stop_func=stopfunc.printables, max_depth=5, codec_categories=No
10051011 if isinstance (stop_func , string_types ):
10061012 stop_func = stopfunc .regex (stop_func )
10071013 if len (input ) > 0 :
1008- result = []
1014+ result = result or {}
1015+ # breadth-first search
10091016 for d in range (max_depth ):
10101017 __guess ("" , input , stop_func , 0 , d + 1 , codec_categories , exclude , result , tuple (found ), stop , show ,
1011- scoring_heuristic , extended )
1018+ scoring_heuristic , extended , debug )
10121019 if stop and len (result ) > 0 :
10131020 return result
10141021 return result
0 commit comments