diff --git a/changelog.txt b/changelog.txt index cb7d673f..4e5c1dd4 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,6 +2,8 @@ jc changelog 20200610 v1.11.5 - Update airport_s parser to fix error on parsing empty data +- Update arp parser to fix error on parsing empty data +- Update blkid parser to fix error on parsing empty data - Add tests to all parsers for no data condition 20200610 v1.11.4 diff --git a/jc/parsers/arp.py b/jc/parsers/arp.py index bec019cf..78777c1b 100644 --- a/jc/parsers/arp.py +++ b/jc/parsers/arp.py @@ -99,7 +99,7 @@ import jc.parsers.universal class info(): - version = '1.4' + version = '1.5' description = 'arp command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -171,69 +171,65 @@ def parse(data, raw=False, quiet=False): if not quiet: jc.utils.compatibility(__name__, info.compatible) - cleandata = data.splitlines() + raw_output = [] + cleandata = list(filter(None, data.splitlines())) - # remove final Entries row if -v was used - if cleandata[-1].startswith('Entries:'): - cleandata.pop(-1) + if cleandata: - # detect if freebsd/osx style was used - if cleandata[0][-1] == ']': - raw_output = [] - for line in cleandata: - splitline = line.split() - output_line = { - 'name': splitline[0], - 'address': splitline[1].lstrip('(').rstrip(')'), - 'hwtype': splitline[-1].lstrip('[').rstrip(']'), - 'hwaddress': splitline[3], - 'iface': splitline[5] - } + # remove final Entries row if -v was used + if cleandata[-1].startswith('Entries:'): + cleandata.pop(-1) - if 'permanent' in splitline: - output_line['permanent'] = True + # detect if freebsd/osx style was used + if cleandata[0][-1] == ']': + for line in cleandata: + splitline = line.split() + output_line = { + 'name': splitline[0], + 'address': splitline[1].lstrip('(').rstrip(')'), + 'hwtype': splitline[-1].lstrip('[').rstrip(']'), + 'hwaddress': splitline[3], + 'iface': splitline[5] + } + + if 'permanent' in splitline: + output_line['permanent'] = True + else: + output_line['permanent'] = False + + if 'expires' in splitline: + output_line['expires'] = splitline[-3] + + raw_output.append(output_line) + + if raw: + return raw_output else: - output_line['permanent'] = False + return process(raw_output) - if 'expires' in splitline: - output_line['expires'] = splitline[-3] + # detect if linux style was used + elif cleandata[0].startswith('Address'): - raw_output.append(output_line) + # fix header row to change Flags Mask to flags_mask + cleandata[0] = cleandata[0].replace('Flags Mask', 'flags_mask') + cleandata[0] = cleandata[0].lower() - if raw: - return raw_output + raw_output = jc.parsers.universal.simple_table_parse(cleandata) + + # otherwise, try bsd style else: - return process(raw_output) + for line in cleandata: + line = line.split() + output_line = { + 'name': line[0], + 'address': line[1].lstrip('(').rstrip(')'), + 'hwtype': line[4].lstrip('[').rstrip(']'), + 'hwaddress': line[3], + 'iface': line[6], + } + raw_output.append(output_line) - # detect if linux style was used - elif cleandata[0].startswith('Address'): - - # fix header row to change Flags Mask to flags_mask - cleandata[0] = cleandata[0].replace('Flags Mask', 'flags_mask') - cleandata[0] = cleandata[0].lower() - - raw_output = jc.parsers.universal.simple_table_parse(cleandata) - - if raw: - return raw_output - else: - return process(raw_output) - - # otherwise, try bsd style + if raw: + return raw_output else: - raw_output = [] - for line in cleandata: - line = line.split() - output_line = { - 'name': line[0], - 'address': line[1].lstrip('(').rstrip(')'), - 'hwtype': line[4].lstrip('[').rstrip(']'), - 'hwaddress': line[3], - 'iface': line[6], - } - raw_output.append(output_line) - - if raw: - return raw_output - else: - return process(raw_output) + return process(raw_output) diff --git a/jc/parsers/blkid.py b/jc/parsers/blkid.py index b241acaf..0b11f120 100644 --- a/jc/parsers/blkid.py +++ b/jc/parsers/blkid.py @@ -79,7 +79,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = 'blkid command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -176,7 +176,7 @@ def parse(data, raw=False, quiet=False): raw_output = [] - if data: + if len(data) > 1: # if the first field is a device, use normal parsing: if data.split(maxsplit=1)[0][-1] == ':': linedata = data.splitlines() diff --git a/tests/test_arp.py b/tests/test_arp.py index 1db88555..0d0e6850 100644 --- a/tests/test_arp.py +++ b/tests/test_arp.py @@ -71,6 +71,12 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/arp-a.json'), 'r', encoding='utf-8') as f: self.freebsd12_arp_a_json = json.loads(f.read()) + def test_arp_nodata(self): + """ + Test 'arp' with no data + """ + self.assertEqual(jc.parsers.arp.parse('', quiet=True), []) + def test_arp_centos_7_7(self): """ Test 'arp' on Centos 7.7 diff --git a/tests/test_blkid.py b/tests/test_blkid.py index 5ea1f439..d76d3a67 100644 --- a/tests/test_blkid.py +++ b/tests/test_blkid.py @@ -71,6 +71,12 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/blkid-ip-udev-multi.json'), 'r', encoding='utf-8') as f: self.ubuntu_18_4_blkid_ip_udev_multi_json = json.loads(f.read()) + def test_blkid_nodata(self): + """ + Test 'blkid' with no data + """ + self.assertEqual(jc.parsers.blkid.parse('', quiet=True), []) + def test_blkid_centos_7_7(self): """ Test 'blkid' on Centos 7.7