From dda73cb37048c8c616cefa81aaea0979f037fadd Mon Sep 17 00:00:00 2001 From: thatmattlove Date: Thu, 16 Sep 2021 17:12:30 -0700 Subject: [PATCH] Refactor and restructure directive model --- hyperglass/configuration/main.py | 2 +- hyperglass/execution/drivers/_construct.py | 2 +- hyperglass/models/config/devices.py | 2 +- .../models/{commands/generic.py => directive.py} | 6 +++--- hyperglass/plugins/_manager.py | 2 +- hyperglass/plugins/main.py | 10 +++++----- 6 files changed, 12 insertions(+), 12 deletions(-) rename hyperglass/models/{commands/generic.py => directive.py} (98%) diff --git a/hyperglass/configuration/main.py b/hyperglass/configuration/main.py index 8fca01f..4d5a3af 100644 --- a/hyperglass/configuration/main.py +++ b/hyperglass/configuration/main.py @@ -16,10 +16,10 @@ from hyperglass.settings import Settings from hyperglass.constants import PARSED_RESPONSE_FIELDS, __version__ from hyperglass.models.ui import UIParameters from hyperglass.util.files import check_path +from hyperglass.models.directive import Directive from hyperglass.exceptions.private import ConfigError, ConfigMissing from hyperglass.models.config.params import Params from hyperglass.models.config.devices import Devices -from hyperglass.models.commands.generic import Directive # Local from .markdown import get_markdown diff --git a/hyperglass/execution/drivers/_construct.py b/hyperglass/execution/drivers/_construct.py index 9d5efba..42fbebf 100644 --- a/hyperglass/execution/drivers/_construct.py +++ b/hyperglass/execution/drivers/_construct.py @@ -20,8 +20,8 @@ from hyperglass.exceptions.private import ConfigError if t.TYPE_CHECKING: # Project from hyperglass.models.api.query import Query + from hyperglass.models.directive import Directive from hyperglass.models.config.devices import Device - from hyperglass.models.commands.generic import Directive class Construct: diff --git a/hyperglass/models/config/devices.py b/hyperglass/models/config/devices.py index 65b18ce..4f3b939 100644 --- a/hyperglass/models/config/devices.py +++ b/hyperglass/models/config/devices.py @@ -29,8 +29,8 @@ from .proxy import Proxy from .params import Params from ..fields import SupportedDriver from .network import Network +from ..directive import Directive from .credential import Credential -from ..commands.generic import Directive class Device(HyperglassModelWithId, extra="allow"): diff --git a/hyperglass/models/commands/generic.py b/hyperglass/models/directive.py similarity index 98% rename from hyperglass/models/commands/generic.py rename to hyperglass/models/directive.py index 9a1ceda..a295348 100644 --- a/hyperglass/models/commands/generic.py +++ b/hyperglass/models/directive.py @@ -22,12 +22,12 @@ from hyperglass.settings import Settings from hyperglass.exceptions.private import InputValidationError # Local -from ..main import HyperglassModel, HyperglassModelWithId -from ..fields import Action +from .main import HyperglassModel, HyperglassModelWithId +from .fields import Action if t.TYPE_CHECKING: # Local - from ..config.params import Params + from .config.params import Params IPv4PrefixLength = conint(ge=0, le=32) IPv6PrefixLength = conint(ge=0, le=128) diff --git a/hyperglass/plugins/_manager.py b/hyperglass/plugins/_manager.py index 66b381f..ccdd319 100644 --- a/hyperglass/plugins/_manager.py +++ b/hyperglass/plugins/_manager.py @@ -18,8 +18,8 @@ if t.TYPE_CHECKING: # Project from hyperglass.state import HyperglassState from hyperglass.models.api.query import Query + from hyperglass.models.directive import Directive from hyperglass.models.config.devices import Device - from hyperglass.models.commands.generic import Directive PluginT = t.TypeVar("PluginT", bound=HyperglassPlugin) diff --git a/hyperglass/plugins/main.py b/hyperglass/plugins/main.py index 66905f4..e6c3c40 100644 --- a/hyperglass/plugins/main.py +++ b/hyperglass/plugins/main.py @@ -2,7 +2,7 @@ # Standard Library import sys -from typing import Any, Tuple +import typing as t from inspect import isclass, getmembers from pathlib import Path from importlib.util import module_from_spec, spec_from_file_location @@ -19,7 +19,7 @@ from ._manager import InputPluginManager, OutputPluginManager _PLUGIN_GLOBALS = {"InputPlugin": InputPlugin, "OutputPlugin": OutputPlugin, "log": log} -def _is_class(module: Any, obj: object) -> bool: +def _is_class(module: t.Any, obj: object) -> bool: if isclass(obj): # Get the object's containing module name. obj_module_name: str = getattr(obj, "__module__", "") @@ -30,7 +30,7 @@ def _is_class(module: Any, obj: object) -> bool: return False -def _register_from_module(module: Any, **kwargs: Any) -> Tuple[str, ...]: +def _register_from_module(module: t.Any, **kwargs: t.Any) -> t.Tuple[str, ...]: """Register defined classes from the module.""" failures = () defs = getmembers(module, lambda o: _is_class(module, o)) @@ -48,7 +48,7 @@ def _register_from_module(module: Any, **kwargs: Any) -> Tuple[str, ...]: return failures -def _module_from_file(file: Path) -> Any: +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) @@ -64,7 +64,7 @@ def init_builtin_plugins() -> None: _register_from_module(_builtin) -def register_plugin(plugin_file: Path, **kwargs) -> Tuple[str, ...]: +def register_plugin(plugin_file: Path, **kwargs) -> t.Tuple[str, ...]: """Register an external plugin by file path.""" if plugin_file.exists(): module = _module_from_file(plugin_file)