forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
64 lines (56 loc) · 2.72 KB
/
Copy path__init__.py
File metadata and controls
64 lines (56 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: UTF-8 -*-
from argparse import ArgumentParser, RawTextHelpFormatter
from types import MethodType
from .base45 import *
from .base85 import *
from .base91 import *
from .base100 import *
from .base122 import *
from .baseN import *
from ..__common__ import *
from ..__info__ import __version__
def main():
descr = """Usage: unbase [OPTION]... [FILE]
Decode multi-layer base encoded FILE, or standard input, to standard output.
With no FILE, or when FILE is -, read standard input.
Optional arguments:
-E, --extended also consider generic base codecs while guess-decoding
-f, --stop-function set the result chceking function (default: text)
format: printables|text|flag|lang_[bigram]
-M, --max-depth maximum codec search depth (default: 5)
-m, --min-depth minimum codec search depth (default: 0)
-p, --pattern pattern to be matched while searching
-s, --show show the decoding chain
--help display this help and exit
--verbose show guessing information and steps
--version output version information and exit
Report unbase bugs to <https://github.com/dhondta/python-codext/issues/new>
Full documentation at: <https://python-codext.readthedocs.io/en/latest/enc/base.html>
"""
parser = ArgumentParser(description=descr, formatter_class=RawTextHelpFormatter, add_help=False)
parser.format_help = MethodType(lambda s: s.description, parser)
group = parser.add_mutually_exclusive_group()
parser.add_argument("file", nargs="?")
parser.add_argument("-E", "--extended", action="store_true")
group.add_argument("-f", "--stop-function", default="text")
parser.add_argument("-M", "--max-depth", type=int, default=10)
parser.add_argument("-m", "--min-depth", type=int, default=0)
group.add_argument("-p", "--pattern")
parser.add_argument("-s", "--show", action="store_true")
parser.add_argument("--help", action="help")
parser.add_argument("--version", action="version")
parser.add_argument("--verbose", action="store_true")
parser.version = "CodExt " + __version__
args = parser.parse_args()
c, e = _input(args.file), [["base%d-generic" % i for i in range(2, 256)], []][args.extended]
c = c.rstrip("\r\n") if isinstance(c, str) else c.rstrip(b"\r\n")
r = codecs.guess(c, stopfunc._validate(args.stop_function), 0, args.max_depth, "base", tuple(e), stop=False,
show=args.verbose, debug=args.verbose)
if len(r) == 0:
print("Could not decode :-(")
return 0
ans = max(r.items(), key=lambda x: len(x[0]))
if args.show:
print(" - ".join(ans[0]))
print(ensure_str(ans[1]))
return 0