mirror of
https://github.com/drygdryg/netbox-plugin-interface-sync
synced 2024-11-29 18:20:52 +03:00
35 lines
968 B
Python
35 lines
968 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
|
||
|
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))
|