mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-15 01:24:29 +02:00
fix for no data
This commit is contained in:
@ -8,7 +8,15 @@ jc changelog
|
||||
- Update crontab_u parser to fix error on parsing empty data
|
||||
- Update df parser to fix error on parsing empty data
|
||||
- Update free parser to fix error on parsing empty data
|
||||
- Update lsblk parser to fix error on parsing empty data
|
||||
- Update lsmod parser to fix error on parsing empty data
|
||||
- Update mount parser to fix error on parsing empty data
|
||||
- Update netstat parser to fix error on parsing empty data
|
||||
- Update ntpq parser to fix error on parsing empty data
|
||||
- Update ps parser to fix error on parsing empty data
|
||||
- Update route parser to fix error on parsing empty data
|
||||
- Add tests to all parsers for no data condition
|
||||
- Update ss parser to fix integer fields
|
||||
|
||||
20200610 v1.11.4
|
||||
- Update ls parser to fix error on parsing an empty directory
|
||||
|
@ -56,7 +56,7 @@ import jc.utils
|
||||
|
||||
|
||||
class info():
|
||||
version = '1.3'
|
||||
version = '1.4'
|
||||
description = 'mount command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -162,6 +162,7 @@ def parse(data, raw=False, quiet=False):
|
||||
|
||||
# Clear any blank lines
|
||||
cleandata = list(filter(None, linedata))
|
||||
raw_output = []
|
||||
|
||||
if cleandata:
|
||||
# check for OSX output
|
||||
|
@ -247,7 +247,7 @@ Examples:
|
||||
|
||||
|
||||
class info():
|
||||
version = '1.6'
|
||||
version = '1.7'
|
||||
description = 'netstat command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -435,25 +435,26 @@ def parse(data, raw=False, quiet=False):
|
||||
cleandata = list(filter(None, cleandata))
|
||||
raw_output = []
|
||||
|
||||
# check for FreeBSD/OSX vs Linux
|
||||
# is this from FreeBSD/OSX?
|
||||
if cleandata[0] == 'Active Internet connections' \
|
||||
or cleandata[0] == 'Active Internet connections (including servers)' \
|
||||
or cleandata[0] == 'Active Multipath Internet connections' \
|
||||
or cleandata[0] == 'Active LOCAL (UNIX) domain sockets' \
|
||||
or cleandata[0] == 'Registered kernel control modules' \
|
||||
or cleandata[0] == 'Active kernel event sockets' \
|
||||
or cleandata[0] == 'Active kernel control sockets' \
|
||||
or cleandata[0] == 'Routing tables' \
|
||||
or cleandata[0].startswith('Name '):
|
||||
if cleandata:
|
||||
# check for FreeBSD/OSX vs Linux
|
||||
# is this from FreeBSD/OSX?
|
||||
if cleandata[0] == 'Active Internet connections' \
|
||||
or cleandata[0] == 'Active Internet connections (including servers)' \
|
||||
or cleandata[0] == 'Active Multipath Internet connections' \
|
||||
or cleandata[0] == 'Active LOCAL (UNIX) domain sockets' \
|
||||
or cleandata[0] == 'Registered kernel control modules' \
|
||||
or cleandata[0] == 'Active kernel event sockets' \
|
||||
or cleandata[0] == 'Active kernel control sockets' \
|
||||
or cleandata[0] == 'Routing tables' \
|
||||
or cleandata[0].startswith('Name '):
|
||||
|
||||
import jc.parsers.netstat_freebsd_osx
|
||||
raw_output = jc.parsers.netstat_freebsd_osx.parse(cleandata)
|
||||
import jc.parsers.netstat_freebsd_osx
|
||||
raw_output = jc.parsers.netstat_freebsd_osx.parse(cleandata)
|
||||
|
||||
# use linux parser
|
||||
else:
|
||||
import jc.parsers.netstat_linux
|
||||
raw_output = jc.parsers.netstat_linux.parse(cleandata)
|
||||
# use linux parser
|
||||
else:
|
||||
import jc.parsers.netstat_linux
|
||||
raw_output = jc.parsers.netstat_linux.parse(cleandata)
|
||||
|
||||
if raw:
|
||||
return raw_output
|
||||
|
@ -183,7 +183,7 @@ import jc.parsers.universal
|
||||
|
||||
|
||||
class info():
|
||||
version = '1.1'
|
||||
version = '1.2'
|
||||
description = 'ntpq -p command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -268,28 +268,29 @@ def parse(data, raw=False, quiet=False):
|
||||
if not quiet:
|
||||
jc.utils.compatibility(__name__, info.compatible)
|
||||
|
||||
cleandata = data.splitlines()
|
||||
raw_output = []
|
||||
|
||||
cleandata = data.splitlines()
|
||||
cleandata[0] = 's ' + cleandata[0]
|
||||
cleandata[0] = cleandata[0].lower()
|
||||
if list(filter(None, cleandata)):
|
||||
cleandata[0] = 's ' + cleandata[0]
|
||||
cleandata[0] = cleandata[0].lower()
|
||||
|
||||
# delete header delimiter
|
||||
del cleandata[1]
|
||||
# delete header delimiter
|
||||
del cleandata[1]
|
||||
|
||||
# separate first character with a space for easier parsing
|
||||
for i, line in list(enumerate(cleandata[1:])):
|
||||
if line[0] == ' ':
|
||||
# fixup for no-state
|
||||
cleandata[i + 1] = '~ ' + line[1:]
|
||||
else:
|
||||
# fixup - realign columns since we added the 's' column
|
||||
cleandata[i + 1] = line[:1] + ' ' + line[1:]
|
||||
# separate first character with a space for easier parsing
|
||||
for i, line in list(enumerate(cleandata[1:])):
|
||||
if line[0] == ' ':
|
||||
# fixup for no-state
|
||||
cleandata[i + 1] = '~ ' + line[1:]
|
||||
else:
|
||||
# fixup - realign columns since we added the 's' column
|
||||
cleandata[i + 1] = line[:1] + ' ' + line[1:]
|
||||
|
||||
# fixup for occaisional ip/hostname fields with a space
|
||||
cleandata[i + 1] = cleandata[i + 1].replace(' (', '_(')
|
||||
# fixup for occaisional ip/hostname fields with a space
|
||||
cleandata[i + 1] = cleandata[i + 1].replace(' (', '_(')
|
||||
|
||||
raw_output = jc.parsers.universal.simple_table_parse(cleandata)
|
||||
raw_output = jc.parsers.universal.simple_table_parse(cleandata)
|
||||
|
||||
if raw:
|
||||
return raw_output
|
||||
|
@ -32,7 +32,7 @@ import jc.parsers.universal
|
||||
|
||||
|
||||
class info():
|
||||
version = '1.1'
|
||||
version = '1.2'
|
||||
description = 'pip list command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -93,23 +93,24 @@ def parse(data, raw=False, quiet=False):
|
||||
# Clear any blank lines
|
||||
cleandata = list(filter(None, linedata))
|
||||
|
||||
# detect legacy output type
|
||||
if ' (' in cleandata[0]:
|
||||
for row in cleandata:
|
||||
raw_output.append({'package': row.split(' (')[0],
|
||||
'version': row.split(' (')[1].rstrip(')')})
|
||||
if cleandata:
|
||||
# detect legacy output type
|
||||
if ' (' in cleandata[0]:
|
||||
for row in cleandata:
|
||||
raw_output.append({'package': row.split(' (')[0],
|
||||
'version': row.split(' (')[1].rstrip(')')})
|
||||
|
||||
# otherwise normal table output
|
||||
else:
|
||||
# clear separator line
|
||||
for i, line in reversed(list(enumerate(cleandata))):
|
||||
if '---' in line:
|
||||
cleandata.pop(i)
|
||||
# otherwise normal table output
|
||||
else:
|
||||
# clear separator line
|
||||
for i, line in reversed(list(enumerate(cleandata))):
|
||||
if '---' in line:
|
||||
cleandata.pop(i)
|
||||
|
||||
cleandata[0] = cleandata[0].lower()
|
||||
cleandata[0] = cleandata[0].lower()
|
||||
|
||||
if cleandata:
|
||||
raw_output = jc.parsers.universal.simple_table_parse(cleandata)
|
||||
if cleandata:
|
||||
raw_output = jc.parsers.universal.simple_table_parse(cleandata)
|
||||
|
||||
if raw:
|
||||
return raw_output
|
||||
|
@ -177,7 +177,7 @@ import jc.parsers.universal
|
||||
|
||||
|
||||
class info():
|
||||
version = '1.1'
|
||||
version = '1.2'
|
||||
description = 'ps command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -282,9 +282,12 @@ 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()
|
||||
|
||||
raw_output = jc.parsers.universal.simple_table_parse(cleandata)
|
||||
|
||||
if raw:
|
||||
return raw_output
|
||||
|
@ -84,7 +84,7 @@ import jc.parsers.universal
|
||||
|
||||
|
||||
class info():
|
||||
version = '1.1'
|
||||
version = '1.2'
|
||||
description = 'route command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -182,9 +182,12 @@ def parse(data, raw=False, quiet=False):
|
||||
jc.utils.compatibility(__name__, info.compatible)
|
||||
|
||||
cleandata = data.splitlines()[1:]
|
||||
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()
|
||||
|
||||
raw_output = jc.parsers.universal.simple_table_parse(cleandata)
|
||||
|
||||
if raw:
|
||||
return raw_output
|
||||
|
@ -251,7 +251,7 @@ import jc.utils
|
||||
|
||||
|
||||
class info():
|
||||
version = '1.0'
|
||||
version = '1.1'
|
||||
description = 'ss command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -308,17 +308,17 @@ def process(proc_data):
|
||||
except (ValueError):
|
||||
entry[key] = None
|
||||
|
||||
if 'local_port' in entry:
|
||||
if 'local_port' in entry:
|
||||
try:
|
||||
entry['local_port_num'] = int(entry['local_port'])
|
||||
except (ValueError):
|
||||
pass
|
||||
|
||||
if 'peer_port' in entry:
|
||||
try:
|
||||
entry['peer_port_num'] = int(entry['peer_port'])
|
||||
except (ValueError):
|
||||
pass
|
||||
if 'peer_port' in entry:
|
||||
try:
|
||||
entry['peer_port_num'] = int(entry['peer_port'])
|
||||
except (ValueError):
|
||||
pass
|
||||
|
||||
return proc_data
|
||||
|
||||
|
2
tests/fixtures/centos-7.7/ss-sudo-a.json
vendored
2
tests/fixtures/centos-7.7/ss-sudo-a.json
vendored
File diff suppressed because one or more lines are too long
2
tests/fixtures/ubuntu-18.04/ss-sudo-a.json
vendored
2
tests/fixtures/ubuntu-18.04/ss-sudo-a.json
vendored
File diff suppressed because one or more lines are too long
@ -35,6 +35,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsof-sudo.json'), 'r', encoding='utf-8') as f:
|
||||
self.ubuntu_18_4_lsof_sudo_json = json.loads(f.read())
|
||||
|
||||
def test_lsof_nodata(self):
|
||||
"""
|
||||
Test 'lsof' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.lsof.parse('', quiet=True), [])
|
||||
|
||||
def test_lsof_centos_7_7(self):
|
||||
"""
|
||||
Test 'lsof' on Centos 7.7
|
||||
|
@ -35,6 +35,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/mount2.json'), 'r', encoding='utf-8') as f:
|
||||
self.osx_10_14_6_mount2_json = json.loads(f.read())
|
||||
|
||||
def test_mount_nodata(self):
|
||||
"""
|
||||
Test 'mount' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.mount.parse('', quiet=True), [])
|
||||
|
||||
def test_mount_centos_7_7(self):
|
||||
"""
|
||||
Test 'mount' on Centos 7.7
|
||||
|
@ -227,6 +227,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/netstat-ib.json'), 'r', encoding='utf-8') as f:
|
||||
self.freebsd12_netstat_ib_json = json.loads(f.read())
|
||||
|
||||
def test_netstat_nodata(self):
|
||||
"""
|
||||
Test 'netstat' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.netstat.parse('', quiet=True), [])
|
||||
|
||||
def test_netstat_centos_7_7(self):
|
||||
"""
|
||||
Test 'netstat' on Centos 7.7
|
||||
|
@ -47,6 +47,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/ntpq-p.json'), 'r', encoding='utf-8') as f:
|
||||
self.freebsd12_ntpq_p_json = json.loads(f.read())
|
||||
|
||||
def test_ntpq_p_nodata(self):
|
||||
"""
|
||||
Test 'ntpq -p' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.ntpq.parse('', quiet=True), [])
|
||||
|
||||
def test_ntpq_p_centos_7_7(self):
|
||||
"""
|
||||
Test 'ntpq -p' on Centos 7.7
|
||||
|
@ -29,6 +29,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/passwd.json'), 'r', encoding='utf-8') as f:
|
||||
self.osx_10_14_6_passwd_json = json.loads(f.read())
|
||||
|
||||
def test_passwd_nodata(self):
|
||||
"""
|
||||
Test 'cat /etc/passwd' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.passwd.parse('', quiet=True), [])
|
||||
|
||||
def test_passwd_centos_7_7(self):
|
||||
"""
|
||||
Test 'cat /etc/passwd' on Centos 7.7
|
||||
|
@ -41,6 +41,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/pip-list.json'), 'r', encoding='utf-8') as f:
|
||||
self.osx_10_14_6_pip_list_json = json.loads(f.read())
|
||||
|
||||
def test_pip_list_nodata(self):
|
||||
"""
|
||||
Test 'pip_list' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.pip_list.parse('', quiet=True), [])
|
||||
|
||||
def test_pip_list_centos_7_7(self):
|
||||
"""
|
||||
Test 'pip_list' on Centos 7.7
|
||||
|
@ -35,6 +35,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/pip-show.json'), 'r', encoding='utf-8') as f:
|
||||
self.osx_10_14_6_pip_show_json = json.loads(f.read())
|
||||
|
||||
def test_pip_show_nodata(self):
|
||||
"""
|
||||
Test 'pip show' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.pip_show.parse('', quiet=True), [])
|
||||
|
||||
def test_pip_show_centos_7_7(self):
|
||||
"""
|
||||
Test 'pip show' on Centos 7.7
|
||||
|
@ -59,6 +59,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ps-axu.json'), 'r', encoding='utf-8') as f:
|
||||
self.osx_10_14_6_ps_axu_json = json.loads(f.read())
|
||||
|
||||
def test_ps_nodata(self):
|
||||
"""
|
||||
Test 'ps' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.ps.parse('', quiet=True), [])
|
||||
|
||||
def test_ps_ef_centos_7_7(self):
|
||||
"""
|
||||
Test 'ps -ef' on Centos 7.7
|
||||
|
@ -41,6 +41,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/nixos/route-ee.json'), 'r', encoding='utf-8') as f:
|
||||
self.nixos_route_ee_json = json.loads(f.read())
|
||||
|
||||
def test_route_nodata(self):
|
||||
"""
|
||||
Test 'route' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.route.parse('', quiet=True), [])
|
||||
|
||||
def test_route_centos_7_7(self):
|
||||
"""
|
||||
Test 'route' on Centos 7.7
|
||||
|
@ -23,6 +23,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/shadow.json'), 'r', encoding='utf-8') as f:
|
||||
self.ubuntu_18_4_shadow_json = json.loads(f.read())
|
||||
|
||||
def test_shadow_nodata(self):
|
||||
"""
|
||||
Test 'cat /etc/shadow' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.shadow.parse('', quiet=True), [])
|
||||
|
||||
def test_shadow_centos_7_7(self):
|
||||
"""
|
||||
Test 'cat /etc/shadow' on Centos 7.7
|
||||
|
@ -23,6 +23,12 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ss-sudo-a.json'), 'r', encoding='utf-8') as f:
|
||||
self.ubuntu_18_4_ss_sudo_a_json = json.loads(f.read())
|
||||
|
||||
def test_ss_nodata(self):
|
||||
"""
|
||||
Test 'ss' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.ss.parse('', quiet=True), [])
|
||||
|
||||
def test_ss_sudo_a_centos_7_7(self):
|
||||
"""
|
||||
Test 'sudo ss -a' on Centos 7.7
|
||||
|
Reference in New Issue
Block a user