@@ -1154,7 +1154,7 @@ def generate_strings_from_regex(regex, star_plus_max=STAR_PLUS_MAX, repeat_max=R
11541154stopfunc .default = stopfunc .text
11551155
11561156stopfunc .LANG_BACKEND = None
1157- stopfunc .LANG_BACKENDS = [n for n in ["langid " , "langdetect" , "pycld2 " , "cld3" , "textblob" ] if __module_exists (n )]
1157+ stopfunc .LANG_BACKENDS = [n for n in ["pycld2 " , "langdetect" , "langid " , "cld3" , "textblob" ] if __module_exists (n )]
11581158if len (stopfunc .LANG_BACKENDS ) > 0 :
11591159 stopfunc .LANG_BACKEND = stopfunc .LANG_BACKENDS [0 ]
11601160if "cld3" in stopfunc .LANG_BACKENDS :
@@ -1223,7 +1223,6 @@ def _load_lang_backend(backend=None):
12231223 flng = "lang_%s" % LANG
12241224 if getattr (stopfunc , flng , None ):
12251225 stopfunc .default = getattr (stopfunc , flng )
1226- _load_lang_backend (stopfunc .LANG_BACKEND )
12271226stopfunc ._reload_lang = _load_lang_backend
12281227
12291228
@@ -1244,10 +1243,12 @@ def __develop(encodings):
12441243
12451244
12461245def __guess (prev_input , input , stop_func , depth , max_depth , min_depth , codec_categories , exclude , result , found = (),
1247- stop = True , show = False , scoring_heuristic = False , extended = False , debug = False ):
1246+ stop = True , show = False , scoring_heuristic = False , extended = False , debug = False , regex = False ):
12481247 """ Perform a breadth-first tree search using a ranking logic to select and prune the list of codecs. """
12491248 if depth > min_depth and stop_func (input ):
1250- if not stop and show and found not in result :
1249+ if regex :
1250+ stop = True
1251+ if not stop and (show or debug ) and found not in result :
12511252 s = repr (input )
12521253 s = s [2 :- 1 ] if s .startswith ("b'" ) and s .endswith ("'" ) else s
12531254 s = "[+] %s: %s" % (", " .join (found ), s )
@@ -1287,7 +1288,7 @@ def expand(items, descr=None, transform=None):
12871288 if debug :
12881289 print ("[*] Depth %0{}d/%d: %s" .format (len (str (max_depth ))) % (depth + 1 , max_depth , encoding ))
12891290 __guess (input , new_input , stop_func , depth + 1 , max_depth , min_depth , codec_categories , exclude , result ,
1290- found + (encoding , ), stop , show , scoring_heuristic , extended , debug )
1291+ found + (encoding , ), stop , show , scoring_heuristic , extended , debug , regex )
12911292
12921293
12931294def __rank (prev_input , input , prev_encoding , codecs , heuristic = False , extended = False , yield_score = False ):
@@ -1374,7 +1375,7 @@ def __score(prev_input, input, prev_encoding, codec, heuristic=False, extended=F
13741375 epxf = f - .1 <= expf <= f + .1
13751376 elif isinstance (expf , (tuple , list )) and len (expf ) == 2 :
13761377 expf = f - expf [1 ] <= expf [0 ] <= expf [1 ] + .1
1377- s += .1
1378+ s += [ - 1. , .1 ][ expf ]
13781379 # afterwards, if the input text has an entropy close to the expected one, give a bonus weighted on the
13791380 # number of input characters to take bad entropies of shorter strings into account
13801381 entr = sc .get ('entropy' , {})
@@ -1408,18 +1409,22 @@ def guess(input, stop_func=stopfunc.default, min_depth=0, max_depth=5, codec_cat
14081409 """ Try decoding without the knowledge of the encoding(s). """
14091410 if max_depth <= 0 :
14101411 raise ValueError ("Depth must be a non-null positive integer" )
1412+ if min_depth > max_depth :
1413+ raise ValueError ("Min depth shall be less than or equal to the max depth" )
14111414 if len (found ) > 0 :
14121415 for encoding in found :
14131416 input = decode (input , encoding )
1417+ regex = False
14141418 if isinstance (stop_func , string_types ):
14151419 stop_func = stopfunc .regex (stop_func )
1420+ regex = True
14161421 result = {}
14171422 if len (input ) > 0 :
14181423 try :
14191424 # breadth-first search
14201425 for d in range (max_depth ):
14211426 __guess ("" , input , stop_func , 0 , d + 1 , min_depth , codec_categories , exclude , result , tuple (found ), stop ,
1422- show , scoring_heuristic , extended , debug )
1427+ show , scoring_heuristic , extended , debug , regex )
14231428 if stop and len (result ) > 0 :
14241429 return result
14251430 except KeyboardInterrupt :
0 commit comments