Enhance device interface structure and update dependencies

- Added `.idea` to `.gitignore` to exclude IDE configuration files.
- Introduced `pydantic` version `2.12.5` as a dependency in `pyproject.toml` and `uv.lock`.
- Added `annotated-types` as a new dependency in `uv.lock`.
- Refactored `BaseDevice` class to include abstract methods for device properties and loading templates.
- Created `Interfaces`, `System`, `Vlans`, and `Device` models in a new `contract.py` file for structured data handling.
- Updated `Mikrotik` class to implement the new `BaseDevice` structure and added a template loading method.
This commit is contained in:
IluaAir
2026-02-16 00:57:09 +03:00
parent 544688dae1
commit 2cfcc41e58
7 changed files with 178 additions and 3 deletions

View File

@@ -9,7 +9,12 @@ def register_parser(
name: list[str],
) -> Callable[[Type[BaseDevice]], Type[BaseDevice]]:
def wrapper(cls):
for item in name:
name_list = []
if isinstance(name, str):
name_list.append(name)
else:
name_list.extend(name)
for item in name_list:
device_registry[item.lower()] = cls
return cls

View File

@@ -1 +1,37 @@
class BaseDevice: ...
from abc import ABC, abstractmethod
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from oxi.interfaces.contract import Interfaces, System, Vlans
class BaseDevice(ABC):
@property
@abstractmethod
def template(self) -> str:
"""
:return:
"""
@abstractmethod
def vlans(self) -> "Vlans": ...
@abstractmethod
def interfaces(self) -> "Interfaces": ...
@abstractmethod
def system(self) -> "System": ...
def __load_template(self):
path = Path(__file__).parent / "template" / self.template
if not path.exists():
raise FileNotFoundError(f"Template {self.template} not found")
return path.read_text(encoding="utf-8")
def run(self):
self.load = self.__load_template()
return self.load
BaseDevice()

View File

@@ -0,0 +1,25 @@
from ipaddress import IPv4Address
from pydantic import BaseModel, Field
class Interfaces(BaseModel):
ip_address: IPv4Address
mask: int
description: str
class System(BaseModel):
model: str
serial_number: str
version: str
class Vlans(BaseModel):
id: int
name: str = Field(alias="description")
class Device(BaseModel):
system: System
interfaces: Interfaces
vlans: Vlans

View File

@@ -3,4 +3,11 @@ from oxi.interfaces.base import BaseDevice
@register_parser(["routeros", "ros", "mikrotik"])
class Mikrotik(BaseDevice): ...
class Mikrotik(BaseDevice):
template = "mikrotik.ttp"
if __name__ == "__main__":
mikr = Mikrotik()
mikr.run()
print(mikr.load)