From a9f53ee258e18bc90934e263d8dc96feee84e939 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Thu, 3 Mar 2022 17:50:19 -0800 Subject: [PATCH] optimize streaming parser detection in cli --- jc/cli.py | 4 ++-- jc/lib.py | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/jc/cli.py b/jc/cli.py index 02359ec7..6dd1a147 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -11,7 +11,7 @@ import shlex import subprocess import json from .lib import (__version__, all_parser_info, parsers, - _parser_argument, _get_parser, streaming_parser_mod_list) + _parser_argument, _get_parser, _parser_is_streaming) from . import utils from . import tracebackplus from .exceptions import LibraryNotInstalled, ParseError @@ -502,7 +502,7 @@ def main(): # differentiate between regular and streaming parsers # streaming - if parser_name in streaming_parser_mod_list(): + if _parser_is_streaming(parser): result = parser.parse(sys.stdin, raw=raw, quiet=quiet, diff --git a/jc/lib.py b/jc/lib.py index b8403c96..72b34d30 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -144,6 +144,17 @@ def _get_parser(parser_mod_name): modpath = 'jcparsers.' if parser_cli_name in local_parsers else 'jc.parsers.' return importlib.import_module(f'{modpath}{parser_mod_name}') +def _parser_is_streaming(parser): + """ + Returns True if this is a streaming parser, else False + + parser is a parser module object. + """ + if getattr(parser.info, 'streaming', None): + return True + + return False + def parse( parser_mod_name: str, data: Union[str, Iterable[str]], @@ -237,7 +248,7 @@ def standard_parser_mod_list() -> List[str]: plist = [] for p in parsers: parser = _get_parser(p) - if not getattr(parser.info, 'streaming', None): + if not _parser_is_streaming(parser): plist.append(p) return plist @@ -249,7 +260,7 @@ def streaming_parser_mod_list() -> List[str]: plist = [] for p in parsers: parser = _get_parser(p) - if getattr(parser.info, 'streaming', None): + if _parser_is_streaming(parser): plist.append(p) return plist