load only required plugins

This commit is contained in:
Devaev Maxim
2019-04-28 21:01:03 +03:00
parent 26338c5acf
commit e13b5027d6
7 changed files with 22 additions and 38 deletions

View File

@@ -22,7 +22,6 @@
import importlib
import functools
import os
from typing import Dict
from typing import Type
@@ -38,34 +37,23 @@ class UnknownPluginError(Exception):
# =====
class BasePlugin:
PLUGIN_NAME: str = ""
def __init__(self, **_: Any) -> None:
pass # pragma: nocover
@classmethod
def get_options(cls) -> Dict[str, Option]:
def get_plugin_name(cls) -> str:
name = cls.__module__
return name[name.rindex(".") + 1:]
@classmethod
def get_plugin_options(cls) -> Dict[str, Option]:
return {} # pragma: nocover
# =====
def get_plugin_class(sub: str, name: str) -> Type[BasePlugin]:
classes = _get_plugin_classes(sub)
try:
return classes[name]
except KeyError:
raise UnknownPluginError("Unknown plugin '%s/%s'" % (sub, name))
# =====
@functools.lru_cache()
def _get_plugin_classes(sub: str) -> Dict[str, Type[BasePlugin]]:
classes: Dict[str, Type[BasePlugin]] = {} # noqa: E701
sub_path = os.path.join(os.path.dirname(__file__), sub)
for file_name in os.listdir(sub_path):
if not file_name.startswith("__") and file_name.endswith(".py"):
module_name = file_name[:-3]
module = importlib.import_module("kvmd.plugins.{}.{}".format(sub, module_name))
plugin_class = getattr(module, "Plugin")
classes[plugin_class.PLUGIN_NAME] = plugin_class
return classes
def get_plugin_class(sub: str, name: str) -> Type[BasePlugin]:
try:
module = importlib.import_module("kvmd.plugins.{}.{}".format(sub, name))
except ModuleNotFoundError:
raise UnknownPluginError("Unknown plugin '%s/%s'" % (sub, name))
return getattr(module, "Plugin")

View File

@@ -33,13 +33,11 @@ from . import BaseAuthService
# =====
class Plugin(BaseAuthService):
PLUGIN_NAME = "htpasswd"
def __init__(self, path: str) -> None: # pylint: disable=super-init-not-called
self.__path = path
@classmethod
def get_options(cls) -> Dict[str, Option]:
def get_plugin_options(cls) -> Dict[str, Option]:
return {
"file": Option("/etc/kvmd/htpasswd", type=valid_abs_path_exists, unpack_as="path"),
}

View File

@@ -40,8 +40,6 @@ from . import BaseAuthService
# =====
class Plugin(BaseAuthService):
PLUGIN_NAME = "http"
def __init__( # pylint: disable=super-init-not-called
self,
url: str,
@@ -60,7 +58,7 @@ class Plugin(BaseAuthService):
self.__http_session: Optional[aiohttp.ClientSession] = None
@classmethod
def get_options(cls) -> Dict[str, Option]:
def get_plugin_options(cls) -> Dict[str, Option]:
return {
"url": Option("http://localhost/auth"),
"verify": Option(True, type=valid_bool),