mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-17 00:07:37 +02:00
optimize streaming parser detection in cli
This commit is contained in:
@ -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,
|
||||
|
15
jc/lib.py
15
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
|
||||
|
||||
|
Reference in New Issue
Block a user