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

load parser modules 'just in time' so we don't need to load all modules at startup

This commit is contained in:
Kelly Brazil
2020-02-05 10:55:08 -08:00
parent ab06989a18
commit a7b7bdd467

182
jc/cli.py
View File

@ -3,88 +3,72 @@
JC cli module JC cli module
""" """
import sys import sys
import importlib
import textwrap import textwrap
import signal import signal
import json import json
import jc.utils import jc.utils
import jc.parsers.arp
import jc.parsers.crontab parsers = [
import jc.parsers.crontab_u 'arp',
import jc.parsers.df 'crontab',
import jc.parsers.dig 'crontab-u',
import jc.parsers.du 'df',
import jc.parsers.env 'dig',
import jc.parsers.free 'du',
import jc.parsers.fstab 'env',
import jc.parsers.history 'free',
import jc.parsers.hosts 'fstab',
import jc.parsers.id 'history',
import jc.parsers.ifconfig 'hosts',
import jc.parsers.ini 'id',
import jc.parsers.iptables 'ifconfig',
import jc.parsers.jobs 'ini',
import jc.parsers.ls 'iptables',
import jc.parsers.lsblk 'jobs',
import jc.parsers.lsmod 'ls',
import jc.parsers.lsof 'lsblk',
import jc.parsers.mount 'lsmod',
import jc.parsers.netstat 'lsof',
import jc.parsers.pip_list 'mount',
import jc.parsers.pip_show 'netstat',
import jc.parsers.ps 'pip-list',
import jc.parsers.route 'pip-show',
import jc.parsers.ss 'ps',
import jc.parsers.stat 'route',
import jc.parsers.systemctl 'ss',
import jc.parsers.systemctl_lj 'stat',
import jc.parsers.systemctl_ls 'systemctl',
import jc.parsers.systemctl_luf 'systemctl-lj',
import jc.parsers.uname 'systemctl-ls',
import jc.parsers.uptime 'systemctl-luf',
import jc.parsers.w 'uname',
import jc.parsers.xml 'uptime',
import jc.parsers.yaml 'w',
'xml',
'yaml'
]
parser_map = { def parser_shortname(parser_argument):
'--arp': jc.parsers.arp, # short name of the parser with dashes and no -- prefix
'--crontab': jc.parsers.crontab, return parser_argument[2:]
'--crontab-u': jc.parsers.crontab_u,
'--df': jc.parsers.df,
'--dig': jc.parsers.dig, def parser_argument(parser):
'--du': jc.parsers.du, # short name of the parser with dashes and with -- prefix
'--env': jc.parsers.env, return f'--{parser}'
'--free': jc.parsers.free,
'--fstab': jc.parsers.fstab,
'--history': jc.parsers.history, def parser_mod_shortname(parser):
'--hosts': jc.parsers.hosts, # short name of the parser's module name (no -- prefix and dashes converted to underscores)
'--id': jc.parsers.id, return parser.replace('--', '').replace('-', '_')
'--ifconfig': jc.parsers.ifconfig,
'--ini': jc.parsers.ini,
'--iptables': jc.parsers.iptables, def parser_module(parser):
'--jobs': jc.parsers.jobs, # import the module just in time and present the module object
'--ls': jc.parsers.ls, importlib.import_module('jc.parsers.' + parser_mod_shortname(parser))
'--lsblk': jc.parsers.lsblk, return getattr(jc.parsers, parser_mod_shortname(parser))
'--lsmod': jc.parsers.lsmod,
'--lsof': jc.parsers.lsof,
'--mount': jc.parsers.mount,
'--netstat': jc.parsers.netstat,
'--pip-list': jc.parsers.pip_list,
'--pip-show': jc.parsers.pip_show,
'--ps': jc.parsers.ps,
'--route': jc.parsers.route,
'--ss': jc.parsers.ss,
'--stat': jc.parsers.stat,
'--systemctl': jc.parsers.systemctl,
'--systemctl-lj': jc.parsers.systemctl_lj,
'--systemctl-ls': jc.parsers.systemctl_ls,
'--systemctl-luf': jc.parsers.systemctl_luf,
'--uname': jc.parsers.uname,
'--uptime': jc.parsers.uptime,
'--w': jc.parsers.w,
'--xml': jc.parsers.xml,
'--yaml': jc.parsers.yaml
}
class info(): class info():
@ -103,27 +87,36 @@ def ctrlc(signum, frame):
def parsers_text(): def parsers_text():
ptext = '' ptext = ''
for parser in parser_map: for parser in parsers:
if hasattr(parser_map[parser], 'info'): parser_arg = parser_argument(parser)
padding = 16 - len(parser) parser_mod = parser_module(parser)
if hasattr(parser_mod, 'info'):
parser_desc = getattr(parser_mod.info, 'description')
padding = 16 - len(parser_arg)
padding_char = ' ' padding_char = ' '
padding_text = padding_char * padding padding_text = padding_char * padding
ptext += ' ' + parser + padding_text + parser_map[parser].info.description + '\n' ptext += ' ' + parser_arg + padding_text + parser_desc + '\n'
return ptext return ptext
def about_jc(): def about_jc():
parser_list = [] parser_list = []
for parser in parser_map:
if hasattr(parser_map[parser], 'info'): for parser in parsers:
parser_mod = parser_module(parser)
if hasattr(parser_mod, 'info'):
info_dict = {} info_dict = {}
info_dict['name'] = parser_map[parser].__name__.split('.')[-1] info_dict['name'] = getattr(parser_mod, '__name__').split('.')[-1]
info_dict['argument'] = parser info_dict['argument'] = parser_argument(parser)
parser_entry = vars(parser_map[parser].info) parser_entry = vars(getattr(parser_mod, 'info'))
for k, v in parser_entry.items(): for k, v in parser_entry.items():
if not k.startswith('__'): if not k.startswith('__'):
info_dict[k] = v info_dict[k] = v
parser_list.append(info_dict) parser_list.append(info_dict)
return { return {
@ -202,19 +195,28 @@ def main():
if debug: if debug:
for arg in sys.argv: for arg in sys.argv:
if arg in parser_map: parser_name = parser_shortname(arg)
result = parser_map[arg].parse(data, raw=raw, quiet=quiet)
if parser_name in parsers:
# load parser module just in time so we don't need to load all modules
parser_mod = parser_module(arg)
parser = getattr(parser_mod, 'parse')
result = parser(data, raw=raw, quiet=quiet)
found = True found = True
break break
else: else:
for arg in sys.argv: for arg in sys.argv:
if arg in parser_map: parser_name = parser_shortname(arg)
if parser_name in parsers:
# load parser module just in time so we don't need to load all modules
parser_mod = parser_module(arg)
try: try:
result = parser_map[arg].parse(data, raw=raw, quiet=quiet) parser = getattr(parser_mod, 'parse')
result = parser(data, raw=raw, quiet=quiet)
found = True found = True
break break
except: except:
parser_name = parser_map[arg].__name__.split('.')[-1]
jc.utils.error_message(f'{parser_name} parser could not parse the input data. Did you use the correct parser?\n For details use the -d option.') jc.utils.error_message(f'{parser_name} parser could not parse the input data. Did you use the correct parser?\n For details use the -d option.')
exit(1) exit(1)