mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-21 00:19:42 +02:00
formatting
This commit is contained in:
46
jc/lib.py
46
jc/lib.py
@ -98,8 +98,8 @@ parsers = [
|
||||
'zipinfo'
|
||||
]
|
||||
|
||||
# List of custom or override parsers.
|
||||
# Allow any <user_data_dir>/jc/jcparsers/*.py
|
||||
# Create the local_parsers list. This is a list of custom or
|
||||
# override parsers from <user_data_dir>/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]
|
||||
|
Reference in New Issue
Block a user