mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
Improve MultiModel API
This commit is contained in:
@@ -23,7 +23,7 @@ from hyperglass.settings import Settings
|
||||
from hyperglass.exceptions.private import InputValidationError
|
||||
|
||||
# Local
|
||||
from .main import HyperglassModel, HyperglassMultiModel, HyperglassModelWithId
|
||||
from .main import MultiModel, HyperglassModel, HyperglassModelWithId
|
||||
from .fields import Action
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
@@ -315,35 +315,9 @@ class BuiltinDirective(Directive):
|
||||
DirectiveT = t.Union[BuiltinDirective, Directive]
|
||||
|
||||
|
||||
class Directives(HyperglassMultiModel[Directive]):
|
||||
class Directives(MultiModel[Directive], model=Directive, unique_by="id"):
|
||||
"""Collection of directives."""
|
||||
|
||||
def __init__(self, *items: t.Union[DirectiveT, t.Dict[str, t.Any]]) -> None:
|
||||
"""Initialize base class and validate objects."""
|
||||
super().__init__(*items, model=Directive, accessor="id")
|
||||
|
||||
def __add__(self, other: "Directives") -> "Directives":
|
||||
"""Create a new `Directives` instance by merging this instance with another."""
|
||||
valid = all(
|
||||
(
|
||||
isinstance(other, self.__class__),
|
||||
hasattr(other, "model"),
|
||||
getattr(other, "model", None) == self.model,
|
||||
),
|
||||
)
|
||||
if not valid:
|
||||
raise TypeError(f"Cannot add {other!r} to {self.__class__.__name__}")
|
||||
merged = self._merge_with(*other, unique_by=self.accessor)
|
||||
return Directives(*merged)
|
||||
|
||||
def ids(self) -> t.Tuple[str]:
|
||||
"""Get all directive IDs."""
|
||||
return tuple(sorted(directive.id for directive in self))
|
||||
|
||||
def filter_by_ids(self, *ids: str) -> "Directives":
|
||||
"""Filter directives by directive IDs."""
|
||||
return Directives(*(directive for directive in self if directive.id in ids))
|
||||
|
||||
def device_builtins(self, *, platform: str):
|
||||
"""Get builtin directives for a device."""
|
||||
return Directives(
|
||||
|
||||
Reference in New Issue
Block a user