Refactor Keenetic model to utilize centralized UTF-8 decoding utility
- Removed the internal `_decode_utf` method from the `Keenetic` class and replaced its usage with the new `decode_utf` utility function for decoding interface descriptions. - Added a new configuration file `config.conf` for Keenetic devices to facilitate testing. - Introduced an expected output JSON file `config.expected.json` to validate the parsing of Keenetic configurations against expected results.
This commit is contained in:
@@ -1,24 +1,13 @@
|
||||
from ipaddress import ip_interface
|
||||
from oxi.interfaces import register_parser
|
||||
from oxi.interfaces.base import BaseDevice
|
||||
from oxi.interfaces.utils import decode_utf
|
||||
|
||||
|
||||
@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:
|
||||
@@ -29,7 +18,7 @@ class Keenetic(BaseDevice):
|
||||
item["mask"] = ipaddress.network.prefixlen
|
||||
item.pop("netmask", "Key not found")
|
||||
if item.get("description"):
|
||||
decoded = self._decode_utf(item.get("description", ""))
|
||||
decoded = decode_utf(item.get("description", ""))
|
||||
item["description"] = decoded
|
||||
return interfaces
|
||||
|
||||
@@ -37,13 +26,6 @@ class Keenetic(BaseDevice):
|
||||
vlans = self.raw["vlans"]
|
||||
for item in vlans:
|
||||
if item.get("description"):
|
||||
decoded = self._decode_utf(item.get("description", ""))
|
||||
decoded = 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())
|
||||
|
||||
Reference in New Issue
Block a user