From cec73d61310591e4890605f61dc5c1bef5808ec4 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 18 Oct 2019 09:57:22 -0700 Subject: [PATCH] linting --- jc/jc.py | 7 +++++-- jc/parsers/ifconfig.py | 7 +++---- jc/parsers/ls.py | 3 ++- jc/parsers/netstat.py | 23 ++++++++++++++--------- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/jc/jc.py b/jc/jc.py index 58551973..cc401823 100755 --- a/jc/jc.py +++ b/jc/jc.py @@ -10,14 +10,16 @@ import jc.parsers.ifconfig import jc.parsers.ls import jc.parsers.netstat + def main(): pretty = False data = sys.stdin.read() if len(sys.argv) < 2: - print(f'\nError: jc\n Must specify parser. (e.g. --ls, --netstat, --ifconfig, etc.)') + print('Error: jc') + print(' Must specify parser. (e.g. --ls, --netstat, --ifconfig, etc.)') print(' Use -p to pretty print') - print(f'\nExample: ls -al | jc --ls -p\n') + print('Example: ls -al | jc --ls -p\n') exit() arg = sys.argv[1] @@ -39,5 +41,6 @@ def main(): else: print(json.dumps(result)) + if __name__ == '__main__': main() diff --git a/jc/parsers/ifconfig.py b/jc/parsers/ifconfig.py index 4bbac6fb..e716ba05 100644 --- a/jc/parsers/ifconfig.py +++ b/jc/parsers/ifconfig.py @@ -13,9 +13,10 @@ $ ifconfig | jc --ifconfig -p from collections import namedtuple from ifconfigparser import IfconfigParser + def parse(data): output = [] - + parsed = IfconfigParser(console_output=data) interfaces = parsed.get_interfaces() @@ -24,7 +25,5 @@ def parse(data): d = interfaces[iface]._asdict() dct = dict(d) output.append(dct) - + return output - - diff --git a/jc/parsers/ls.py b/jc/parsers/ls.py index 78b275bf..4c2a411b 100644 --- a/jc/parsers/ls.py +++ b/jc/parsers/ls.py @@ -85,9 +85,10 @@ $ $ ls -l /usr/bin | jc --ls | jq .[] | jq 'select(.bytes > 50000000)' """ import re + def parse(data): output = [] - + cleandata = data.splitlines() # Delete first line if it starts with 'total' diff --git a/jc/parsers/netstat.py b/jc/parsers/netstat.py index ed8c0ea7..de277e12 100644 --- a/jc/parsers/netstat.py +++ b/jc/parsers/netstat.py @@ -138,6 +138,7 @@ import string output = {} + class state(): section = '' session = '' @@ -147,12 +148,13 @@ class state(): client_tcp_ip6 = [] client_udp_ip4 = [] client_udp_ip6 = [] - + server_tcp_ip4 = [] server_tcp_ip6 = [] server_udp_ip4 = [] server_udp_ip6 = [] + def parse_line(entry): parsed_line = entry.split() output_line = {} @@ -166,7 +168,7 @@ def parse_line(entry): if parsed_line[5][0] not in string.digits: output_line['state'] = parsed_line[5] - + if len(parsed_line) > 6: output_line['pid'] = int(parsed_line[6].split('/')[0]) output_line['program_name'] = parsed_line[6].split('/')[1] @@ -179,6 +181,7 @@ def parse_line(entry): return output_line + def parse(data): cleandata = data.splitlines() @@ -191,13 +194,13 @@ def parse(data): if line.find('Active Internet connections (only servers)') == 0: state.section = 'server' continue - + if line.find('Proto') == 0: continue if line.find('Active UNIX') == 0: break - + if state.section == 'client': if line.find('tcp') == 0: state.session = 'tcp' @@ -225,6 +228,7 @@ def parse(data): else: state.network = 'ipv4' + # client section if state.section == 'client' and state.session == 'tcp' and state.network == 'ipv4': state.client_tcp_ip4.append(parse_line(line)) @@ -237,7 +241,7 @@ def parse(data): if state.section == 'client' and state.session == 'udp' and state.network == 'ipv6': state.client_udp_ip6.append(parse_line(line)) - + # server section if state.section == 'server' and state.session == 'tcp' and state.network == 'ipv4': state.server_tcp_ip4.append(parse_line(line)) @@ -254,6 +258,7 @@ def parse(data): state.network = '' # build dictionary + # client section if state.client_tcp_ip4: if 'client' not in output: output['client'] = {} @@ -281,8 +286,8 @@ def parse(data): if 'udp' not in output['client']: output['client']['udp'] = {} output['client']['udp']['ipv6'] = state.client_udp_ip6 - - + + # server section if state.server_tcp_ip4: if 'server' not in output: output['server'] = {} @@ -303,7 +308,7 @@ def parse(data): if 'udp' not in output['server']: output['server']['udp'] = {} output['server']['udp']['ipv4'] = state.server_udp_ip4 - + if state.server_udp_ip6: if 'server' not in output: output['server'] = {} @@ -311,4 +316,4 @@ def parse(data): output['server']['udp'] = {} output['server']['udp']['ipv6'] = state.server_udp_ip6 - return output \ No newline at end of file + return output