From d2ccad6a83138fbf6eb863634fd354dc6c3fa238 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Thu, 11 Jun 2020 17:09:51 -0700 Subject: [PATCH] fix for no data --- jc/parsers/lsblk.py | 21 ++++++++++++--------- jc/parsers/lsmod.py | 15 +++++++++------ tests/test_lsblk.py | 6 ++++++ tests/test_lsmod.py | 6 ++++++ 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/jc/parsers/lsblk.py b/jc/parsers/lsblk.py index 49544d51..b4955191 100644 --- a/jc/parsers/lsblk.py +++ b/jc/parsers/lsblk.py @@ -216,7 +216,7 @@ import jc.parsers.universal class info(): - version = '1.3' + version = '1.4' description = 'lsblk command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -330,17 +330,20 @@ def parse(data, raw=False, quiet=False): linedata = data.splitlines() # Clear any blank lines cleandata = list(filter(None, linedata)) - cleandata = data.splitlines() + raw_output = [] - cleandata[0] = cleandata[0].lower() - cleandata[0] = cleandata[0].replace(':', '_') - cleandata[0] = cleandata[0].replace('-', '_') + if cleandata: + cleandata = data.splitlines() - raw_output = jc.parsers.universal.sparse_table_parse(cleandata) + cleandata[0] = cleandata[0].lower() + cleandata[0] = cleandata[0].replace(':', '_') + cleandata[0] = cleandata[0].replace('-', '_') - # clean up non-ascii characters, if any - for entry in raw_output: - entry['name'] = entry['name'].encode('ascii', errors='ignore').decode() + raw_output = jc.parsers.universal.sparse_table_parse(cleandata) + + # clean up non-ascii characters, if any + for entry in raw_output: + entry['name'] = entry['name'].encode('ascii', errors='ignore').decode() if raw: return raw_output diff --git a/jc/parsers/lsmod.py b/jc/parsers/lsmod.py index 6b8cf40a..488346af 100644 --- a/jc/parsers/lsmod.py +++ b/jc/parsers/lsmod.py @@ -107,7 +107,7 @@ import jc.parsers.universal class info(): - version = '1.1' + version = '1.2' description = 'lsmod command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -175,13 +175,16 @@ def parse(data, raw=False, quiet=False): jc.utils.compatibility(__name__, info.compatible) cleandata = data.splitlines() - cleandata[0] = cleandata[0].lower() + raw_output = [] - raw_output = jc.parsers.universal.simple_table_parse(cleandata) + if list(filter(None, cleandata)): + cleandata[0] = cleandata[0].lower() - for mod in raw_output: - if 'by' in mod: - mod['by'] = mod['by'].split(',') + raw_output = jc.parsers.universal.simple_table_parse(cleandata) + + for mod in raw_output: + if 'by' in mod: + mod['by'] = mod['by'].split(',') if raw: return raw_output diff --git a/tests/test_lsblk.py b/tests/test_lsblk.py index 0ec2c1d7..606ce638 100644 --- a/tests/test_lsblk.py +++ b/tests/test_lsblk.py @@ -35,6 +35,12 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsblk-allcols.json'), 'r', encoding='utf-8') as f: self.ubuntu_18_4_lsblk_allcols_json = json.loads(f.read()) + def test_lsblk_nodata(self): + """ + Test 'lsblk' with no data + """ + self.assertEqual(jc.parsers.lsblk.parse('', quiet=True), []) + def test_lsblk_centos_7_7(self): """ Test 'lsblk' on Centos 7.7 diff --git a/tests/test_lsmod.py b/tests/test_lsmod.py index 2ac482d5..5cd9b768 100644 --- a/tests/test_lsmod.py +++ b/tests/test_lsmod.py @@ -23,6 +23,12 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsmod.json'), 'r', encoding='utf-8') as f: self.ubuntu_18_4_lsmod_json = json.loads(f.read()) + def test_lsmod_nodata(self): + """ + Test 'lsmod' with no data + """ + self.assertEqual(jc.parsers.lsmod.parse('', quiet=True), []) + def test_lsmod_centos_7_7(self): """ Test 'lsmod' on Centos 7.7