Add VLAN range expansion utility and improve VLAN processing in Qtech model

- Introduced a new helper function `_expand_vlan_range` to convert VLAN range strings into a list of individual VLAN IDs.
- Enhanced the `vlans` method in the `Qtech` class to utilize the new function, improving the handling of VLAN IDs and ensuring proper processing of both individual and range-based VLAN inputs.
This commit is contained in:
IluaAir
2026-05-25 15:41:50 +03:00
parent 74647bea5b
commit e8c33b0e64

View File

@@ -3,6 +3,30 @@ from oxi.interfaces import register_parser
from oxi.interfaces.base import BaseDevice from oxi.interfaces.base import BaseDevice
def _expand_vlan_range(value: str) -> list[str]:
"""Разворачивает строку вида '1,7,14-15,200-205' в список ['1','7','14','15',...]."""
result: list[str] = []
if not value:
return result
for part in value.split(","):
part = part.strip()
if not part:
continue
if "-" in part:
start_s, end_s = part.split("-", 1)
try:
start, end = int(start_s), int(end_s)
except ValueError:
result.append(part)
continue
if start > end:
start, end = end, start
result.extend(str(i) for i in range(start, end + 1))
else:
result.append(part)
return result
@register_parser(["QTECH"]) @register_parser(["QTECH"])
class Qtech(BaseDevice): class Qtech(BaseDevice):
template = "qtech.ttp" template = "qtech.ttp"
@@ -13,19 +37,20 @@ class Qtech(BaseDevice):
def vlans(self) -> list[dict]: def vlans(self) -> list[dict]:
vlans_ttp = self.raw["vlans"] vlans_ttp = self.raw["vlans"]
vlans = [] vlans: list[dict] = []
named_vlan = set() named_vlan: set[str] = set()
for item in vlans_ttp: for item in vlans_ttp:
if item.get("vlan_id"): vlan_id = item.get("vlan_id")
named_vlan.add(item.get("vlan_id")) if vlan_id and "," not in vlan_id and "-" not in vlan_id:
named_vlan.add(vlan_id)
vlans.append(item) vlans.append(item)
else: continue
ids = item.get("vlan_ids", "")
ids = item.get("vlan_ids") or vlan_id or ""
tail = item.get("vlan_tail") tail = item.get("vlan_tail")
if tail: if tail:
ids = f"{ids},{tail}" ids = f"{ids},{tail}"
for vid in ids.split(","): for vid in _expand_vlan_range(ids):
vid = vid.strip()
if vid in named_vlan: if vid in named_vlan:
continue continue
vlans.append({"vlan_id": vid}) vlans.append({"vlan_id": vid})
@@ -39,3 +64,8 @@ if __name__ == "__main__":
qtech = Qtech(data) qtech = Qtech(data)
qt = qtech.parse() qt = qtech.parse()
print(qt) print(qt)
with open("./test3-1.txt") as file:
data = file.read()
qtech = Qtech(data)
qt = qtech.parse()
print(qt)