From 5f8e70d73054f1a106c0e75eee621bc0cefd1c6b Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 25 Oct 2019 10:55:09 -0700 Subject: [PATCH] convert headers to lowercase --- changelog.txt | 1 + jc/parsers/df.py | 4 ++-- jc/parsers/free.py | 2 +- jc/parsers/lsblk.py | 8 ++++---- jc/parsers/lsmod.py | 9 +++------ jc/parsers/lsof.py | 8 ++++---- jc/parsers/ps.py | 2 +- jc/parsers/route.py | 2 +- jc/parsers/w.py | 6 +++--- 9 files changed, 20 insertions(+), 22 deletions(-) diff --git a/changelog.txt b/changelog.txt index 079e487f..250a6382 100644 --- a/changelog.txt +++ b/changelog.txt @@ -6,6 +6,7 @@ jc changelog - Add history parser - Flatten env parser output - Remove problematic characters from key names in: df, free, history, lsblk, lsof, and w +- Where possible, lowercase all keys (except cases like env where the key is the variable name) 20191023 v0.9.1 - Add jobs parser diff --git a/jc/parsers/df.py b/jc/parsers/df.py index 7d04a68d..3bb0a60d 100644 --- a/jc/parsers/df.py +++ b/jc/parsers/df.py @@ -50,11 +50,11 @@ def parse(data): # https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501 cleandata = data.splitlines() - headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h] + headers = [h for h in ' '.join(cleandata[0].lower().strip().split()).split() if h] # clean up 'Use%' header # even though % in a key is valid json, it can make things difficult - headers = ['Use_percent' if x == 'Use%' else x for x in headers] + headers = ['use_percent' if x == 'use%' else x for x in headers] raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:]) return [dict(zip(headers, r)) for r in raw_data] diff --git a/jc/parsers/free.py b/jc/parsers/free.py index 9f24c5a9..5d775a3d 100644 --- a/jc/parsers/free.py +++ b/jc/parsers/free.py @@ -32,7 +32,7 @@ def parse(data): # https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501 cleandata = data.splitlines() - headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h] + headers = [h for h in ' '.join(cleandata[0].lower().strip().split()).split() if h] headers.insert(0, "type") # clean up 'buff/cache' header diff --git a/jc/parsers/lsblk.py b/jc/parsers/lsblk.py index 0d02fef3..3b2e1423 100644 --- a/jc/parsers/lsblk.py +++ b/jc/parsers/lsblk.py @@ -59,16 +59,16 @@ def parse(data): # https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501 cleandata = data.splitlines() - headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h] + headers = [h for h in ' '.join(cleandata[0].lower().strip().split()).split() if h] - # clean up 'MAJ:MIN' header + # clean up 'maj:min' header # even though colon in a key is valid json, it can make things difficult - headers = ['MAJ_MIN' if x == 'MAJ:MIN' else x for x in headers] + headers = ['maj_min' if x == 'maj:min' else x for x in headers] raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:]) output = [dict(zip(headers, r)) for r in raw_data] for entry in output: - entry['NAME'] = entry['NAME'].encode('ascii', errors='ignore').decode() + entry['name'] = entry['name'].encode('ascii', errors='ignore').decode() return output diff --git a/jc/parsers/lsmod.py b/jc/parsers/lsmod.py index 625e38cd..14fde7c9 100644 --- a/jc/parsers/lsmod.py +++ b/jc/parsers/lsmod.py @@ -65,16 +65,13 @@ def parse(data): # https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501 cleandata = data.splitlines() - headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h] - - headers.pop(-1) - headers.append('By') + 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:]) output = [dict(zip(headers, r)) for r in raw_data] for mod in output: - if 'By' in mod: - mod['By'] = mod['By'].split(',') + if 'by' in mod: + mod['by'] = mod['by'].split(',') return output diff --git a/jc/parsers/lsof.py b/jc/parsers/lsof.py index da2243b5..60481b15 100644 --- a/jc/parsers/lsof.py +++ b/jc/parsers/lsof.py @@ -85,11 +85,11 @@ def parse(data): # find column value of last character of each header header_row = cleandata.pop(0) - headers = header_row.split() + headers = header_row.lower().split() - # clean up 'SIZE/OFF' header + # clean up 'size/off' header # even though forward slash in a key is valid json, it can make things difficult - headers = ['SIZE_OFF' if x == 'SIZE/OFF' else x for x in headers] + headers = ['size_off' if x == 'size/off' else x for x in headers] header_spec = [] for i, h in enumerate(headers): @@ -109,7 +109,7 @@ def parse(data): header_name = spec[1] col = spec[2] - 1 - if header_name == 'COMMAND' or header_name == 'NAME': + if header_name == 'command' or header_name == 'name': continue if entry[col] == string.whitespace: temp_line.insert(index, None) diff --git a/jc/parsers/ps.py b/jc/parsers/ps.py index 14fc45f0..202fe24c 100644 --- a/jc/parsers/ps.py +++ b/jc/parsers/ps.py @@ -62,6 +62,6 @@ def parse(data): # https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501 cleandata = data.splitlines() - headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h] + 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:]) return [dict(zip(headers, r)) for r in raw_data] diff --git a/jc/parsers/route.py b/jc/parsers/route.py index 46c8f2dd..0ac1a5c9 100644 --- a/jc/parsers/route.py +++ b/jc/parsers/route.py @@ -58,6 +58,6 @@ def parse(data): # https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501 cleandata = data.splitlines()[1:] - headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h] + 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:]) return [dict(zip(headers, r)) for r in raw_data] diff --git a/jc/parsers/w.py b/jc/parsers/w.py index ec35d374..9ecbe157 100644 --- a/jc/parsers/w.py +++ b/jc/parsers/w.py @@ -37,11 +37,11 @@ def parse(data): # https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501 cleandata = data.splitlines()[1:] - headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h] + headers = [h for h in ' '.join(cleandata[0].lower().strip().split()).split() if h] - # clean up 'LOGIN@' header + # clean up 'login@' header # even though @ in a key is valid json, it can make things difficult - headers = ['LOGIN_AT' if x == 'LOGIN@' else x for x in headers] + headers = ['login_at' if x == 'login@' else x for x in headers] raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:]) return [dict(zip(headers, r)) for r in raw_data]