"""
Implement Keenetic model and enhance BaseDevice documentation - Added a new `Keenetic` model that registers a parser for KeeneticOS, extending the `BaseDevice` class. - Updated docstrings in the `BaseDevice` class methods to provide clearer descriptions of their functionality and expected data structures. - Introduced `model_config` in the `Vlans` model to enable name-based population. - Removed unnecessary print statements from the `Mikrotik` model methods to streamline output. """
This commit is contained in:
@@ -28,7 +28,7 @@ class BaseDevice(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def vlans(self) -> list["Vlans"]:
|
||||
f"""
|
||||
"""
|
||||
Parse VLAN configuration from self._raw['vlans'].
|
||||
|
||||
Expected raw structure:
|
||||
@@ -45,7 +45,7 @@ class BaseDevice(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def interfaces(self) -> list["Interfaces"]:
|
||||
f"""
|
||||
"""
|
||||
Parse Interface configuration from self._raw['interfaces'].
|
||||
|
||||
Expected raw structure:
|
||||
@@ -70,6 +70,7 @@ class BaseDevice(ABC):
|
||||
...
|
||||
|
||||
def _load_template(self):
|
||||
"""Подгрузка темплейтов из папки models/templates"""
|
||||
path = Path(__file__).parent / "models" / "templates" / self.template
|
||||
if not path.exists():
|
||||
print("-" * 12)
|
||||
@@ -96,6 +97,7 @@ class BaseDevice(ABC):
|
||||
)
|
||||
|
||||
def _run_ttp(self) -> dict:
|
||||
""" Основной парсер """
|
||||
p = ttp(data=self.config, template=self._loaded_template)
|
||||
p.parse()
|
||||
raw: dict = p.result()[0][0]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from ipaddress import IPv4Address
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
|
||||
class Interfaces(BaseModel):
|
||||
@@ -16,6 +16,8 @@ class System(BaseModel):
|
||||
|
||||
|
||||
class Vlans(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
vlan_id: int
|
||||
name: str | None = Field(default=None, alias="description")
|
||||
|
||||
|
||||
20
oxi/interfaces/models/keenetic.py
Normal file
20
oxi/interfaces/models/keenetic.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from oxi.interfaces import register_parser
|
||||
from oxi.interfaces.base import BaseDevice
|
||||
|
||||
|
||||
@register_parser(["NDMS", "keenetic", "KeeneticOS"])
|
||||
class Keenetic(BaseDevice):
|
||||
template = "keenetic.ttp"
|
||||
|
||||
def system(self): ...
|
||||
|
||||
def interfaces(self): ...
|
||||
|
||||
def vlans(self): ...
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("../../test2.txt") as file:
|
||||
data = file.read()
|
||||
mikr = Keenetic(data)
|
||||
print(mikr.parse().json())
|
||||
@@ -12,11 +12,9 @@ class Mikrotik(BaseDevice):
|
||||
return System(**systems)
|
||||
|
||||
def interfaces(self) -> "Interfaces":
|
||||
print(self._raw.get("interfaces"))
|
||||
return [Interfaces(**item) for item in self._raw.get("interfaces")]
|
||||
|
||||
def vlans(self) -> list["Vlans"]:
|
||||
print(self._raw.get("vlans"))
|
||||
return [Vlans(**item) for item in self._raw.get("vlans")]
|
||||
|
||||
|
||||
@@ -24,5 +22,4 @@ if __name__ == "__main__":
|
||||
with open("../../test.txt") as file:
|
||||
data = file.read()
|
||||
mikr = Mikrotik(data)
|
||||
mikr.parse()
|
||||
print(mikr)
|
||||
print(mikr.parse().json())
|
||||
|
||||
Reference in New Issue
Block a user