mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
firm up flow. add/remove text. parse routes
This commit is contained in:
@ -41,6 +41,7 @@ Examples:
|
||||
import re
|
||||
from typing import List, Dict, Optional
|
||||
import jc.utils
|
||||
from jc.parsers.universal import sparse_table_parse
|
||||
|
||||
|
||||
class info():
|
||||
@ -97,7 +98,15 @@ def _normalize_value(value: str) -> Optional[str]:
|
||||
return value
|
||||
|
||||
|
||||
def _add_text_kv(key: str, value: str) -> Optional[Dict]:
|
||||
def _normalize_header(keyname: str) -> str:
|
||||
return keyname.replace('.', '_')\
|
||||
.replace('[', '_')\
|
||||
.replace(']', ' ')\
|
||||
.replace('-', '_')\
|
||||
.lower()
|
||||
|
||||
|
||||
def _add_text_kv(key: str, value: Optional[str]) -> Optional[Dict]:
|
||||
"""
|
||||
Add keys with _text suffix if there is a text description inside
|
||||
paranthesis at the end of a value. The value of the _text field will
|
||||
@ -112,6 +121,28 @@ def _add_text_kv(key: str, value: str) -> Optional[Dict]:
|
||||
return None
|
||||
|
||||
|
||||
def _remove_text_from_value(value: Optional[str]) -> Optional[str]:
|
||||
"""
|
||||
Remove the text summary part of a value. Used when an extra text
|
||||
summary k/v pair are added.
|
||||
"""
|
||||
if value:
|
||||
return re.sub(r"\s+\((\w+)\)$", '', value)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _split_routes(value: str) -> Dict:
|
||||
# dst = 192.168.71.0/24, nh = 0.0.0.0, mt = 100
|
||||
# dst = ff00::/8, nh = ::, mt = 256, table=255
|
||||
output_dict = {}
|
||||
val_list = value.split(',')
|
||||
for val in val_list:
|
||||
k, v = val.split('=')
|
||||
output_dict[k.strip()] = v.strip()
|
||||
|
||||
return output_dict
|
||||
|
||||
|
||||
def _device_show_parse(data: str) -> List[Dict]:
|
||||
raw_output: List = []
|
||||
@ -132,6 +163,7 @@ def _device_show_parse(data: str) -> List[Dict]:
|
||||
|
||||
text_kv = _add_text_kv(key_n, value_n)
|
||||
if text_kv:
|
||||
item[key_n] = _remove_text_from_value(value_n)
|
||||
item.update(text_kv)
|
||||
|
||||
# get final item
|
||||
@ -154,14 +186,35 @@ def _connection_show_x_parse(data: str) -> List[Dict]:
|
||||
|
||||
text_kv = _add_text_kv(key_n, value_n)
|
||||
if text_kv:
|
||||
item[key_n] = _remove_text_from_value(value_n)
|
||||
item.update(text_kv)
|
||||
|
||||
if '_route_' in key_n and key_n[-1].isdigit():
|
||||
item[key_n] = _split_routes(item[key_n])
|
||||
|
||||
|
||||
if item:
|
||||
raw_output.append(item)
|
||||
|
||||
return raw_output
|
||||
|
||||
|
||||
def _general_permissions_parse(data: str) -> List[Dict]:
|
||||
print('general permissions')
|
||||
|
||||
|
||||
def _table_parse(data: str) -> List[Dict]:
|
||||
data_list = list(filter(None, data.splitlines()))
|
||||
data_list[0] = _normalize_header(data_list[0])
|
||||
raw_output = sparse_table_parse(data_list)
|
||||
|
||||
for item in raw_output:
|
||||
for key in item:
|
||||
item[key] = _normalize_value(item[key])
|
||||
|
||||
return raw_output
|
||||
|
||||
|
||||
def parse(
|
||||
data: str,
|
||||
raw: bool = False,
|
||||
@ -187,13 +240,27 @@ def parse(
|
||||
|
||||
if jc.utils.has_data(data):
|
||||
|
||||
# nmcli (second line startswith \t)
|
||||
if data.splitlines()[1].startswith('\t'):
|
||||
print('nmcli only')
|
||||
|
||||
# nmcli device show
|
||||
# nmcli device show lo
|
||||
if data.startswith('GENERAL.DEVICE'):
|
||||
elif data.startswith('GENERAL.DEVICE'):
|
||||
raw_output = _device_show_parse(data)
|
||||
|
||||
# nmcli connection show lo
|
||||
elif data.startswith('connection.id:'):
|
||||
raw_output = _connection_show_x_parse(data)
|
||||
|
||||
# nmcli general permissions (k/v pairs)
|
||||
elif data.startswith('PERMISSION '):
|
||||
raw_output = _general_permissions_parse(data)
|
||||
|
||||
# nmcli general
|
||||
# nmcli connection
|
||||
# nmcli device
|
||||
else:
|
||||
raw_output = _table_parse(data)
|
||||
|
||||
return raw_output if raw else _process(raw_output)
|
||||
|
Reference in New Issue
Block a user