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:
@@ -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,22 +37,23 @@ 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", "")
|
|
||||||
tail = item.get("vlan_tail")
|
ids = item.get("vlan_ids") or vlan_id or ""
|
||||||
if tail:
|
tail = item.get("vlan_tail")
|
||||||
ids = f"{ids},{tail}"
|
if tail:
|
||||||
for vid in ids.split(","):
|
ids = f"{ids},{tail}"
|
||||||
vid = vid.strip()
|
for vid in _expand_vlan_range(ids):
|
||||||
if vid in named_vlan:
|
if vid in named_vlan:
|
||||||
continue
|
continue
|
||||||
vlans.append({"vlan_id": vid})
|
vlans.append({"vlan_id": vid})
|
||||||
return vlans
|
return vlans
|
||||||
|
|
||||||
|
|
||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user