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:
@ -4,6 +4,7 @@ jc changelog
|
||||
- Add OSX support for the ifconfig parser
|
||||
- Add OSX support for the arp parser
|
||||
- Add OSX support for the df parser
|
||||
- Add universal parsers to refactor repetitive code
|
||||
- Updated ifconfig parser to output state as an array
|
||||
|
||||
20191117 v1.5.1
|
||||
|
@ -81,6 +81,7 @@ Examples:
|
||||
]
|
||||
"""
|
||||
import jc.utils
|
||||
import jc.parsers.universal
|
||||
|
||||
|
||||
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
|
||||
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_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:])
|
||||
raw_output = [dict(zip(headers, r)) for r in raw_data]
|
||||
raw_output = jc.parsers.universal.simple_table_parse(cleandata)
|
||||
|
||||
if raw:
|
||||
return raw_output
|
||||
|
@ -4,6 +4,30 @@
|
||||
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'):
|
||||
"""
|
||||
Parse tables with missing column data or with spaces in column data.
|
||||
|
Reference in New Issue
Block a user