From 1d0e07c77bab5c48405323e52873c7952dd0a587 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 19 Jan 2022 10:31:46 -0800 Subject: [PATCH] formatting --- docs/readme.md | 33 +++++++++++++++------------------ jc/__init__.py | 33 +++++++++++++++------------------ jc/lib.py | 46 +++++++++++++++++++++++++++------------------- 3 files changed, 57 insertions(+), 55 deletions(-) diff --git a/docs/readme.md b/docs/readme.md index 5bb61520..3a1dc744 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -4,9 +4,11 @@ JC - JSON CLI output utility * kellyjonbrazil@gmail.com -This package serializes the output of many standard unix command line tools to JSON format. +This package serializes the output of many standard unix command line tools +to JSON format. -For documentation on each parser, see the [documentation site](https://kellyjonbrazil.github.io/jc/). +For documentation on each parser, see the +[documentation site](https://kellyjonbrazil.github.io/jc/). CLI Example: @@ -60,32 +62,27 @@ Module Example: >>> import subprocess >>> import jc >>> - >>> cmd_output = subprocess.check_output(['dig', 'example.com'], text=True) + >>> cmd_output = subprocess.check_output(['dig', 'example.com'], + text=True) >>> data = jc.parse('dig', cmd_output) >>> >>> data - [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num': - 1, 'authority_num': 0, 'additional_num': 1, 'opt_pseudosection': {'edns': {'version': 0, 'flags': [], 'udp': - 4096}}, 'question': {'name': 'example.com.', 'class': 'IN', 'type': 'A'}, 'answer': [{'name': 'example.com.', - 'class': 'IN', 'type': 'A', 'ttl': 29658, 'data': '93.184.216.34'}], 'query_time': 52, 'server': - '2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)', 'when': 'Fri Apr 16 16:13:00 PDT 2021', 'rcvd': 56, - 'when_epoch': 1618614780, 'when_epoch_utc': None}] + [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', ...}] -Alternatively, you can bypass the high-level API and call the parser modules directly: +Alternatively, you can bypass the high-level API and call the parser +modules directly: >>> import subprocess >>> import jc.parsers.dig >>> - >>> cmd_output = subprocess.check_output(['dig', 'example.com'], text=True) + >>> cmd_output = subprocess.check_output(['dig', 'example.com'], + text=True) >>> data = jc.parsers.dig.parse(cmd_output) >>> >>> data - [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num': - 1, 'authority_num': 0, 'additional_num': 1, 'opt_pseudosection': {'edns': {'version': 0, 'flags': [], 'udp': - 4096}}, 'question': {'name': 'example.com.', 'class': 'IN', 'type': 'A'}, 'answer': [{'name': 'example.com.', - 'class': 'IN', 'type': 'A', 'ttl': 29658, 'data': '93.184.216.34'}], 'query_time': 52, 'server': - '2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)', 'when': 'Fri Apr 16 16:13:00 PDT 2021', 'rcvd': 56, - 'when_epoch': 1618614780, 'when_epoch_utc': None}] + [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', ...}] -To get a list of all available parser module names, use `jc.parser_mod_list()`. For a list of plugin parser module names only, use `jc.plugin_parser_mod_list()`. +To get a list of all available parser module names, use +`jc.parser_mod_list()`. For a list of plugin parser module names only, +use the `jc.plugin_parser_mod_list()` function. diff --git a/jc/__init__.py b/jc/__init__.py index 509c0979..7c9d7349 100644 --- a/jc/__init__.py +++ b/jc/__init__.py @@ -2,9 +2,11 @@ * kellyjonbrazil@gmail.com -This package serializes the output of many standard unix command line tools to JSON format. +This package serializes the output of many standard unix command line tools +to JSON format. -For documentation on each parser, see the [documentation site](https://kellyjonbrazil.github.io/jc/). +For documentation on each parser, see the +[documentation site](https://kellyjonbrazil.github.io/jc/). CLI Example: @@ -58,33 +60,28 @@ Module Example: >>> import subprocess >>> import jc >>> - >>> cmd_output = subprocess.check_output(['dig', 'example.com'], text=True) + >>> cmd_output = subprocess.check_output(['dig', 'example.com'], + text=True) >>> data = jc.parse('dig', cmd_output) >>> >>> data - [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num': - 1, 'authority_num': 0, 'additional_num': 1, 'opt_pseudosection': {'edns': {'version': 0, 'flags': [], 'udp': - 4096}}, 'question': {'name': 'example.com.', 'class': 'IN', 'type': 'A'}, 'answer': [{'name': 'example.com.', - 'class': 'IN', 'type': 'A', 'ttl': 29658, 'data': '93.184.216.34'}], 'query_time': 52, 'server': - '2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)', 'when': 'Fri Apr 16 16:13:00 PDT 2021', 'rcvd': 56, - 'when_epoch': 1618614780, 'when_epoch_utc': None}] + [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', ...}] -Alternatively, you can bypass the high-level API and call the parser modules directly: +Alternatively, you can bypass the high-level API and call the parser +modules directly: >>> import subprocess >>> import jc.parsers.dig >>> - >>> cmd_output = subprocess.check_output(['dig', 'example.com'], text=True) + >>> cmd_output = subprocess.check_output(['dig', 'example.com'], + text=True) >>> data = jc.parsers.dig.parse(cmd_output) >>> >>> data - [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num': - 1, 'authority_num': 0, 'additional_num': 1, 'opt_pseudosection': {'edns': {'version': 0, 'flags': [], 'udp': - 4096}}, 'question': {'name': 'example.com.', 'class': 'IN', 'type': 'A'}, 'answer': [{'name': 'example.com.', - 'class': 'IN', 'type': 'A', 'ttl': 29658, 'data': '93.184.216.34'}], 'query_time': 52, 'server': - '2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)', 'when': 'Fri Apr 16 16:13:00 PDT 2021', 'rcvd': 56, - 'when_epoch': 1618614780, 'when_epoch_utc': None}] + [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', ...}] -To get a list of all available parser module names, use `jc.parser_mod_list()`. For a list of plugin parser module names only, use `jc.plugin_parser_mod_list()`. +To get a list of all available parser module names, use +`jc.parser_mod_list()`. For a list of plugin parser module names only, +use the `jc.plugin_parser_mod_list()` function. """ from .lib import __version__, parse, parser_mod_list, plugin_parser_mod_list diff --git a/jc/lib.py b/jc/lib.py index 8a8827c7..b337a96e 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -98,8 +98,8 @@ parsers = [ 'zipinfo' ] -# List of custom or override parsers. -# Allow any /jc/jcparsers/*.py +# Create the local_parsers list. This is a list of custom or +# override parsers from /jc/jcparsers/*.py. local_parsers = [] data_dir = appdirs.user_data_dir('jc', 'jc') local_parsers_dir = os.path.join(data_dir, 'jcparsers') @@ -111,6 +111,7 @@ if os.path.isdir(local_parsers_dir): local_parsers.append(plugin_name) if plugin_name not in parsers: parsers.append(plugin_name) + del name def _cliname_to_modname(parser_cli_name): @@ -121,12 +122,13 @@ def _modname_to_cliname(parser_mod_name): """Return module's cli name (underscores converted to dashes)""" return parser_mod_name.replace('_', '-') -def parse(parser_mod_name, data, quiet=False, raw=False, ignore_exceptions=None, **kwargs): +def parse(parser_mod_name, data, + quiet=False, raw=False, ignore_exceptions=None, **kwargs): """ Parse the string data using the supplied parser module. - This function provides a high-level API to simplify parser use. This function will - call built-in parsers and custom plugin parsers. + This function provides a high-level API to simplify parser use. This + function will call built-in parsers and custom plugin parsers. Example: @@ -135,15 +137,17 @@ def parse(parser_mod_name, data, quiet=False, raw=False, ignore_exceptions=None, {'year': 2022, 'month': 'Jan', 'month_num': 1, 'day'...} To get a list of available parser module names, use `parser_mod_list()` - or `plugin_parser_mod_list()`. `plugin_parser_mod_list()` is a subset of `parser_mod_list()`. + or `plugin_parser_mod_list()`. `plugin_parser_mod_list()` is a subset + of `parser_mod_list()`. You can also use the lower-level parser modules directly: >>> import jc.parsers.date >>> jc.parsers.date.parse('Tue Jan 18 10:23:07 PST 2022') - Though, accessing plugin parsers directly is a bit more involved, so the higher-level - API is recommended. Here is how you can access plugin parsers without the API: + Though, accessing plugin parsers directly is a bit more involved, so + this higher-level API is recommended. Here is how you can access plugin + parsers without this API: >>> import os >>> import sys @@ -156,13 +160,17 @@ def parse(parser_mod_name, data, quiet=False, raw=False, ignore_exceptions=None, Parameters: - parser_mod_name: (string) Name of the parser module - data: (string or iterator) Data to parse (string for normal parsers, - iterator of strings for streaming parsers) - raw: (boolean) output preprocessed JSON if True - quiet: (boolean) suppress warning messages if True - ignore_exceptions: (boolean) ignore parsing exceptions if True (streaming - parsers only) + parser_mod_name: (string) Name of the parser module + + data: (string or Data to parse (string for normal + iterator) parsers, iterator of strings for + streaming parsers) + + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + + ignore_exceptions: (boolean) ignore parsing exceptions if True + (streaming parsers only) Returns: @@ -175,13 +183,13 @@ def parse(parser_mod_name, data, quiet=False, raw=False, ignore_exceptions=None, if ignore_exceptions is not None: return jc_parser.parse(data, quiet=quiet, raw=raw, ignore_exceptions=ignore_exceptions, **kwargs) - else: - return jc_parser.parse(data, quiet=quiet, raw=raw, **kwargs) + + return jc_parser.parse(data, quiet=quiet, raw=raw, **kwargs) def parser_mod_list(): - """list of all available parser module names.""" + """Returns a list of all available parser module names.""" return [_cliname_to_modname(p) for p in parsers] def plugin_parser_mod_list(): - """list of plugin parser module names.""" + """Returns a list of plugin parser module names.""" return [_cliname_to_modname(p) for p in local_parsers]