2021-09-11 11:17:38 -07:00
|
|
|
"""Base Plugin Definition."""
|
|
|
|
|
|
|
|
# Standard Library
|
|
|
|
from abc import ABC
|
2021-09-12 15:06:34 -07:00
|
|
|
from typing import Any, Union, Literal, TypeVar
|
2021-09-11 11:17:38 -07:00
|
|
|
from inspect import Signature
|
|
|
|
|
|
|
|
# Third Party
|
2021-09-12 15:06:34 -07:00
|
|
|
from pydantic import BaseModel, PrivateAttr
|
2021-09-11 11:17:38 -07:00
|
|
|
|
|
|
|
PluginType = Union[Literal["output"], Literal["input"]]
|
2021-09-12 15:06:34 -07:00
|
|
|
SupportedMethod = TypeVar("SupportedMethod")
|
2021-09-11 11:17:38 -07:00
|
|
|
|
|
|
|
|
|
|
|
class HyperglassPlugin(BaseModel, ABC):
|
|
|
|
"""Plugin to interact with device command output."""
|
|
|
|
|
2021-09-12 15:06:34 -07:00
|
|
|
__hyperglass_builtin__: bool = PrivateAttr(False)
|
2021-09-11 11:17:38 -07:00
|
|
|
name: str
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _signature(self) -> Signature:
|
|
|
|
"""Get this instance's class signature."""
|
|
|
|
return self.__class__.__signature__
|
|
|
|
|
2021-09-12 15:06:34 -07:00
|
|
|
def __eq__(self, other: "HyperglassPlugin") -> bool:
|
2021-09-11 11:17:38 -07:00
|
|
|
"""Other plugin is equal to this plugin."""
|
2021-09-12 15:06:34 -07:00
|
|
|
if hasattr(other, "_signature"):
|
|
|
|
return other and self._signature == other._signature
|
|
|
|
return False
|
2021-09-11 11:17:38 -07:00
|
|
|
|
2021-09-12 15:06:34 -07:00
|
|
|
def __ne__(self, other: "HyperglassPlugin") -> bool:
|
2021-09-11 11:17:38 -07:00
|
|
|
"""Other plugin is not equal to this plugin."""
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
def __hash__(self) -> int:
|
|
|
|
"""Create a hashed representation of this plugin's name."""
|
|
|
|
return hash(self._signature)
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
"""Represent plugin by its name."""
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __init_subclass__(cls, **kwargs: Any) -> None:
|
|
|
|
"""Initialize plugin object."""
|
|
|
|
name = kwargs.pop("name", None) or cls.__name__
|
|
|
|
cls._name = name
|
|
|
|
super().__init_subclass__()
|
|
|
|
|
|
|
|
def __init__(self, **kwargs: Any) -> None:
|
|
|
|
"""Initialize plugin instance."""
|
|
|
|
name = kwargs.pop("name", None) or self.__class__.__name__
|
|
|
|
super().__init__(name=name, **kwargs)
|