From 43ed09ce5ba53261dfd7ef50a2e187cb524c416c Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 18 Oct 2019 13:38:11 -0700 Subject: [PATCH] add route parser --- jc/jc.py | 3 +++ jc/parsers/route.py | 21 +++++++++++++++++++++ setup.py | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 jc/parsers/route.py diff --git a/jc/jc.py b/jc/jc.py index 5f981b7e..182fac45 100755 --- a/jc/jc.py +++ b/jc/jc.py @@ -10,6 +10,7 @@ import jc.parsers.ifconfig import jc.parsers.ls import jc.parsers.netstat import jc.parsers.ps +import jc.parsers.route def main(): @@ -37,6 +38,8 @@ def main(): result = jc.parsers.netstat.parse(data) elif arg == '--ps': result = jc.parsers.ps.parse(data) + elif arg == '--route': + result = jc.parsers.route.parse(data) # output resulting dictionary as json if pretty: diff --git a/jc/parsers/route.py b/jc/parsers/route.py new file mode 100644 index 00000000..ca37fbe2 --- /dev/null +++ b/jc/parsers/route.py @@ -0,0 +1,21 @@ +"""jc - JSON CLI output utility route Parser + +Usage: + specify --route as the first argument if the piped input is coming from route + + +Example: + + +""" + + +def parse(data): + + # code adapted from Conor Heine at: + # https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501 + + cleandata = data.splitlines() + headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h] + raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:]) + return [dict(zip(headers, r)) for r in raw_data] diff --git a/setup.py b/setup.py index b389bf5b..530a3522 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r') as f: setuptools.setup( name='jc', - version='0.5.1', + version='0.5.2', author='Kelly Brazil', author_email='kellyjonbrazil@gmail.com', description='This tool serializes the output of popular command line tools to structured JSON output.',