mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
add dev show and conn show parsers
This commit is contained in:
@ -38,7 +38,7 @@ Examples:
|
|||||||
$ nmcli | jc --nmcli -p -r
|
$ nmcli | jc --nmcli -p -r
|
||||||
[]
|
[]
|
||||||
"""
|
"""
|
||||||
from typing import List, Dict
|
from typing import List, Dict, Optional
|
||||||
import jc.utils
|
import jc.utils
|
||||||
|
|
||||||
|
|
||||||
@ -84,6 +84,59 @@ def _normalize_key(keyname: str) -> str:
|
|||||||
.replace('GENERAL_', '')\
|
.replace('GENERAL_', '')\
|
||||||
.lower()
|
.lower()
|
||||||
|
|
||||||
|
def _normalize_value(value: str) -> Optional[str]:
|
||||||
|
value = value.strip()
|
||||||
|
|
||||||
|
if value == '""':
|
||||||
|
value = ''
|
||||||
|
|
||||||
|
if value == '--':
|
||||||
|
return None
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def _device_show_parse(data: str) -> List[Dict]:
|
||||||
|
raw_output: List = []
|
||||||
|
item: Dict = {}
|
||||||
|
current_item = ''
|
||||||
|
|
||||||
|
for line in filter(None, data.splitlines()):
|
||||||
|
key, value = line.split(':', maxsplit=1)
|
||||||
|
key_n = _normalize_key(key)
|
||||||
|
value_n = _normalize_value(value)
|
||||||
|
|
||||||
|
if item and 'device' in key_n and value_n != current_item:
|
||||||
|
raw_output.append(item)
|
||||||
|
item = {}
|
||||||
|
current_item = value
|
||||||
|
|
||||||
|
item.update({key_n: value_n})
|
||||||
|
|
||||||
|
# get final item
|
||||||
|
if item:
|
||||||
|
raw_output.append(item)
|
||||||
|
|
||||||
|
return raw_output
|
||||||
|
|
||||||
|
|
||||||
|
def _connection_show_x_parse(data: str) -> List[Dict]:
|
||||||
|
raw_output: List = []
|
||||||
|
item: Dict = {}
|
||||||
|
|
||||||
|
for line in filter(None, data.splitlines()):
|
||||||
|
key, value = line.split(':', maxsplit=1)
|
||||||
|
|
||||||
|
key_n = _normalize_key(key)
|
||||||
|
value_n = _normalize_value(value)
|
||||||
|
item.update({key_n: value_n})
|
||||||
|
|
||||||
|
if item:
|
||||||
|
raw_output.append(item)
|
||||||
|
|
||||||
|
return raw_output
|
||||||
|
|
||||||
|
|
||||||
def parse(
|
def parse(
|
||||||
data: str,
|
data: str,
|
||||||
@ -107,24 +160,16 @@ def parse(
|
|||||||
jc.utils.input_type_check(data)
|
jc.utils.input_type_check(data)
|
||||||
|
|
||||||
raw_output: List = []
|
raw_output: List = []
|
||||||
item: Dict = {}
|
|
||||||
current_item = ''
|
|
||||||
|
|
||||||
if jc.utils.has_data(data):
|
if jc.utils.has_data(data):
|
||||||
|
|
||||||
for line in filter(None, data.splitlines()):
|
# nmcli device show
|
||||||
key, value = line.split(':', maxsplit=1)
|
# nmcli device show lo
|
||||||
key = _normalize_key(key)
|
if data.startswith('GENERAL.DEVICE'):
|
||||||
value = value.strip()
|
raw_output = _device_show_parse(data)
|
||||||
|
|
||||||
if item and 'device' in key and value != current_item:
|
# nmcli connection show lo
|
||||||
raw_output.append(item)
|
elif data.startswith('connection.id:'):
|
||||||
item = {}
|
raw_output = _connection_show_x_parse(data)
|
||||||
current_item = value
|
|
||||||
|
|
||||||
item.update({key: value})
|
|
||||||
|
|
||||||
if item:
|
|
||||||
raw_output.append(item)
|
|
||||||
|
|
||||||
return raw_output if raw else _process(raw_output)
|
return raw_output if raw else _process(raw_output)
|
||||||
|
Reference in New Issue
Block a user