From a9294f32a00709737d0b82ff434a311e75ad0bed Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 18 Oct 2019 18:40:56 -0700 Subject: [PATCH] ls fixes --- changelog.txt | 7 ++---- jc/parsers/ls.py | 58 ++++++++++++++++++++++++------------------------ setup.py | 2 +- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/changelog.txt b/changelog.txt index b37f2e71..d929c5e5 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,13 +1,10 @@ jc changelog -2019xxxx v0.5.0 +20191018 v0.5.4 - Fix netstat -p parsing for Ubuntu - Add ps parser - Add route parser -+ Change some ifconfig fields to integers -+ Use maxsplit option in split in ls.py line 109... otherwise filenames with multiple spaces - between words can be incorrectly represented with the .join operation -+ Use list(filter(None, cleandata)) or list comprehension to clean any blank entries in ls.py line 98 +- ls parser fixes 20191017 v0.2.0 - ifconfig, ls, and netstat support diff --git a/jc/parsers/ls.py b/jc/parsers/ls.py index 4c2a411b..ad985a2f 100644 --- a/jc/parsers/ls.py +++ b/jc/parsers/ls.py @@ -89,43 +89,43 @@ import re def parse(data): output = [] - cleandata = data.splitlines() + linedata = data.splitlines() # Delete first line if it starts with 'total' - if cleandata[0].find('total') == 0: - cleandata.pop(0) + if linedata[0].find('total') == 0: + linedata.pop(0) - # Delete last line if it is blank - if cleandata[-1] == '': - cleandata.pop(-1) + # Clear any blank lines + cleandata = list(filter(None, linedata)) - # Check if -l was used to parse extra data - if re.match('^[-dclpsbDCMnP?]([-r][-w][-xsS]){2}([-r][-w][-xtT])[+]?', cleandata[0]): - for entry in cleandata: - output_line = {} + if cleandata: + # Check if -l was used to parse extra data + if re.match('^[-dclpsbDCMnP?]([-r][-w][-xsS]){2}([-r][-w][-xtT])[+]?', cleandata[0]): + for entry in cleandata: + output_line = {} - parsed_line = entry.split() + parsed_line = entry.split(maxsplit=8) - # split filenames and links - filename_field = ' '.join(parsed_line[8:]).split(' -> ') + # split filenames and links + filename_field = parsed_line[8].split(' -> ') - # create list of dictionaries - output_line['filename'] = filename_field[0] + # create list of dictionaries + output_line['filename'] = filename_field[0] - if len(filename_field) > 1: - output_line['link_to'] = filename_field[1] + if len(filename_field) > 1: + output_line['link_to'] = filename_field[1] - output_line['flags'] = parsed_line[0] - output_line['links'] = int(parsed_line[1]) - output_line['owner'] = parsed_line[2] - output_line['group'] = parsed_line[3] - output_line['bytes'] = int(parsed_line[4]) - output_line['date'] = ' '.join(parsed_line[5:8]) - output.append(output_line) - else: - for entry in cleandata: - output_line = {} - output_line['filename'] = entry - output.append(output_line) + output_line['flags'] = parsed_line[0] + output_line['links'] = int(parsed_line[1]) + output_line['owner'] = parsed_line[2] + output_line['group'] = parsed_line[3] + output_line['bytes'] = int(parsed_line[4]) + output_line['date'] = ' '.join(parsed_line[5:8]) + output.append(output_line) + else: + for entry in cleandata: + output_line = {} + output_line['filename'] = entry + output.append(output_line) return output diff --git a/setup.py b/setup.py index a649a3f2..301394f4 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.3', + version='0.5.4', author='Kelly Brazil', author_email='kellyjonbrazil@gmail.com', description='This tool serializes the output of popular command line tools to structured JSON output.',