From ac835d6b56d70f8f249298be7ecc00d1231253bb Mon Sep 17 00:00:00 2001 From: IluaAir Date: Tue, 24 Feb 2026 23:19:57 +0300 Subject: [PATCH] Enhance ModelView class with iterable and indexing support - Added iterator, length, and item access methods to the `ModelView` class, allowing it to handle single models and lists more effectively. - Refactored the `vlans` method in the `Keenetic` model to restore functionality for decoding VLAN descriptions, improving data processing consistency. --- oxi/conf.py | 17 ++++++++++++++++- oxi/interfaces/models/keenetic.py | 14 +++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/oxi/conf.py b/oxi/conf.py index bfcc0d9..6c930f1 100644 --- a/oxi/conf.py +++ b/oxi/conf.py @@ -1,6 +1,6 @@ from functools import cached_property import json -from typing import TYPE_CHECKING, Generic, TypeVar +from typing import TYPE_CHECKING, Generic, Iterator, Type, TypeVar from pydantic import BaseModel @@ -24,6 +24,21 @@ class ModelView(Generic[TModel]): ) return self._model.model_dump_json(by_alias=True) + def __iter__(self) -> Iterator[TModel]: + if isinstance(self._model, list): + return iter(self._model) + raise TypeError("This view wraps a single model, not a list") + + def __len__(self) -> int: + if isinstance(self._model, list): + return len(self._model) + raise TypeError("This view wraps a single model, not a list") + + def __getitem__(self, item): + if isinstance(self._model, list): + return self._model[item] + raise TypeError("This view wraps a single model, not a list") + def __getattr__(self, item): return getattr(self._model, item) diff --git a/oxi/interfaces/models/keenetic.py b/oxi/interfaces/models/keenetic.py index 2a692d8..89ea3c7 100644 --- a/oxi/interfaces/models/keenetic.py +++ b/oxi/interfaces/models/keenetic.py @@ -33,13 +33,13 @@ class Keenetic(BaseDevice): 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 + 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__":