mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-15 01:24:29 +02:00
simplified logic
This commit is contained in:
@ -92,16 +92,12 @@ def parse(data, raw=False, quiet=False):
|
|||||||
device_name = None
|
device_name = None
|
||||||
detail_obj = {}
|
detail_obj = {}
|
||||||
detail_key = ''
|
detail_key = ''
|
||||||
# last_detail_key = ''
|
|
||||||
|
|
||||||
if jc.utils.has_data(data):
|
if jc.utils.has_data(data):
|
||||||
|
|
||||||
for line in filter(None, data.splitlines()):
|
for line in filter(None, data.splitlines()):
|
||||||
if line.startswith('Device:') or line.startswith('Daemon:'):
|
if line.startswith('Device:') or line.startswith('Daemon:'):
|
||||||
if device_obj:
|
if device_obj:
|
||||||
if detail_obj:
|
|
||||||
device_obj[detail_key] = detail_obj
|
|
||||||
|
|
||||||
raw_output.append(device_obj)
|
raw_output.append(device_obj)
|
||||||
device_obj = {}
|
device_obj = {}
|
||||||
|
|
||||||
@ -122,42 +118,34 @@ def parse(data, raw=False, quiet=False):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# history detail lines
|
# history detail lines
|
||||||
# if detail_key.startswith('history_'):
|
|
||||||
if line.startswith(' ') and ':' not in line:
|
if line.startswith(' ') and ':' not in line:
|
||||||
|
device_obj[detail_key] = 'history detail line'
|
||||||
continue
|
continue
|
||||||
# detail_obj['history_line'] = 'history detail line'
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# general detail lines
|
# general detail lines
|
||||||
if line.startswith(' ') and ':' in line:
|
if line.startswith(' ') and ':' in line:
|
||||||
# since there could be multiple detail objects, reset the object key
|
key = line.split(':', maxsplit=1)[0].strip().lower().replace('-', '_').replace(' ', '_').replace('(', '').replace(')', '')
|
||||||
# if detail_key != last_detail_key:
|
|
||||||
# device_obj[last_detail_key] = detail_obj
|
|
||||||
# last_detail_key = detail_key
|
|
||||||
|
|
||||||
key = line.split(':', maxsplit=1)[0].strip().lower().replace('-', '_').replace(' ', '_')
|
|
||||||
val = line.split(':', maxsplit=1)[1].strip()
|
val = line.split(':', maxsplit=1)[1].strip()
|
||||||
detail_obj[key] = val
|
device_obj[detail_key][key] = val
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# history lines are a special case of detail lines
|
# history detail lines are a special case of detail lines
|
||||||
# set the history detail key
|
# set the history detail key
|
||||||
if line.startswith(' ') and ':' in line and line.strip().split(':', maxsplit=1)[1] == '':
|
if line.startswith(' ') and ':' in line and line.strip().split(':', maxsplit=1)[1] == '':
|
||||||
|
detail_key = line.strip().lower().replace('-', '_').replace(' ', '_').replace('(', '').replace(')', '').rstrip(':')
|
||||||
|
device_obj[detail_key] = {}
|
||||||
continue
|
continue
|
||||||
# detail_key = line.strip().lower().replace('-', '_').replace(' ', '_').replace('(', '').replace(')', '')[:-1]
|
|
||||||
# device_obj[detail_key] = {}
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# top level lines
|
# top level lines
|
||||||
if line.startswith(' ') and ':' in line:
|
if line.startswith(' ') and ':' in line:
|
||||||
key = line.split(':', maxsplit=1)[0].strip().lower().replace('-', '_').replace(' ', '_')
|
key = line.split(':', maxsplit=1)[0].strip().lower().replace('-', '_').replace(' ', '_').replace('(', '').replace(')', '')
|
||||||
val = line.split(':', maxsplit=1)[1].strip()
|
val = line.split(':', maxsplit=1)[1].strip()
|
||||||
device_obj[key] = val
|
device_obj[key] = val
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# set the detail key
|
# set the general detail key
|
||||||
if line.startswith(' ') and ':' not in line:
|
if line.startswith(' ') and ':' not in line:
|
||||||
detail_key = line.strip().lower().replace('-', '_').replace(' ', '_')
|
detail_key = line.strip().lower().replace('-', '_').replace(' ', '_').replace('(', '').replace(')', '')
|
||||||
device_obj[detail_key] = {}
|
device_obj[detail_key] = {}
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user