From e9ab3a6d6679d825d0e48523cec13ff710f0220d Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Fri, 14 Feb 2020 17:34:18 +0300 Subject: [PATCH] CLI --evgen --- doc/news.rst | 3 ++- pyderasn.py | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/doc/news.rst b/doc/news.rst index 7e11078..5665a42 100644 --- a/doc/news.rst +++ b/doc/news.rst @@ -15,7 +15,8 @@ News encoded representation storage * Initial support for so called ``evgen_mode``: event generation mode, where no in-memory objects storing happens, giving ability to process - ASN.1 data without fully parsing it first + ASN.1 data without fully parsing it first. ``python -m pyderasn`` has + ``--evgen`` mode switcher * Initial experimental CER encoding mode, allowing streaming encoding of the data directly to some writeable object * Ability to use mmap-ed memoryviews to skip files loading to memory diff --git a/pyderasn.py b/pyderasn.py index 002f060..fe1d4ab 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -1943,6 +1943,7 @@ def pprint( with_colours=False, with_decode_path=False, decode_path_only=(), + decode_path=(), ): """Pretty print object @@ -1995,7 +1996,7 @@ def pprint( else: for row in _pprint_pps(pp): yield row - return "\n".join(_pprint_pps(obj.pps())) + return "\n".join(_pprint_pps(obj.pps(decode_path))) ######################################################################## @@ -6706,6 +6707,11 @@ def main(): # pragma: no cover action="store_true", help="Allow explicit tag out-of-bound", ) + parser.add_argument( + "--evgen", + action="store_true", + help="Turn on event generation mode", + ) parser.add_argument( "RAWFile", type=argparse.FileType("rb"), @@ -6734,10 +6740,9 @@ def main(): # pragma: no cover } if args.defines_by_path is not None: ctx["defines_by_path"] = obj_by_path(args.defines_by_path) - obj, tail = schema().decode(raw, ctx=ctx) from os import environ - print(pprinter( - obj, + pprinter = partial( + pprinter, oid_maps=oid_maps, with_colours=environ.get("NO_COLOR") is None, with_decode_path=args.print_decode_path, @@ -6745,7 +6750,13 @@ def main(): # pragma: no cover () if args.decode_path_only is None else tuple(args.decode_path_only.split(":")) ), - )) + ) + if args.evgen: + for decode_path, obj, tail in schema().decode_evgen(raw, ctx=ctx): + print(pprinter(obj, decode_path=decode_path)) + else: + obj, tail = schema().decode(raw, ctx=ctx) + print(pprinter(obj)) if tail != b"": print("\nTrailing data: %s" % hexenc(tail)) -- 2.44.0