1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

add parser info functions. move _get_parser functionality from cli to lib

This commit is contained in:
Kelly Brazil
2022-01-25 10:46:58 -08:00
parent 08f818aa42
commit 1a05f1c575
3 changed files with 64 additions and 54 deletions

View File

@ -98,6 +98,14 @@ parsers = [
'zipinfo'
]
def _cliname_to_modname(parser_cli_name):
"""Return real module name (dashes converted to underscores)"""
return parser_cli_name.replace('--', '').replace('-', '_')
def _modname_to_cliname(parser_mod_name):
"""Return module's cli name (underscores converted to dashes)"""
return parser_mod_name.replace('_', '-')
# Create the local_parsers list. This is a list of custom or
# override parsers from <user_data_dir>/jc/jcparsers/*.py.
# Once this list is created, extend the parsers list with it.
@ -109,25 +117,24 @@ if os.path.isdir(local_parsers_dir):
for name in os.listdir(local_parsers_dir):
if re.match(r'\w+\.py$', name) and os.path.isfile(os.path.join(local_parsers_dir, name)):
plugin_name = name[0:-3]
local_parsers.append(plugin_name)
local_parsers.append(_modname_to_cliname(plugin_name))
if plugin_name not in parsers:
parsers.append(plugin_name)
parsers.append(_modname_to_cliname(plugin_name))
try:
del name
except Exception:
pass
def _cliname_to_modname(parser_cli_name):
"""Return real module name (dashes converted to underscores)"""
return parser_cli_name.replace('-', '_')
def _modname_to_cliname(parser_mod_name):
"""Return module's cli name (underscores converted to dashes)"""
return parser_mod_name.replace('_', '-')
def _parser_argument(parser_mod_name):
"""Return short name of the parser with dashes and with -- prefix"""
parser = _modname_to_cliname(parser_mod_name)
return f'--{parser}'
def _get_parser(parser_mod_name):
"""Return the parser module object"""
# ensure parser_mod_name is a true module name and not a cli name
parser_mod_name = _cliname_to_modname(parser_mod_name)
parser_cli_name = _modname_to_cliname(parser_mod_name)
modpath = 'jcparsers.' if parser_cli_name in local_parsers else 'jc.parsers.'
return importlib.import_module(f'{modpath}{parser_mod_name}')
@ -170,7 +177,10 @@ def parse(parser_mod_name, data,
Parameters:
parser_mod_name: (string) name of the parser module
parser_mod_name: (string) name of the parser module. This
function will accept module_name,
cli-name, and --argument-name
variants of the module name.
data: (string or data to parse (string for normal
iterator) parsers, iterator of strings for
@ -207,6 +217,38 @@ def plugin_parser_mod_list():
"""
return [_cliname_to_modname(p) for p in local_parsers]
def parser_info(parser_mod_name):
"""
Returns a dictionary that includes the module metadata.
This function will accept module_name, cli-name, and --argument-name
variants of the module name string.
"""
# ensure parser_mod_name is a true module name and not a cli name
parser_mod_name = _cliname_to_modname(parser_mod_name)
parser_mod = _get_parser(parser_mod_name)
if hasattr(parser_mod, 'info'):
info_dict = {}
info_dict['name'] = parser_mod_name
info_dict['argument'] = _parser_argument(parser_mod_name)
parser_entry = vars(parser_mod.info)
for k, v in parser_entry.items():
if not k.startswith('__'):
info_dict[k] = v
return info_dict
def all_parser_info():
return [parser_info(_cliname_to_modname(p)) for p in parsers]
def get_help(parser_mod_name):
"""Show help screen for the selected parser."""
"""
Show help screen for the selected parser.
This function will accept module_name, cli-name, and --argument-name
variants of the module name string.
"""
help(_get_parser(parser_mod_name))