mirror of
https://github.com/thatmattlove/hyperglass.git
synced 2026-01-17 08:48:05 +00:00
Copy plugins to module instead of kludgy import
This commit is contained in:
parent
3c073878fa
commit
8013c48ae9
5 changed files with 13 additions and 9 deletions
|
|
@ -16,6 +16,7 @@ InputPluginReturn = t.Union[None, bool]
|
|||
class InputPlugin(HyperglassPlugin, DirectivePlugin):
|
||||
"""Plugin to validate user input prior to running commands."""
|
||||
|
||||
_type = "input"
|
||||
failure_reason: t.Optional[str] = None
|
||||
|
||||
def validate(self, query: "Query") -> InputPluginReturn:
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ OutputType = Union["OutputDataModel", Series[str]]
|
|||
class OutputPlugin(HyperglassPlugin, DirectivePlugin, PlatformPlugin):
|
||||
"""Plugin to interact with device command output."""
|
||||
|
||||
_type = "output"
|
||||
|
||||
def process(self, *, output: OutputType, query: "Query") -> OutputType:
|
||||
"""Process or manipulate output from a device."""
|
||||
log.warning("Output plugin '{}' has not implemented a 'process()' method", self.name)
|
||||
|
|
|
|||
4
hyperglass/plugins/external/.gitignore
vendored
Normal file
4
hyperglass/plugins/external/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Ignore all files as they're imported at runtime.
|
||||
*
|
||||
!__init__.py
|
||||
!.gitignore
|
||||
0
hyperglass/plugins/external/__init__.py
vendored
Normal file
0
hyperglass/plugins/external/__init__.py
vendored
Normal file
|
|
@ -2,22 +2,18 @@
|
|||
|
||||
# Standard Library
|
||||
import sys
|
||||
import shutil
|
||||
import typing as t
|
||||
from inspect import isclass, getmembers
|
||||
from pathlib import Path
|
||||
from importlib.util import module_from_spec, spec_from_file_location
|
||||
|
||||
# Project
|
||||
from hyperglass.log import log
|
||||
|
||||
# Local
|
||||
from . import _builtin
|
||||
from ._input import InputPlugin
|
||||
from ._output import OutputPlugin
|
||||
from ._manager import InputPluginManager, OutputPluginManager
|
||||
|
||||
_PLUGIN_GLOBALS = {"InputPlugin": InputPlugin, "OutputPlugin": OutputPlugin, "log": log}
|
||||
|
||||
|
||||
def _is_class(module: t.Any, obj: object) -> bool:
|
||||
if isclass(obj):
|
||||
|
|
@ -50,11 +46,12 @@ def _register_from_module(module: t.Any, **kwargs: t.Any) -> t.Tuple[str, ...]:
|
|||
|
||||
def _module_from_file(file: Path) -> t.Any:
|
||||
"""Import a plugin module from its file Path object."""
|
||||
name = file.name.split(".")[0]
|
||||
spec = spec_from_file_location(f"hyperglass.plugins.external.{name}", file)
|
||||
plugins_dir = Path(__file__).parent / "external"
|
||||
dst = plugins_dir / f"imported_{file.name}"
|
||||
shutil.copy2(file, dst)
|
||||
name = f"imported_{file.name.split('.')[0]}"
|
||||
spec = spec_from_file_location(f"hyperglass.plugins.external.{name}", dst)
|
||||
module = module_from_spec(spec)
|
||||
for k, v in _PLUGIN_GLOBALS.items():
|
||||
setattr(module, k, v)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue