3 Commits

Author SHA1 Message Date
Wouter de Bruijn
fd66a4c943 🚨 Fixed Unnecessary ellipsis constant 2025-12-19 09:50:01 +01:00
Wouter de Bruijn
fdaeb79d4d 🐛 Removed deprecation decorator because of unavailability in Python 3.12 2025-12-19 09:47:47 +01:00
Wouter de Bruijn
765b4713a6 🐛 Changed tag sorting to tag name and value 2025-12-19 09:24:14 +01:00
2 changed files with 33 additions and 3 deletions

View File

@@ -869,8 +869,10 @@ class PhysicalDevice:
# Check host tags # Check host tags
if config["tag_sync"]: if config["tag_sync"]:
if remove_duplicates(host["tags"], sortkey="tag") == remove_duplicates( if remove_duplicates(
self.tags, sortkey="tag" host["tags"], lambda tag: f"{tag['tag']}{tag['value']}"
) == remove_duplicates(
self.tags, lambda tag: f"{tag['tag']}{tag['value']}"
): ):
self.logger.debug("Host %s: Tags in-sync.", self.name) self.logger.debug("Host %s: Tags in-sync.", self.name)
else: else:

View File

@@ -1,5 +1,6 @@
"""A collection of tools used by several classes""" """A collection of tools used by several classes"""
from typing import Any, Callable, Optional, overload
from modules.exceptions import HostgroupError from modules.exceptions import HostgroupError
@@ -108,15 +109,42 @@ def field_mapper(host, mapper, nbdevice, logger):
return data return data
def remove_duplicates(input_list, sortkey=None): @overload
def remove_duplicates(
input_list: list[dict[Any, Any]],
sortkey: Optional[str | Callable[[dict[str, Any]], str]] = None,
): ...
@overload
def remove_duplicates(
input_list: dict[Any, Any],
sortkey: Optional[str | Callable[[dict[str, Any]], str]] = None,
):
"""
deprecated: input_list as dict is deprecated, use list of dicts instead
"""
def remove_duplicates(
input_list: list[dict[Any, Any]] | dict[Any, Any],
sortkey: Optional[str | Callable[[dict[str, Any]], str]] = None,
):
""" """
Removes duplicate entries from a list and sorts the list Removes duplicate entries from a list and sorts the list
sortkey: Optional; key to sort the list on. Can be a string or a callable function.
""" """
output_list = [] output_list = []
if isinstance(input_list, list): if isinstance(input_list, list):
output_list = [dict(t) for t in {tuple(d.items()) for d in input_list}] output_list = [dict(t) for t in {tuple(d.items()) for d in input_list}]
if sortkey and isinstance(sortkey, str): if sortkey and isinstance(sortkey, str):
output_list.sort(key=lambda x: x[sortkey]) output_list.sort(key=lambda x: x[sortkey])
elif sortkey and callable(sortkey):
output_list.sort(key=sortkey)
return output_list return output_list