Skip to content

Commit a175934

Browse files
committed
Fixed failing refactored code
1 parent ef03c0a commit a175934

24 files changed

Lines changed: 79 additions & 39 deletions

codext/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.0
1+
1.2.1

codext/__common__.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
__all__ = ["add", "b", "clear", "codecs", "ensure_str", "re", "register",
2222
"remove", "reset", "s2i", "PY3"]
23+
CODECS_REGISTRY = None
2324
PY3 = sys.version[0] == "3"
25+
__codecs_registry = []
2426

2527

2628
isb = lambda s: isinstance(s, binary_type)
@@ -163,18 +165,16 @@ def reset():
163165
"""
164166
Reset codext's local registry of search functions.
165167
"""
168+
global CODECS_REGISTRY, __codecs_registry
166169
clear()
167-
tmp = os.getcwd()
168-
wd = os.path.dirname(__file__)
169-
for root, _, files in os.walk(wd):
170-
if root.split(os.sep)[-1].startswith("_"):
171-
continue
172-
for f in files:
173-
if not f.endswith(".py") or f.startswith("_"):
174-
continue
175-
os.chdir(root)
176-
reload(import_module(f[:-3]))
177-
os.chdir(tmp)
170+
for pkg in ["base", "crypto", "languages", "others", "stegano"]:
171+
reload(import_module("codext." + pkg))
172+
# backup codext's registry
173+
if CODECS_REGISTRY is None:
174+
CODECS_REGISTRY = __codecs_registry[:]
175+
# restore codext's registry
176+
else:
177+
__codecs_registry = CODECS_REGISTRY[:]
178178
codecs.reset = reset
179179

180180

codext/__init__.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,43 @@
1919
reset()
2020

2121

22+
def __stdin_pipe():
23+
""" Stdin pipe read function. """
24+
try:
25+
with open(0, 'rb') as f:
26+
for l in f:
27+
yield l
28+
except TypeError:
29+
import sys
30+
for l in sys.stdin:
31+
yield l
32+
33+
2234
def main():
2335
import argparse, os
2436
parser = argparse.ArgumentParser()
25-
group = parser.add_mutually_exclusive_group(required=True)
26-
group.add_argument("-d", "--decode")
27-
group.add_argument("-e", "--encode")
37+
parser.add_argument("encoding")
38+
parser.add_argument("-d", "--decode", action="store_true")
39+
parser.add_argument("-e", "--errors", default="strict",
40+
choices=["ignore", "replace", "strict"],
41+
help="ignore|replace|strict")
2842
parser.add_argument("-i", "--input-file", dest="infile")
2943
parser.add_argument("-o", "--output-file", dest="outfile")
3044
args = parser.parse_args()
45+
# handle input file or stdin
3146
if os.path.isfile(args.infile):
32-
with codecs.open(f, encoding=args.decode) as fin:
33-
c = fin.read()
47+
with open(args.infile) as f:
48+
c = f.read()
3449
else:
35-
pass #FIXME: take from stdin
50+
c = ""
51+
for line in __stdin_pipe():
52+
c += codecs.encode(line, errors=args.errors)
53+
# encode or decode
54+
c = getattr(codext, ["encode", "decode"][args.decode])\
55+
(c, args.encoding, args.errors)
56+
# hanbdle output file or stdout
3657
if args.outfile is None:
3758
print(c)
3859
else:
39-
with open(args.outfile, 'w') as fout:
40-
fout.write(c)
60+
with open(args.outfile, 'w') as f:
61+
f.write(c)

codext/base/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: UTF-8 -*-
2+
from .ascii85 import *
3+
from .base85 import *
4+
from .base100 import *
5+
from .base122 import *
6+
from .baseN import *

codext/base/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from string import printable
88
from types import FunctionType
99

10-
from codext.__common__ import *
10+
from ..__common__ import *
1111

1212

1313
# generic base en/decoding functions

codext/base/_base2n.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"""
55
from math import ceil, log
66

7-
from _base import base, _get_charset, BaseError
8-
from codext.__common__ import *
7+
from ..__common__ import *
8+
from ._base import base, _get_charset, BaseError
99

1010

1111
# base en/decoding functions for N a power of 2

codext/base/ascii85.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212
import base64
1313

14-
from codext.__common__ import *
14+
from ..__common__ import *
1515

1616

1717
if PY3:

codext/base/base100.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- decodes file content to str (read)
1111
- encodes file content from str to bytes (write)
1212
"""
13-
from codext.__common__ import *
13+
from ..__common__ import *
1414

1515

1616
if PY3:

codext/base/base122.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- decodes file content to str (read)
88
- encodes file content from str to bytes (write)
99
"""
10-
from codext.__common__ import *
10+
from ..__common__ import *
1111

1212

1313
def base122_encode(input, errors="strict"):

codext/base/base85.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212
import base64
1313

14-
from codext.__common__ import *
14+
from ..__common__ import *
1515

1616

1717
if PY3:

0 commit comments

Comments
 (0)