Skip to content

Commit 25a6dc6

Browse files
committed
Fixed #8
1 parent 1048785 commit 25a6dc6

5 files changed

Lines changed: 30 additions & 27 deletions

File tree

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[pytest]
2-
pythonpath = src
2+
python_paths = src

src/codext/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.15.1
1+
1.15.3

src/codext/__common__.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ def __new__(cls, name):
104104
try:
105105
self.codecs = MACROS[name]
106106
except KeyError:
107-
raise LookupError("unknown macro: %s" % name)
107+
raise LookupError(f"unknown macro: {name}")
108108
if not isinstance(self.codecs, (tuple, list)):
109-
raise ValueError("bad macro list: %s" % str(self.codecs))
109+
raise ValueError(f"bad macro list: {self.codecs}")
110110
self.codecs = [lookup(e, False) for e in self.codecs] # lookup(e, False)
111111
self.parameters = {'name': name, 'category': "macro"} # ^ means that macros won't be nestable
112112
# test examples to check that the chain of encodings works
@@ -158,7 +158,7 @@ def encode(self, input, error="strict"):
158158
return input, l
159159

160160
def __repr__(self):
161-
return "<codext.CodecMacro object for encoding %s at %#x>" % (self.name, id(self))
161+
return f"<codext.CodecMacro object for encoding {self.name} at {id(self):#x}>"
162162

163163

164164
# inspired from: https://stackoverflow.com/questions/10875442/possible-to-change-a-functions-repr-in-python
@@ -172,7 +172,7 @@ def __call__(self, *args, **kwargs):
172172
return self.__func(*args, **kwargs)
173173

174174
def __repr__(self):
175-
return "<search-function %s at 0x%x>" % (self.__name, id(self))
175+
return f"<search-function {self.__name} at {id(self):#x}>"
176176

177177

178178
def __stdin_pipe():
@@ -200,7 +200,7 @@ def _input(infile):
200200

