Files
oxipy/oxi/interfaces/models/keenetic.py
IluaAir 3635a07b27 Refactor BaseDevice methods for improved data handling and validation
- Updated the `BaseDevice` class to replace `_raw` with `raw` for consistency in data access.
- Enhanced the `vlans`, `interfaces`, and `system` methods to utilize the new `raw` attribute.
- Introduced a `_validate_contract` method to streamline the validation of parsed data into structured models.
- Adjusted the `Keenetic` and `Mikrotik` models to align with the updated data handling approach, ensuring proper parsing and decoding of interface and VLAN data.
2026-02-22 09:52:17 +03:00

51 lines
1.6 KiB
Python

from ipaddress import ip_interface
from oxi.interfaces import register_parser
from oxi.interfaces.base import BaseDevice
from oxi.interfaces.contract import Interfaces, Vlans
@register_parser(["NDMS", "keenetic", "KeeneticOS"])
class Keenetic(BaseDevice):
template = "keenetic.ttp"
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 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
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
if __name__ == "__main__":
with open("./test2.txt") as file:
data = file.read()
mikr = Keenetic(data)
print(mikr.parse().model_dump_json())