Enhance Keenetic model and update templates for improved data handling
- Implemented the `interfaces` and `vlans` methods in the `Keenetic` model to process and decode interface and VLAN data. - Added a `_decode_utf` method to handle UTF-8 encoded descriptions. - Updated the Keenetic TTP template to define structured groups for system, interfaces, and VLANs. - Refactored file paths in the `Mikrotik` model for consistency and clarity.
This commit is contained in:
@@ -1,20 +1,54 @@
|
||||
from ipaddress import ip_interface
|
||||
from pprint import pprint
|
||||
from oxi.interfaces import register_parser
|
||||
from oxi.interfaces.base import BaseDevice
|
||||
from oxi.interfaces.contract import Interfaces, System, Vlans
|
||||
|
||||
|
||||
@register_parser(["NDMS", "keenetic", "KeeneticOS"])
|
||||
class Keenetic(BaseDevice):
|
||||
template = "keenetic.ttp"
|
||||
|
||||
def system(self): ...
|
||||
def system(self):
|
||||
return System(**self._raw["system"])
|
||||
|
||||
def interfaces(self): ...
|
||||
def _decode_utf(self, text: str):
|
||||
if "\\x" in text:
|
||||
desc = text.strip('"')
|
||||
decoded = (
|
||||
desc.encode("utf-8")
|
||||
.decode("unicode_escape")
|
||||
.encode("latin1")
|
||||
.decode("utf-8")
|
||||
)
|
||||
return decoded
|
||||
return text
|
||||
|
||||
def vlans(self): ...
|
||||
def interfaces(self):
|
||||
interfaces: list[dict] = self._raw["interfaces"]
|
||||
for item in interfaces:
|
||||
if item.get("ip_address") and item.get("netmask"):
|
||||
ipaddress = ip_interface(
|
||||
f"{item.get('ip_address')}/{item.get('netmask')}"
|
||||
)
|
||||
item["mask"] = ipaddress.network.prefixlen
|
||||
item.pop("netmask", "Key not found")
|
||||
if item.get("description"):
|
||||
decoded = self._decode_utf(item.get("description", ""))
|
||||
item["description"] = decoded
|
||||
return [Interfaces(**item) for item in interfaces]
|
||||
|
||||
def vlans(self):
|
||||
vlans = self._raw["vlans"]
|
||||
for item in vlans:
|
||||
if item.get("description"):
|
||||
decoded = self._decode_utf(item.get("description", ""))
|
||||
item["description"] = decoded
|
||||
return [Vlans(**item) for item in vlans]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("../../test2.txt") as file:
|
||||
with open("./test2.txt") as file:
|
||||
data = file.read()
|
||||
mikr = Keenetic(data)
|
||||
print(mikr.parse().json())
|
||||
print(mikr.parse().model_dump_json())
|
||||
|
||||
Reference in New Issue
Block a user