1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

use universal simple table parser

This commit is contained in:
Kelly Brazil
2019-12-11 17:27:48 -08:00
parent d221b4aa29
commit a56e4dc752
3 changed files with 28 additions and 3 deletions

View File

@ -4,6 +4,7 @@ jc changelog
- Add OSX support for the ifconfig parser - Add OSX support for the ifconfig parser
- Add OSX support for the arp parser - Add OSX support for the arp parser
- Add OSX support for the df parser - Add OSX support for the df parser
- Add universal parsers to refactor repetitive code
- Updated ifconfig parser to output state as an array - Updated ifconfig parser to output state as an array
20191117 v1.5.1 20191117 v1.5.1

View File

@ -81,6 +81,7 @@ Examples:
] ]
""" """
import jc.utils import jc.utils
import jc.parsers.universal
def process(proc_data): def process(proc_data):
@ -168,10 +169,9 @@ def parse(data, raw=False, quiet=False):
# fix header row to change Flags Mask to flags_mask # fix header row to change Flags Mask to flags_mask
cleandata[0] = cleandata[0].replace('Flags Mask', 'flags_mask') cleandata[0] = cleandata[0].replace('Flags Mask', 'flags_mask')
cleandata[0] = cleandata[0].lower()
headers = [h for h in ' '.join(cleandata[0].lower().strip().split()).split() if h] raw_output = jc.parsers.universal.simple_table_parse(cleandata)
raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:])
raw_output = [dict(zip(headers, r)) for r in raw_data]
if raw: if raw:
return raw_output return raw_output

View File

@ -4,6 +4,30 @@
import string import string
def simple_table_parse(data):
"""
Parse simple tables. The last column may contain data with spaces
Parameters:
data: (list) Text data to parse that has been split into lines via .splitlines().
Item 0 must be the header row. Any spaces in header names should be
changed to underscore '_'. You should also ensure headers are
lowercase by using .lower().
Also, ensure there are no blank lines (list items) in the data.
Returns:
dictionary raw structured data
"""
headers = [h for h in ' '.join(data[0].strip().split()).split() if h]
raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), data[1:])
raw_output = [dict(zip(headers, r)) for r in raw_data]
return raw_output
def sparse_table_parse(data, delim='\u2063'): def sparse_table_parse(data, delim='\u2063'):
""" """
Parse tables with missing column data or with spaces in column data. Parse tables with missing column data or with spaces in column data.