@@ -109,10 +109,11 @@ def __new__(cls, name):
109
109
for action , examples in (self .codecs [0 ].parameters .get ('examples' , {}) or {'enc-dec(' : ["T3st str!" ]}).items ():
110
110
if re .match (r"enc(-dec)?\(" , action ):
111
111
for e in (examples .keys () if action .startswith ("enc(" ) else examples or []):
112
- rd = re .match (r"\@random(?:\{(\d+(?:,(\d+))*?)\})?$" , e )
112
+ rd = re .match (r"\@(i?) random(?:\{(\d+(?:,(\d+))*?)\})?$" , e )
113
113
if rd :
114
- for n in (rd .group (1 ) or "512" ).split ("," ):
115
- self .encode ("" .join (chr (randint (0 , 255 )) for i in range (int (n ))))
114
+ for n in (rd .group (2 ) or "512" ).split ("," ):
115
+ s = "" .join (chr (randint (0 , 255 )) for i in range (int (n )))
116
+ self .encode (s .lower () if rd .group (1 ) else s )
116
117
continue
117
118
self .encode (e )
118
119
@@ -1276,10 +1277,9 @@ def __make_encodings_dict(include, exclude):
1276
1277
def _develop (d , keep = True ):
1277
1278
d = d or {}
1278
1279
for k , v in d .items ():
1279
- l , cc = [], [e for e in v if e in CODECS_CATEGORIES ]
1280
+ l , cc , sc = [], [e for e in v if e in CODECS_CATEGORIES ], [ e for e in v if e not in CODECS_CATEGORIES ]
1280
1281
# list from in-scope categories and then everything that is not a category
1281
- for enc in ((list_encodings (* cc ) if len (cc ) > 0 or keep else []) + \
1282
- [e for e in v if e not in CODECS_CATEGORIES ]):
1282
+ for enc in ((list_encodings (* cc ) if (len (cc ) > 0 or keep ) and len (sc ) == 0 else []) + sc ):
1283
1283
g = []
1284
1284
for e in (search (enc , False ) or [enc ]):
1285
1285
try :
@@ -1293,8 +1293,8 @@ def _develop(d, keep=True):
1293
1293
l .extend (g )
1294
1294
d [k ] = list (set (l ))
1295
1295
return d
1296
- exclude = _develop (exclude , False )
1297
- return {k : [x for x in v if x not in exclude .get (k , [])] for k , v in _develop ( include ) .items ()}
1296
+ _excl , _incl = _develop (exclude , False ), _develop ( include )
1297
+ return {k : [x for x in v if x not in _excl .get (k , [])] for k , v in _incl .items ()}
1298
1298
1299
1299
1300
1300
def __rank (prev_input , input , prev_encoding , encodings , heuristic = False , extended = False , yield_score = False ):
@@ -1304,7 +1304,10 @@ def __rank(prev_input, input, prev_encoding, encodings, heuristic=False, extende
1304
1304
try :
1305
1305
codec = CODECS_CACHE [e ]
1306
1306
except KeyError :
1307
- CODECS_CACHE [e ] = codec = lookup (e , False )
1307
+ try :
1308
+ CODECS_CACHE [e ] = codec = lookup (e , False )
1309
+ except LookupError :
1310
+ continue
1308
1311
t = __score (prev_input , input , prev_encoding , e , codec , heuristic , extended )
1309
1312
if t :
1310
1313
ranking [e ] = t
@@ -1321,7 +1324,7 @@ def __init__(self, text, pad_char=None):
1321
1324
pad_char , last_char = (chr (pad_char ), chr (c )) if isinstance (c , int ) else (pad_char , c )
1322
1325
self .padding = pad_char is not None and last_char == pad_char
1323
1326
if self .padding :
1324
- text = text .rstrip (pad_char )
1327
+ text = text .rstrip (b ( pad_char ) if isinstance ( text , bytes ) else pad_char )
1325
1328
self .len = len (self .text )
1326
1329
self .lcharset = len (set (self .text ))
1327
1330
self .printables = float (len ([c for c in self .text if c in printable ])) / self .len
@@ -1501,7 +1504,8 @@ def rank(input, extended=False, limit=-1, include=None, exclude=None):
1501
1504
:param include: inclusion list with category, codec or encoding names (nothing means include every encoding)
1502
1505
:param exclude: exclusion list with category, codec or encoding names (nothing means exclude no encoding)
1503
1506
"""
1504
- encodings = __make_encodings_dict ({- 1 : include or CODECS_CATEGORIES }, {- 1 : exclude or []})
1507
+ encodings = __make_encodings_dict (include if isinstance (include , dict ) else {- 1 : include or CODECS_CATEGORIES },
1508
+ exclude if isinstance (exclude , dict ) else {- 1 : exclude or []})
1505
1509
r = list (__rank (None , input , "" , encodings [- 1 ], True , extended , True ))
1506
1510
return r [:limit ] if len (r ) > 1 else r
1507
1511
codecs .rank = rank
0 commit comments