From a56e4dc752a01635fbd9fc31e21d390bbd629084 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 11 Dec 2019 17:27:48 -0800 Subject: [PATCH] use universal simple table parser --- changelog.txt | 1 + jc/parsers/arp.py | 6 +++--- jc/parsers/universal.py | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/changelog.txt b/changelog.txt index 7f879159..7aaa18f8 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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 diff --git a/jc/parsers/arp.py b/jc/parsers/arp.py index 107c562d..8d6e018a 100644 --- a/jc/parsers/arp.py +++ b/jc/parsers/arp.py @@ -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 diff --git a/jc/parsers/universal.py b/jc/parsers/universal.py index 7f7877fe..f5ad1953 100644 --- a/jc/parsers/universal.py +++ b/jc/parsers/universal.py @@ -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.