From e16bc7e882a27a1d5a4bd7cf9972208e6997f96f Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sat, 14 Dec 2019 23:15:15 -0800 Subject: [PATCH] add about information to parsers --- jc/cli.py | 71 ++++++++++++++++++++++++++++--------- jc/parsers/arp.py | 1 + jc/parsers/df.py | 1 + jc/parsers/dig.py | 1 + jc/parsers/env.py | 1 + jc/parsers/foo.py | 1 + jc/parsers/free.py | 1 + jc/parsers/fstab.py | 1 + jc/parsers/history.py | 1 + jc/parsers/hosts.py | 1 + jc/parsers/iptables.py | 1 + jc/parsers/jobs.py | 1 + jc/parsers/ls.py | 1 + jc/parsers/lsblk.py | 1 + jc/parsers/lsmod.py | 1 + jc/parsers/lsof.py | 1 + jc/parsers/mount.py | 1 + jc/parsers/netstat.py | 1 + jc/parsers/ps.py | 1 + jc/parsers/route.py | 1 + jc/parsers/ss.py | 1 + jc/parsers/stat.py | 1 + jc/parsers/systemctl.py | 1 + jc/parsers/systemctl_lj.py | 1 + jc/parsers/systemctl_ls.py | 1 + jc/parsers/systemctl_luf.py | 1 + jc/parsers/uname.py | 1 + jc/parsers/uptime.py | 1 + jc/parsers/w.py | 1 + 29 files changed, 82 insertions(+), 17 deletions(-) diff --git a/jc/cli.py b/jc/cli.py index 3373655c..febfade3 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -81,16 +81,42 @@ def ctrlc(signum, frame): def parsers_text(): ptext = '' - for key in parser_map: - if hasattr(parser_map[key], 'info'): - padding = 16 - len(key) + for parser in parser_map: + if hasattr(parser_map[parser], 'info'): + padding = 16 - len(parser) padding_char = ' ' padding_text = padding_char * padding - ptext += ' ' + key + padding_text + parser_map[key].info.description + '\n' + ptext += ' ' + parser + padding_text + parser_map[parser].info.description + '\n' return ptext +def about_jc(): + parser_list = [] + for parser in parser_map: + if hasattr(parser_map[parser], 'info'): + parser_entry = { + 'name': parser_map[parser].__name__.split('.')[-1], + 'description': parser_map[parser].info.description, + 'author': parser_map[parser].info.author, + 'author_email': parser_map[parser].info.author_email, + 'compatible': parser_map[parser].info.compatible, + 'details': parser_map[parser].info.details + } + parser_list.append(parser_entry) + + result = { + 'name': __name__, + 'version': info.version, + 'description': info.description, + 'author': info.author, + 'author_email': info.author_email, + 'parsers': parser_list + } + + return result + + def helptext(message): parsers_string = parsers_text() @@ -102,6 +128,7 @@ def helptext(message): Parsers: {parsers_string} Options: + -a about jc -d debug - show trace messages -p pretty print output -q quiet - suppress warnings @@ -113,14 +140,17 @@ def helptext(message): print(textwrap.dedent(helptext_string), file=sys.stderr) +def json_out(data, pretty=False): + if pretty: + print(json.dumps(data, indent=2)) + else: + print(json.dumps(data)) + + def main(): signal.signal(signal.SIGINT, ctrlc) - if sys.stdin.isatty(): - helptext('missing piped data') - exit() - - data = sys.stdin.read() + about = False debug = False pretty = False quiet = False @@ -139,15 +169,26 @@ def main(): if '-r' in sys.argv: raw = True + if '-a' in sys.argv: + about = True + result = about_jc() + + if sys.stdin.isatty() and not about: + helptext('missing piped data') + exit() + + if not about: + data = sys.stdin.read() + found = False - if debug: + if debug and not about: for arg in sys.argv: if arg in parser_map: result = parser_map[arg].parse(data, raw=raw, quiet=quiet) found = True break - else: + elif not about: for arg in sys.argv: if arg in parser_map: try: @@ -159,15 +200,11 @@ def main(): 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) - if not found: + if not found and not about: helptext('missing or incorrect arguments') exit() - # output resulting dictionary as json - if pretty: - print(json.dumps(result, indent=2)) - else: - print(json.dumps(result)) + json_out(result, pretty=pretty) if __name__ == '__main__': diff --git a/jc/parsers/arp.py b/jc/parsers/arp.py index b8320758..fd351ddc 100644 --- a/jc/parsers/arp.py +++ b/jc/parsers/arp.py @@ -95,6 +95,7 @@ class info(): description = 'arp parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'aix', 'freebsd', 'darwin'] diff --git a/jc/parsers/df.py b/jc/parsers/df.py index e6a0e32d..d8bd70b6 100644 --- a/jc/parsers/df.py +++ b/jc/parsers/df.py @@ -77,6 +77,7 @@ class info(): description = 'df parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin'] diff --git a/jc/parsers/dig.py b/jc/parsers/dig.py index 4bd1c528..7a772afb 100644 --- a/jc/parsers/dig.py +++ b/jc/parsers/dig.py @@ -328,6 +328,7 @@ class info(): description = 'dig parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'aix', 'freebsd', 'darwin'] diff --git a/jc/parsers/env.py b/jc/parsers/env.py index 421acdd4..f519168c 100644 --- a/jc/parsers/env.py +++ b/jc/parsers/env.py @@ -56,6 +56,7 @@ class info(): description = 'env parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] diff --git a/jc/parsers/foo.py b/jc/parsers/foo.py index a0e44b84..e9550f30 100644 --- a/jc/parsers/foo.py +++ b/jc/parsers/foo.py @@ -24,6 +24,7 @@ class info(): description = 'foo parser' author = 'John Doe' author_email = 'johndoe@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] diff --git a/jc/parsers/free.py b/jc/parsers/free.py index c29a96b1..f17f8b16 100644 --- a/jc/parsers/free.py +++ b/jc/parsers/free.py @@ -57,6 +57,7 @@ class info(): description = 'free parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/fstab.py b/jc/parsers/fstab.py index d8936e81..d6f91e03 100644 --- a/jc/parsers/fstab.py +++ b/jc/parsers/fstab.py @@ -74,6 +74,7 @@ class info(): description = '/etc/fstab file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/history.py b/jc/parsers/history.py index b5aea12b..39ab8fdb 100644 --- a/jc/parsers/history.py +++ b/jc/parsers/history.py @@ -48,6 +48,7 @@ class info(): description = 'history parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] diff --git a/jc/parsers/hosts.py b/jc/parsers/hosts.py index 3e5bc093..fef781b3 100644 --- a/jc/parsers/hosts.py +++ b/jc/parsers/hosts.py @@ -65,6 +65,7 @@ class info(): description = '/etc/hosts file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] diff --git a/jc/parsers/iptables.py b/jc/parsers/iptables.py index 96e70a52..870cb24e 100644 --- a/jc/parsers/iptables.py +++ b/jc/parsers/iptables.py @@ -138,6 +138,7 @@ class info(): description = 'iptables parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/jobs.py b/jc/parsers/jobs.py index 2d16f777..311c91b4 100644 --- a/jc/parsers/jobs.py +++ b/jc/parsers/jobs.py @@ -81,6 +81,7 @@ class info(): description = 'jobs parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin', 'cygwin', 'aix', 'freebsd'] diff --git a/jc/parsers/ls.py b/jc/parsers/ls.py index 4190ce71..b1ee12ad 100644 --- a/jc/parsers/ls.py +++ b/jc/parsers/ls.py @@ -148,6 +148,7 @@ class info(): description = 'ls parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin', 'cygwin', 'aix', 'freebsd'] diff --git a/jc/parsers/lsblk.py b/jc/parsers/lsblk.py index 17be124d..89f2439d 100644 --- a/jc/parsers/lsblk.py +++ b/jc/parsers/lsblk.py @@ -220,6 +220,7 @@ class info(): description = 'lsblk parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/lsmod.py b/jc/parsers/lsmod.py index 58340cde..0a93f4f8 100644 --- a/jc/parsers/lsmod.py +++ b/jc/parsers/lsmod.py @@ -111,6 +111,7 @@ class info(): description = 'lsmod parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/lsof.py b/jc/parsers/lsof.py index 223a3a65..e71eb4d4 100644 --- a/jc/parsers/lsof.py +++ b/jc/parsers/lsof.py @@ -101,6 +101,7 @@ class info(): description = 'lsof parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/mount.py b/jc/parsers/mount.py index d85675a0..1cf54348 100644 --- a/jc/parsers/mount.py +++ b/jc/parsers/mount.py @@ -60,6 +60,7 @@ class info(): description = 'mount parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin'] diff --git a/jc/parsers/netstat.py b/jc/parsers/netstat.py index b3f0e3b6..8e6bcf4f 100644 --- a/jc/parsers/netstat.py +++ b/jc/parsers/netstat.py @@ -317,6 +317,7 @@ class info(): description = 'netstat parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/ps.py b/jc/parsers/ps.py index 87f385ac..15006079 100644 --- a/jc/parsers/ps.py +++ b/jc/parsers/ps.py @@ -181,6 +181,7 @@ class info(): description = 'ps parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin', 'cygwin', 'aix', 'freebsd'] diff --git a/jc/parsers/route.py b/jc/parsers/route.py index a5c9ca82..01ef07dc 100644 --- a/jc/parsers/route.py +++ b/jc/parsers/route.py @@ -105,6 +105,7 @@ class info(): description = 'route parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/ss.py b/jc/parsers/ss.py index e3259959..33e8c147 100644 --- a/jc/parsers/ss.py +++ b/jc/parsers/ss.py @@ -255,6 +255,7 @@ class info(): description = 'ss parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/stat.py b/jc/parsers/stat.py index 929bb187..c4e83931 100644 --- a/jc/parsers/stat.py +++ b/jc/parsers/stat.py @@ -107,6 +107,7 @@ class info(): description = 'stat parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/systemctl.py b/jc/parsers/systemctl.py index 6e147c4e..bbed896f 100644 --- a/jc/parsers/systemctl.py +++ b/jc/parsers/systemctl.py @@ -44,6 +44,7 @@ class info(): description = 'systemctl parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/systemctl_lj.py b/jc/parsers/systemctl_lj.py index d0239b0d..c2d6ea2a 100644 --- a/jc/parsers/systemctl_lj.py +++ b/jc/parsers/systemctl_lj.py @@ -63,6 +63,7 @@ class info(): description = 'systemctl list-jobs parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/systemctl_ls.py b/jc/parsers/systemctl_ls.py index bdcf3d7a..ddc34945 100644 --- a/jc/parsers/systemctl_ls.py +++ b/jc/parsers/systemctl_ls.py @@ -38,6 +38,7 @@ class info(): description = 'systemctl list-sockets parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/systemctl_luf.py b/jc/parsers/systemctl_luf.py index 3b9b2da6..889078d1 100644 --- a/jc/parsers/systemctl_luf.py +++ b/jc/parsers/systemctl_luf.py @@ -35,6 +35,7 @@ class info(): description = 'systemctl list-unit-files parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/uname.py b/jc/parsers/uname.py index 1132da08..c6670dba 100644 --- a/jc/parsers/uname.py +++ b/jc/parsers/uname.py @@ -34,6 +34,7 @@ class info(): description = 'uname -a parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] diff --git a/jc/parsers/uptime.py b/jc/parsers/uptime.py index 331c1fc3..6eb41e95 100644 --- a/jc/parsers/uptime.py +++ b/jc/parsers/uptime.py @@ -38,6 +38,7 @@ class info(): description = 'uptime parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin', 'cygwin', 'aix', 'freebsd'] diff --git a/jc/parsers/w.py b/jc/parsers/w.py index 47933319..03ca18cb 100644 --- a/jc/parsers/w.py +++ b/jc/parsers/w.py @@ -87,6 +87,7 @@ class info(): description = 'w parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' + details = None # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'darwin', 'cygwin', 'aix', 'freebsd']