mirror of
https://github.com/drygdryg/netbox-plugin-interface-sync
synced 2024-11-25 18:10:52 +03:00
36 lines
991 B
Python
36 lines
991 B
Python
import re
|
|
from typing import Iterable
|
|
from dataclasses import dataclass
|
|
|
|
|
|
def split(s):
|
|
for x, y in re.findall(r'(\d*)(\D*)', s):
|
|
yield '', int(x or '0')
|
|
yield y, 0
|
|
|
|
|
|
def natural_keys(c):
|
|
return tuple(split(c))
|
|
|
|
|
|
def human_sorted(iterable: Iterable):
|
|
return sorted(iterable, key=natural_keys)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UnifiedInterface:
|
|
"""A unified way to represent the interface and interface template"""
|
|
id: int
|
|
name: str
|
|
type: str
|
|
type_display: str
|
|
is_template: bool = False
|
|
|
|
def __eq__(self, other):
|
|
# Ignore some fields when comparing; ignore interface name case and whitespaces
|
|
return (self.name.lower().replace(' ', '') == other.name.lower().replace(' ', '')) and (self.type == other.type)
|
|
|
|
def __hash__(self):
|
|
# Ignore some fields when hashing; ignore interface name case and whitespaces
|
|
return hash((self.name.lower().replace(' ', ''), self.type))
|