201201
def _set_exc(name, etype="ValueError"):
202202
if not hasattr(builtins, name):
203-
exec("class %s(%s): __module__ = 'builtins'" % (name, etype))
203+
exec(f"class {name}({etype}): __module__ = 'builtins'")
204204
setattr(builtins, name, locals()[name])
205205
_set_exc("InputSizeLimitError")
206206
_set_exc("ParameterError")
@@ -237,11 +237,11 @@ def add(ename, encode=None, decode=None, pattern=None, text=True, add_to_codecs=
237237
if encode:
238238
if not isinstance(encode, FunctionType):
239239
raise ValueError("Bad 'encode' function")
240-
_set_exc("%sEncodeError" % exc_name(ename)) # create the custom encode exception as a builtin
240+
_set_exc(f"{exc_name(ename)}EncodeError") # create the custom encode exception as a builtin
241241
if decode:
242242
if not isinstance(decode, FunctionType):
243243
raise ValueError("Bad 'decode' function")
244-
_set_exc("%sDecodeError" % exc_name(ename)) # create the custom decode exception as a builtin
244+
_set_exc(f"{exc_name(ename)}DecodeError") # create the custom decode exception as a builtin
245245
if not encode and not decode:
246246
raise ValueError("At least one en/decoding function must be defined")
247247
for exc in kwargs.get('extra_exceptions', []):
@@ -375,7 +375,7 @@ def add_macro(mname, *encodings):
375375
raise ValueError("Macro name already exists")
376376
try:
377377
ci = lookup(mname, False)
378-
raise ValueError("Macro name clashes with codec '%s'" % ci.name)
378+
raise ValueError(f"Macro name clashes with codec '{ci.name}'")
379379
except LookupError:
380380
pass
381381
try:
@@ -463,7 +463,7 @@ def _wrapper(param):
463463
isinstance(mapdict, dict) and p in mapdict.keys():
464464
smapdict = {k: v for k, v in mapdict[p].items()}
465465
else:
466-
raise LookupError("Bad parameter for encoding '{}': '{}'".format(ename, p))
466+
raise LookupError(f"Bad parameter for encoding '{ename}': '{p}'")
467467
# case 3: dictionary of regex-selected encoding mappings
468468
elif isinstance(mapdict, dict) and isinstance(list(mapdict.values())[0], dict):
469469
tmp = None
@@ -474,7 +474,7 @@ def _wrapper(param):
474474
tmp = d
475475
break
476476
if tmp is None:
477-
raise LookupError("Bad parameter for encoding '{}': '{}'".format(ename, p))
477+
raise LookupError(f"Bad parameter for encoding '{ename}': '{p}'")
478478
smapdict = tmp
479479
# case 4: encoding characters translation
480480
else:
@@ -494,7 +494,7 @@ def _wrapper(param):
494494
for k, v in smapdict.items():
495495
smapdict[k] = [x.translate(t) for x in v] if isinstance(v, list) else v.translate(t)
496496
else:
497-
raise LookupError("Bad parameter for encoding '{}': '{}'".format(ename, p))
497+
raise LookupError(f"Bad parameter for encoding '{ename}': '{p}'")
498498
if ignore_case is not None:
499499
cases = ["upper", "lower"]
500500
case_d = cases[any(c in str(list(smapdict.values())) for c in "abcdefghijklmnopqrstuvwxyz")]
@@ -538,7 +538,7 @@ def code(text, errors="strict"):
538538
text = ensure_str(text)
539539
if not decode:
540540
if intype == "bin":
541-
text = "".join("{:0>8}".format(bin(ord(c))[2:]) for c in text)
541+
text = "".join(f"{bin(ord(c))[2:]:0>8}" for c in text)
542542
elif intype == "ord":
543543
text = "".join(str(ord(c)).zfill(3) for c in text)
544544
r = ""
@@ -720,7 +720,7 @@ def list_encodings(*categories):
720720
enc.append(name)
721721
for category in categories:
722722
if category not in CODECS_CATEGORIES:
723-
raise ValueError("Category '%s' does not exist" % category)
723+
raise ValueError(f"Category '{category}' does not exist")
724724
return sorted(list(set(enc)), key=_human_keys)
725725

726726

@@ -755,7 +755,7 @@ def remove(name):
755755
pass
756756
for s in ["En", "De"]:
757757
try:
758-
delattr(builtins, "%s%scodeError" % (name.capitalize(), s))
758+
delattr(builtins, f"{name.capitalize()}{s}codeError")
759759
except AttributeError:
760760
pass
761761
codecs.remove = remove
@@ -801,7 +801,7 @@ def b(s):
801801
return s
802802

803803

804-
def ensure_str(s, encoding='utf-8', errors='strict'):
804+
def ensure_str(s, encoding="utf-8", errors='strict'):
805805
""" Dummy str conversion function. """
806806
if isinstance(s, bytes):
807807
try:
@@ -859,7 +859,7 @@ def handle_error(ename, errors, sep="", repl_char="?", repl_minlen=1, decode=Fal
859859
:param decode: whether we are encoding or decoding
860860
:param item: position item description (for describing the error ; e.g. "group" or "token")
861861
"""
862-
exc = "%s%scodeError" % (exc_name(ename), ["En", "De"][decode])
862+
exc = f"{exc_name(ename)}{['En','De'][decode]}codeError"
863863

864864
def _handle_error(token, position, output="", eename=None):
865865
""" This handles an encoding/decoding error according to the selected handling mode.
@@ -883,7 +883,7 @@ def _handle_error(token, position, output="", eename=None):
883883
elif errors == "ignore":
884884
return ""
885885
else:
886-
raise ValueError("Unsupported error handling '{}'".format(errors))
886+
raise ValueError(f"Unsupported error handling '{errors}'")
887887
return _handle_error
888888

889889

@@ -950,7 +950,7 @@ def lookup(encoding, macro=True):
950950
try:
951951
return CodecMacro(encoding)
952952
except LookupError:
953-
e = LookupError("unknown encoding: %s" % encoding)
953+
e = LookupError(f"unknown encoding: {encoding}")
954954
e.__cause__ = e # stop exception chaining
955955
raise e
956956
codecs.lookup = lookup
@@ -1112,7 +1112,7 @@ def __gen_str_from_re(regex, star_plus_max, repeat_max, yield_max, parsed=False)
11121112
__groups[value[0]] = result
11131113
tokens.append(result)
11141114
else:
1115-
raise NotImplementedError("Unhandled code '{}'".format(code))
1115+
raise NotImplementedError(f"Unhandled code '{code}'")
11161116
if len(tokens) == 0:
11171117
tokens = [""]
11181118
i = 0
@@ -1231,11 +1231,11 @@ def _load_lang_backend(backend=None):
12311231
stopfunc.CLD3_LANGUAGES if _lb == "cld3" else \
12321232
stopfunc.TEXTBLOB_LANGUAGES if _lb == "textblob" else \
12331233
[]):
1234-
n = "lang_%s" % lang
1234+
n = f"lang_{lang}"
12351235
setattr(stopfunc, n, _lang(lang))
12361236
getattr(stopfunc, n).__name__ = getattr(stopfunc, n).__qualname__ = n
12371237
if LANG:
1238-
flng = "lang_%s" % LANG
1238+
flng = f"lang_{LANG}"
12391239
if getattr(stopfunc, flng, None):
12401240
stopfunc.default = getattr(stopfunc, flng)
12411241
stopfunc._reload_lang = _load_lang_backend
@@ -1263,7 +1263,7 @@ def __guess(prev_input, input, stop_func, depth, max_depth, min_depth, encodings
12631263
if not stop and (show or debug) and found not in result:
12641264
s = repr(input)
12651265
s = s[2:-1] if s.startswith("b'") and s.endswith("'") else s
1266-
s = "[+] %s: %s" % (", ".join(found), s)
1266+
s = "[+] {', '.join(found)}: {s}"
12671267
print(s if len(s) <= 80 else s[:77] + "...")
12681268
result[found] = input
12691269
if depth >= max_depth or len(result) > 0 and stop:
@@ -1274,7 +1274,7 @@ def __guess(prev_input, input, stop_func, depth, max_depth, min_depth, encodings
12741274
if len(result) > 0 and stop:
12751275
return
12761276
if debug:
1277-
print("[*] Depth %0{}d/%d: %s".format(len(str(max_depth))) % (depth+1, max_depth, encoding))
1277+
print(f"[*] Depth %0{len(str(max_depth))}d/%d: {encoding}" % (depth+1, max_depth))
12781278
__guess(input, new_input, stop_func, depth+1, max_depth, min_depth, encodings, result, found + (encoding, ),
12791279
stop, show, scoring_heuristic, extended, debug)
12801280

src/codext/base/_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from argparse import ArgumentParser, RawTextHelpFormatter
66
from math import log
77
from string import ascii_lowercase as lower, ascii_uppercase as upper, digits, printable
8+
from sys import stdout
89
from textwrap import wrap as wraptext
910
from types import FunctionType, MethodType
1011

@@ -280,8 +281,11 @@ def _main():
280281
except Exception as err:
281282
print("%sbase%s: invalid input" % (getattr(err, "output", ""), base))
282283
return 1
284+
if args.decode:
285+
stdout.buffer.write(c)
286+
return 0
283287
c = ensure_str(c)
284-
if swap and args.swapcase and not args.decode:
288+
if swap and args.swapcase:
285289
c = codecs.encode(c, "swapcase")
286290
for l in (wraptext(c, args.wrap) if args.wrap > 0 else [c]) if wrap else c.split("\n"):
287291
print(l)

tests/test_generated.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def _template(self):
3232
f1 = getattr(codecs, ["decode", "encode"][k.startswith("enc")])
3333
f2 = getattr(codecs, ["encode", "decode"][k.startswith("enc")])
3434
for ename in m.groups():
35-
#FIXME
3635
if ename == "*":
3736
# ignore mode only
3837
continue

0 commit comments

Comments
 (0)