Skip to content

Commit 291421c

Browse files
feat: Enhance device ID fetching with natural sorting
1 parent 0fa0bbd commit 291421c

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/videoipath_automation_tool/apps/inventory/inventory_api.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from videoipath_automation_tool.apps.inventory.model.inventory_request_rpc import InventoryRequestRpc
1919
from videoipath_automation_tool.connector.models.response_rpc import ResponseRPC
2020
from videoipath_automation_tool.connector.vip_connector import VideoIPathConnector
21-
from videoipath_automation_tool.utils.cross_app_utils import create_fallback_logger
21+
from videoipath_automation_tool.utils.cross_app_utils import create_fallback_logger, extract_natural_sort_key
2222
from videoipath_automation_tool.validators.device_id import validate_device_id
2323

2424

@@ -372,16 +372,27 @@ def fetch_device_ids_list(self) -> List[str]:
372372
return [device["_id"] for device in response.data["config"]["devman"]["devices"]["_items"]]
373373

374374
def fetch_device_ids_by_driver(self, driver: DriverLiteral) -> List[str]:
375-
"""Method to fetch all device ids by driver id from VideoIPath-Inventory"""
375+
"""Fetch all device IDs by driver ID from VideoIPath-Inventory with natural sorting."""
376376
driver_organization, driver_name, driver_version = extract_driver_info_from_id(driver_id=driver)
377+
377378
escaped_driver_organization = urllib.parse.quote(driver_organization, safe="")
378379
escaped_driver_name = urllib.parse.quote(driver_name, safe="")
379380
escaped_driver_version = urllib.parse.quote(driver_version, safe="")
380-
url_path = f"/rest/v2/data/config/devman/devices/* where (config.driver.name='{escaped_driver_name}' and config.driver.version='{escaped_driver_version}' and config.driver.organization='{escaped_driver_organization}') /**"
381+
382+
url_path = (
383+
f"/rest/v2/data/config/devman/devices/* "
384+
f"where (config.driver.name='{escaped_driver_name}' "
385+
f"and config.driver.version='{escaped_driver_version}' "
386+
f"and config.driver.organization='{escaped_driver_organization}') /**"
387+
)
388+
381389
response = self.vip_connector.rest.get(url_path)
390+
382391
if not response.data:
383392
raise ValueError("Response data is empty.")
384-
return [device["_id"] for device in response.data["config"]["devman"]["devices"]["_items"]]
393+
394+
device_ids = [device["_id"] for device in response.data["config"]["devman"]["devices"]["_items"]]
395+
return sorted(device_ids, key=extract_natural_sort_key)
385396

386397
# --- Bulk Device Label Fetching Methods ---
387398
def fetch_devices_factory_labels_as_dict(self) -> dict[str, str]:

src/videoipath_automation_tool/utils/cross_app_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import re
23
import uuid
34

45
from typing_extensions import deprecated
@@ -19,6 +20,11 @@ def generate_uuid_4():
1920
return str(uuid.uuid4())
2021

2122

23+
# --- Natural Sort Device ID list ---
24+
def extract_natural_sort_key(s: str):
25+
return [int(text) if text.isdigit() else text.lower() for text in re.split(r"(\d+)", s)]
26+
27+
2228
# --- Deprecated Functions for Device ID Validation ---
2329
# --- Device ID string validation ---
2430
@deprecated("Use 'validate_device_id' from 'videoipath_automation_tool.validators.device_id' instead.")

0 commit comments

Comments
 (0)