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

add about information to parsers

This commit is contained in:
Kelly Brazil
2019-12-14 23:15:15 -08:00
parent fe9bdd4811
commit e16bc7e882
29 changed files with 82 additions and 17 deletions

View File

@ -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__':

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']

View File

@ -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']