1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-21 00:19:42 +02:00

formatting

This commit is contained in:
Kelly Brazil
2022-01-19 10:31:46 -08:00
parent 06a322cf9c
commit 1d0e07c77b
3 changed files with 57 additions and 55 deletions

View File

@ -4,9 +4,11 @@ JC - JSON CLI output utility
* kellyjonbrazil@gmail.com * 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: CLI Example:
@ -60,32 +62,27 @@ Module Example:
>>> import subprocess >>> import subprocess
>>> import jc >>> 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 = jc.parse('dig', cmd_output)
>>> >>>
>>> data >>> data
[{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num': [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', ...}]
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}]
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 subprocess
>>> import jc.parsers.dig >>> 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 = jc.parsers.dig.parse(cmd_output)
>>> >>>
>>> data >>> data
[{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num': [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', ...}]
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}]
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.

View File

@ -2,9 +2,11 @@
* kellyjonbrazil@gmail.com * 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: CLI Example:
@ -58,33 +60,28 @@ Module Example:
>>> import subprocess >>> import subprocess
>>> import jc >>> 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 = jc.parse('dig', cmd_output)
>>> >>>
>>> data >>> data
[{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num': [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', ...}]
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}]
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 subprocess
>>> import jc.parsers.dig >>> 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 = jc.parsers.dig.parse(cmd_output)
>>> >>>
>>> data >>> data
[{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num': [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', ...}]
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}]
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 from .lib import __version__, parse, parser_mod_list, plugin_parser_mod_list

View File

@ -98,8 +98,8 @@ parsers = [
'zipinfo' 'zipinfo'
] ]
# List of custom or override parsers. # Create the local_parsers list. This is a list of custom or
# Allow any <user_data_dir>/jc/jcparsers/*.py # override parsers from <user_data_dir>/jc/jcparsers/*.py.
local_parsers = [] local_parsers = []
data_dir = appdirs.user_data_dir('jc', 'jc') data_dir = appdirs.user_data_dir('jc', 'jc')
local_parsers_dir = os.path.join(data_dir, 'jcparsers') 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) local_parsers.append(plugin_name)
if plugin_name not in parsers: if plugin_name not in parsers:
parsers.append(plugin_name) parsers.append(plugin_name)
del name
def _cliname_to_modname(parser_cli_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 module's cli name (underscores converted to dashes)"""
return parser_mod_name.replace('_', '-') 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. Parse the string data using the supplied parser module.
This function provides a high-level API to simplify parser use. This function will This function provides a high-level API to simplify parser use. This
call built-in parsers and custom plugin parsers. function will call built-in parsers and custom plugin parsers.
Example: 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'...} {'year': 2022, 'month': 'Jan', 'month_num': 1, 'day'...}
To get a list of available parser module names, use `parser_mod_list()` 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: You can also use the lower-level parser modules directly:
>>> import jc.parsers.date >>> import jc.parsers.date
>>> jc.parsers.date.parse('Tue Jan 18 10:23:07 PST 2022') >>> 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 Though, accessing plugin parsers directly is a bit more involved, so
API is recommended. Here is how you can access plugin parsers without the API: this higher-level API is recommended. Here is how you can access plugin
parsers without this API:
>>> import os >>> import os
>>> import sys >>> import sys
@ -156,13 +160,17 @@ def parse(parser_mod_name, data, quiet=False, raw=False, ignore_exceptions=None,
Parameters: Parameters:
parser_mod_name: (string) Name of the parser module 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) data: (string or Data to parse (string for normal
raw: (boolean) output preprocessed JSON if True iterator) parsers, iterator of strings for
quiet: (boolean) suppress warning messages if True streaming parsers)
ignore_exceptions: (boolean) ignore parsing exceptions if True (streaming
parsers only) 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: Returns:
@ -175,13 +183,13 @@ def parse(parser_mod_name, data, quiet=False, raw=False, ignore_exceptions=None,
if ignore_exceptions is not None: if ignore_exceptions is not None:
return jc_parser.parse(data, quiet=quiet, raw=raw, ignore_exceptions=ignore_exceptions, **kwargs) 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(): 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] return [_cliname_to_modname(p) for p in parsers]
def plugin_parser_mod_list(): 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] return [_cliname_to_modname(p) for p in local_parsers]