|
13 | 13 |
|
14 | 14 |
|
15 | 15 | def main(): |
16 | | - descr = """Usage: debase [OPTION]... [FILE] |
17 | | -Base decode multi-layer FILE, or standard input, to standard output. |
| 16 | + descr = """Usage: unbase [OPTION]... [FILE] |
| 17 | +Decode multi-layer base encoded FILE, or standard input, to standard output. |
18 | 18 |
|
19 | 19 | With no FILE, or when FILE is -, read standard input. |
20 | 20 |
|
21 | 21 | Optional arguments: |
| 22 | + -e, --extended also consider generic base codecs while guess-decoding |
22 | 23 | -f, --stop-function set the result chceking function (default: text) |
23 | | - format: printables|text|flag|lang_[bigram]|[regex] |
24 | | - -i, --ignore-generic ignore generic base codecs while guess-decoding |
| 24 | + format: printables|text|flag|lang_[bigram] |
25 | 25 | -M, --max-depth maximum codec search depth (default: 5) |
26 | 26 | -m, --min-depth minimum codec search depth (default: 0) |
27 | | - -s, --do-not-stop do not stop if a valid output is found |
| 27 | + -p, --pattern pattern to be matched while searching |
| 28 | + -s, --show show the decoding chain |
28 | 29 |
|
29 | 30 | --help display this help and exit |
30 | 31 | --verbose show guessing information and steps |
31 | 32 | --version output version information and exit |
32 | 33 |
|
33 | | -Report debase bugs to <https://github.com/dhondta/python-codext/issues/new> |
| 34 | +Report unbase bugs to <https://github.com/dhondta/python-codext/issues/new> |
34 | 35 | Full documentation at: <https://python-codext.readthedocs.io/en/latest/enc/base.html> |
35 | 36 | """ |
36 | 37 | parser = ArgumentParser(description=descr, formatter_class=RawTextHelpFormatter, add_help=False) |
37 | 38 | parser.format_help = MethodType(lambda s: s.description, parser) |
38 | 39 | parser.add_argument("file", nargs="?") |
| 40 | + parser.add_argument("-e", "--extended", action="store_true") |
39 | 41 | parser.add_argument("-f", "--stop-function", default="text") |
40 | | - parser.add_argument("-i", "--ignore-generic", action="store_true") |
41 | | - parser.add_argument("-M", "--max-depth", default=5, type=int) |
42 | | - parser.add_argument("-m", "--min-depth", default=0, type=int) |
43 | | - parser.add_argument("-s", "--do-not-stop", action="store_true") |
| 42 | + parser.add_argument("-M", "--max-depth", type=int, default=10) |
| 43 | + parser.add_argument("-m", "--min-depth", type=int, default=0) |
| 44 | + parser.add_argument("-p", "--pattern") |
| 45 | + parser.add_argument("-s", "--show", action="store_true") |
44 | 46 | parser.add_argument("--help", action="help") |
45 | 47 | parser.add_argument("--version", action="version") |
46 | 48 | parser.add_argument("--verbose", action="store_true") |
47 | 49 | parser.version = "CodExt " + __version__ |
48 | 50 | args = parser.parse_args() |
49 | | - excl = [[], ["base%d-generic" % i for i in range(2, 255)]][args.ignore_generic] |
50 | | - sfunc = getattr(stopfunc, args.stop_function, args.stop_function) |
| 51 | + excl, s = [["base%d-generic" % i for i in range(2, 256)], []][args.extended], args.stop_function |
| 52 | + if re.match(r"lang_[a-z]{2}$", s) and all(re.match(r"lang_[a-z]{2}$", x) is None for x in dir(stopfunc)): |
| 53 | + stopfunc._reload_lang(stopfunc.LANG_BACKEND) |
| 54 | + #TODO: validate args.stop_function |
| 55 | + #TODO: make --stop-function and --pattern mutually exclusive |
| 56 | + sfunc = getattr(stopfunc, s, s) |
51 | 57 | c = _input(args.file) |
52 | 58 | c = c.rstrip("\r\n") if isinstance(c, str) else c.rstrip(b"\r\n") |
53 | | - r = codecs.guess(c, sfunc, args.min_depth, args.max_depth, exclude=excl, codec_categories="base", |
54 | | - stop=not args.do_not_stop, show=True, scoring_heuristic=False, debug=args.verbose) |
55 | | - if not args.do_not_stop: |
56 | | - print("Could not decode :-(" if len(r) == 0 else ensure_str(list(r.items())[0][1])) |
| 59 | + r = codecs.guess(c, sfunc, 0, args.max_depth, exclude=tuple(excl), codec_categories="base", |
| 60 | + stop=False, show=args.verbose, scoring_heuristic=False, debug=args.verbose) |
| 61 | + if len(r) == 0: |
| 62 | + print("Could not decode :-(") |
| 63 | + return 0 |
| 64 | + ans = max(r.items(), key=lambda x: len(x[0])) |
| 65 | + if args.show: |
| 66 | + print(" - ".join(ans[0])) |
| 67 | + print(ensure_str(ans[1])) |
| 68 | + return 0 |
57 | 69 |
|
0 commit comments