diff --git a/changelog.txt b/changelog.txt index 9805566a..aef8bf7d 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,8 @@ jc changelog +20200402 v1.10.1 +- Code cleanup + 20200402 v1.10.0 - Add color output by default when not piping data to another program - Add -m option for monochrome output diff --git a/jc/cli.py b/jc/cli.py index 900d549f..e77a3050 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -18,7 +18,7 @@ import jc.utils class info(): - version = '1.10.0' + version = '1.10.1' description = 'jc cli output JSON conversion tool' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' diff --git a/jc/parsers/arp.py b/jc/parsers/arp.py index 8c08e861..e4acb6b5 100644 --- a/jc/parsers/arp.py +++ b/jc/parsers/arp.py @@ -91,7 +91,7 @@ import jc.parsers.universal class info(): - version = '1.2' + version = '1.3' description = 'arp command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -156,7 +156,7 @@ def parse(data, raw=False, quiet=False): cleandata = data.splitlines() # remove final Entries row if -v was used - if cleandata[-1].find('Entries:') == 0: + if cleandata[-1].startswith('Entries:'): cleandata.pop(-1) # detect if osx style was used @@ -179,7 +179,7 @@ def parse(data, raw=False, quiet=False): return process(raw_output) # detect if linux style was used - elif cleandata[0].find('Address') == 0: + elif cleandata[0].startswith('Address'): # fix header row to change Flags Mask to flags_mask cleandata[0] = cleandata[0].replace('Flags Mask', 'flags_mask') diff --git a/jc/parsers/crontab.py b/jc/parsers/crontab.py index f656cf40..b64e2281 100644 --- a/jc/parsers/crontab.py +++ b/jc/parsers/crontab.py @@ -132,7 +132,7 @@ import jc.parsers.universal class info(): - version = '1.1' + version = '1.2' description = 'crontab command and file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -227,13 +227,13 @@ def parse(data, raw=False, quiet=False): # Clear any commented lines for i, line in reversed(list(enumerate(cleandata))): - if line.strip().find('#') == 0: + if line.strip().startswith('#'): cleandata.pop(i) # Pop any variable assignment lines cron_var = [] for i, line in reversed(list(enumerate(cleandata))): - if line.find('=') != -1: + if '=' in line: var_line = cleandata.pop(i) var_name = var_line.split('=', maxsplit=1)[0].strip() var_value = var_line.split('=', maxsplit=1)[1].strip() diff --git a/jc/parsers/crontab_u.py b/jc/parsers/crontab_u.py index 1c1090a2..6487cf3e 100644 --- a/jc/parsers/crontab_u.py +++ b/jc/parsers/crontab_u.py @@ -133,7 +133,7 @@ import jc.parsers.universal class info(): - version = '1.0' + version = '1.1' description = 'crontab file parser with user support' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -228,13 +228,13 @@ def parse(data, raw=False, quiet=False): # Clear any commented lines for i, line in reversed(list(enumerate(cleandata))): - if line.strip().find('#') == 0: + if line.strip().startswith('#'): cleandata.pop(i) # Pop any variable assignment lines cron_var = [] for i, line in reversed(list(enumerate(cleandata))): - if line.find('=') != -1: + if '=' in line: var_line = cleandata.pop(i) var_name = var_line.split('=', maxsplit=1)[0].strip() var_value = var_line.split('=', maxsplit=1)[1].strip() diff --git a/jc/parsers/df.py b/jc/parsers/df.py index 817f0a21..a76527c5 100644 --- a/jc/parsers/df.py +++ b/jc/parsers/df.py @@ -73,7 +73,7 @@ import jc.parsers.universal class info(): - version = '1.1' + version = '1.2' description = 'df command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -135,7 +135,7 @@ def process(proc_data): # change any entry for key with '_blocks' in the name to int for k in entry: - if str(k).find('_blocks') != -1: + if '_blocks' in str(k): try: blocks_int = int(entry[k]) entry[k] = blocks_int diff --git a/jc/parsers/dig.py b/jc/parsers/dig.py index d54d6dc8..01fade61 100644 --- a/jc/parsers/dig.py +++ b/jc/parsers/dig.py @@ -324,7 +324,7 @@ import jc.utils class info(): - version = '1.1' + version = '1.2' description = 'dig command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -525,11 +525,12 @@ def parse_answer(answer): 'ttl': answer_ttl, 'data': answer_data} + def parse_axfr(axfr): - #; <<>> DiG 9.11.14-3-Debian <<>> @81.4.108.41 axfr zonetransfer.me +nocookie - #; (1 server found) - #;; global options: +cmd - #zonetransfer.me. 7200 IN A 5.196.105.14 + # ; <<>> DiG 9.11.14-3-Debian <<>> @81.4.108.41 axfr zonetransfer.me +nocookie + # ; (1 server found) + # ;; global options: +cmd + # zonetransfer.me. 7200 IN A 5.196.105.14 axfr = axfr.split(maxsplit=4) axfr_name = axfr[0] axfr_ttl = axfr[1] @@ -575,7 +576,7 @@ def parse(data, raw=False, quiet=False): output_entry = {} for line in cleandata: - if line.startswith('; <<>> ') and line.lower().find(' axfr ') != -1: + if line.startswith('; <<>> ') and ' axfr ' in line.lower(): question = False authority = False answer = False @@ -583,7 +584,7 @@ def parse(data, raw=False, quiet=False): axfr_list = [] continue - if line.find(';') == -1 and axfr: + if ';' not in line and axfr: axfr_list.append(parse_axfr(line)) output_entry.update({'axfr': axfr_list}) continue @@ -620,7 +621,7 @@ def parse(data, raw=False, quiet=False): authority_list = [] continue - if line.find(';') == -1 and authority: + if ';' not in line and authority: authority_list.append(parse_authority(line)) output_entry.update({'authority': authority_list}) continue @@ -633,7 +634,7 @@ def parse(data, raw=False, quiet=False): answer_list = [] continue - if line.find(';') == -1 and answer: + if ';' not in line and answer: answer_list.append(parse_answer(line)) output_entry.update({'answer': answer_list}) continue diff --git a/jc/parsers/fstab.py b/jc/parsers/fstab.py index 430378d0..644200dd 100644 --- a/jc/parsers/fstab.py +++ b/jc/parsers/fstab.py @@ -70,7 +70,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = 'fstab file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -145,7 +145,7 @@ def parse(data, raw=False, quiet=False): for line in cleandata: output_line = {} # ignore commented lines - if line.strip().find('#') == 0: + if line.strip().startswith('#'): continue line_list = line.split(maxsplit=6) diff --git a/jc/parsers/hosts.py b/jc/parsers/hosts.py index 3d3a6776..ba42cf31 100644 --- a/jc/parsers/hosts.py +++ b/jc/parsers/hosts.py @@ -61,7 +61,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = '/etc/hosts file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -126,7 +126,7 @@ def parse(data, raw=False, quiet=False): for line in cleandata: output_line = {} # ignore commented lines - if line.strip().find('#') == 0: + if line.strip().startswith('#'): continue line_list = line.split(maxsplit=1) @@ -136,7 +136,7 @@ def parse(data, raw=False, quiet=False): comment_found = False for i, item in enumerate(hosts_list): - if item.find('#') != -1: + if '#' in item: comment_found = True comment_item = i break diff --git a/jc/parsers/ifconfig.py b/jc/parsers/ifconfig.py index 7c4df2ed..60515a76 100644 --- a/jc/parsers/ifconfig.py +++ b/jc/parsers/ifconfig.py @@ -146,7 +146,7 @@ from ifconfigparser import IfconfigParser class info(): - version = '1.5' + version = '1.6' description = 'ifconfig command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -220,7 +220,7 @@ def process(proc_data): # convert OSX-style subnet mask to dotted quad if 'ipv4_mask' in entry: try: - if entry['ipv4_mask'].find('0x') == 0: + if entry['ipv4_mask'].startswith('0x'): new_mask = entry['ipv4_mask'] new_mask = new_mask.lstrip('0x') new_mask = '.'.join(str(int(i, 16)) for i in [new_mask[i:i + 2] for i in range(0, len(new_mask), 2)]) diff --git a/jc/parsers/iptables.py b/jc/parsers/iptables.py index 9a75fd78..b4ecd853 100644 --- a/jc/parsers/iptables.py +++ b/jc/parsers/iptables.py @@ -134,7 +134,7 @@ import jc.utils class info(): - version = '1.1' + version = '1.2' description = 'iptables command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -247,7 +247,7 @@ def parse(data, raw=False, quiet=False): for line in cleandata: - if line.find('Chain') == 0: + if line.startswith('Chain'): raw_output.append(chain) chain = {} headers = [] @@ -259,7 +259,7 @@ def parse(data, raw=False, quiet=False): continue - elif line.find('target') == 0 or line.find('pkts') == 1 or line.find('num') == 0: + elif line.startswith('target') or line.find('pkts') == 1 or line.startswith('num'): headers = [] headers = [h for h in ' '.join(line.lower().strip().split()).split() if h] headers.append("options") diff --git a/jc/parsers/jobs.py b/jc/parsers/jobs.py index c5c5f0b8..41ef192d 100644 --- a/jc/parsers/jobs.py +++ b/jc/parsers/jobs.py @@ -77,7 +77,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = 'jobs command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -176,11 +176,11 @@ def parse(data, raw=False, quiet=False): parsed_line.insert(0, job_number) # check for + or - in first field - if parsed_line[0].find('+') != -1: + if '+' in parsed_line[0]: job_history = 'current' parsed_line[0] = parsed_line[0].rstrip('+') - if parsed_line[0].find('-') != -1: + if '-' in parsed_line[0]: job_history = 'previous' parsed_line[0] = parsed_line[0].rstrip('-') diff --git a/jc/parsers/lsof.py b/jc/parsers/lsof.py index 6557ad9f..356dbaba 100644 --- a/jc/parsers/lsof.py +++ b/jc/parsers/lsof.py @@ -97,7 +97,7 @@ import jc.parsers.universal class info(): - version = '1.0' + version = '1.1' description = 'lsof command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -180,47 +180,6 @@ def parse(data, raw=False, quiet=False): raw_output = jc.parsers.universal.sparse_table_parse(cleandata) - ''' - # find column value of last character of each header - header_text = cleandata.pop(0).lower() - - # clean up 'size/off' header - # even though forward slash in a key is valid json, it can make things difficult - header_row = header_text.replace('/', '_') - - headers = header_row.split() - - header_spec = [] - for i, h in enumerate(headers): - # header tuple is (index, header_name, col) - header_spec.append((i, h, header_row.find(h) + len(h))) - - # parse lines - for entry in cleandata: - output_line = {} - - # normalize data by inserting Null for missing data - temp_line = entry.split(maxsplit=len(headers) - 1) - - for spec in header_spec: - - index = spec[0] - header_name = spec[1] - col = spec[2] - 1 # subtract one since column starts at 0 instead of 1 - - if header_name == 'command' or header_name == 'name': - continue - if entry[col] in string.whitespace: - temp_line.insert(index, None) - - name = ' '.join(temp_line[9:]) - fixed_line = temp_line[0:9] - fixed_line.append(name) - - output_line = dict(zip(headers, fixed_line)) - raw_output.append(output_line) - ''' - if raw: return raw_output else: diff --git a/jc/parsers/mount.py b/jc/parsers/mount.py index 0fcdb690..dfb8a7d0 100644 --- a/jc/parsers/mount.py +++ b/jc/parsers/mount.py @@ -56,7 +56,7 @@ import jc.utils class info(): - version = '1.1' + version = '1.2' description = 'mount command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -165,7 +165,7 @@ def parse(data, raw=False, quiet=False): if cleandata: # check for OSX output - if cleandata[0].find(' type ') == -1: + if ' type ' not in cleandata[0]: raw_output = osx_parse(cleandata) else: diff --git a/jc/parsers/netstat.py b/jc/parsers/netstat.py index a31531c5..6a90bde3 100644 --- a/jc/parsers/netstat.py +++ b/jc/parsers/netstat.py @@ -313,7 +313,7 @@ import jc.utils class info(): - version = '1.2' + version = '1.3' description = 'netstat command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -498,14 +498,14 @@ def parse_post(raw_data): if 'proto' in entry and 'kind' in entry: if entry['kind'] == 'network': - if entry['proto'].find('tcp') != -1: + if 'tcp' in entry['proto']: entry['transport_protocol'] = 'tcp' - elif entry['proto'].find('udp') != -1: + elif 'udp' in entry['proto']: entry['transport_protocol'] = 'udp' else: entry['transport_protocol'] = None - if entry['proto'].find('6') != -1: + if '6' in entry['proto']: entry['network_protocol'] = 'ipv6' else: entry['network_protocol'] = 'ipv4' @@ -542,19 +542,19 @@ def parse(data, raw=False, quiet=False): for line in cleandata: - if line.find('Active Internet') == 0: + if line.startswith('Active Internet'): network_list = [] network = True socket = False continue - if line.find('Active UNIX') == 0: + if line.startswith('Active UNIX'): socket_list = [] network = False socket = True continue - if line.find('Proto') == 0: + if line.startswith('Proto'): header_text = normalize_headers(line) headers = header_text.split() continue diff --git a/jc/parsers/pip_list.py b/jc/parsers/pip_list.py index 5bcd5160..ba10d360 100644 --- a/jc/parsers/pip_list.py +++ b/jc/parsers/pip_list.py @@ -32,7 +32,7 @@ import jc.parsers.universal class info(): - version = '1.0' + version = '1.1' description = 'pip list command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -94,7 +94,7 @@ def parse(data, raw=False, quiet=False): cleandata = list(filter(None, linedata)) # detect legacy output type - if cleandata[0].find(' (') != -1: + if ' (' in cleandata[0]: for row in cleandata: raw_output.append({'package': row.split(' (')[0], 'version': row.split(' (')[1].rstrip(')')}) @@ -103,7 +103,7 @@ def parse(data, raw=False, quiet=False): else: # clear separator line for i, line in reversed(list(enumerate(cleandata))): - if line.find('---') != -1: + if '---' in line: cleandata.pop(i) cleandata[0] = cleandata[0].lower() diff --git a/jc/parsers/stat.py b/jc/parsers/stat.py index ecf89f48..7961b592 100644 --- a/jc/parsers/stat.py +++ b/jc/parsers/stat.py @@ -104,7 +104,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = 'stat command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -208,7 +208,7 @@ def parse(data, raw=False, quiet=False): output_line['file'] = line_list[1] # populate link_to field if -> found - if output_line['file'].find(' -> ') != -1: + if ' -> ' in output_line['file']: filename = output_line['file'].split(' -> ')[0].strip('\u2018').rstrip('\u2019') link = output_line['file'].split(' -> ')[1].strip('\u2018').rstrip('\u2019') output_line['file'] = filename @@ -229,7 +229,7 @@ def parse(data, raw=False, quiet=False): continue # line #3 - if line.find('Device:') == 0: + if line.startswith('Device:'): line_list = line.split() output_line['device'] = line_list[1] output_line['inode'] = line_list[3] @@ -237,7 +237,7 @@ def parse(data, raw=False, quiet=False): continue # line #4 - if line.find('Access: (') == 0: + if line.startswith('Access: ('): line = line.replace('(', ' ').replace(')', ' ').replace('/', ' ') line_list = line.split() output_line['access'] = line_list[1] @@ -249,19 +249,19 @@ def parse(data, raw=False, quiet=False): continue # line #5 - if line.find('Access: 2') == 0: + if line.startswith('Access: 2'): line_list = line.split(maxsplit=1) output_line['access_time'] = line_list[1] continue # line #6 - if line.find('Modify:') == 0: + if line.startswith('Modify:'): line_list = line.split(maxsplit=1) output_line['modify_time'] = line_list[1] continue # line #7 - if line.find('Change:') == 0: + if line.startswith('Change:'): line_list = line.split(maxsplit=1) output_line['change_time'] = line_list[1] continue diff --git a/jc/parsers/systemctl.py b/jc/parsers/systemctl.py index d70fe63f..3f6f7074 100644 --- a/jc/parsers/systemctl.py +++ b/jc/parsers/systemctl.py @@ -40,7 +40,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = 'systemctl command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -110,7 +110,7 @@ def parse(data, raw=False, quiet=False): raw_output = [] for entry in cleandata[1:]: - if entry.find('LOAD = ') != -1: + if 'LOAD = ' in entry: break else: diff --git a/jc/parsers/systemctl_lj.py b/jc/parsers/systemctl_lj.py index 0ca69dbf..ee3d0ba9 100644 --- a/jc/parsers/systemctl_lj.py +++ b/jc/parsers/systemctl_lj.py @@ -59,7 +59,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = 'systemctl list-jobs command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -137,7 +137,7 @@ def parse(data, raw=False, quiet=False): raw_output = [] for entry in cleandata[1:]: - if entry.find('No jobs running.') != -1 or entry.find('jobs listed.') != -1: + if 'No jobs running.' in entry or 'jobs listed.' in entry: break else: diff --git a/jc/parsers/systemctl_ls.py b/jc/parsers/systemctl_ls.py index bbc2c27d..a712460f 100644 --- a/jc/parsers/systemctl_ls.py +++ b/jc/parsers/systemctl_ls.py @@ -34,7 +34,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = 'systemctl list-sockets command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -102,7 +102,7 @@ def parse(data, raw=False, quiet=False): raw_output = [] for entry in cleandata[1:]: - if entry.find('sockets listed.') != -1: + if 'sockets listed.' in entry: break else: diff --git a/jc/parsers/systemctl_luf.py b/jc/parsers/systemctl_luf.py index a049d351..6876b04c 100644 --- a/jc/parsers/systemctl_luf.py +++ b/jc/parsers/systemctl_luf.py @@ -31,7 +31,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = 'systemctl list-unit-files command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -99,7 +99,7 @@ def parse(data, raw=False, quiet=False): raw_output = [] for entry in cleandata[1:]: - if entry.find('unit files listed.') != -1: + if 'unit files listed.' in entry: break else: diff --git a/setup.py b/setup.py index f07ca5e5..62a94431 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r') as f: setuptools.setup( name='jc', - version='1.10.0', + version='1.10.1', author='Kelly Brazil', author_email='kellyjonbrazil@gmail.com', description='This tool serializes the output of popular command line tools and filetypes to structured JSON output.',