Add VLAN range expansion utility and corresponding unit tests
- Introduced a new utility function `expand_vlan_range` in `utils.py` to expand VLAN range strings into individual VLAN IDs. - Created a new test file `test_units.py` with unit tests for the `expand_vlan_range` function, covering various scenarios including simple ranges, reversed ranges, non-numeric inputs, and list inputs. - Enhanced test coverage for other functionalities in the `device_registry` and template validation classes.
This commit is contained in:
25
oxi/interfaces/utils.py
Normal file
25
oxi/interfaces/utils.py
Normal file
@@ -0,0 +1,25 @@
|
||||
def expand_vlan_range(value: str | list[str]) -> list[str]:
|
||||
"""Expand values like '1,7,14-15' into individual VLAN IDs."""
|
||||
if isinstance(value, list):
|
||||
value = ",".join(str(item) for item in value)
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user