-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_loader.py
51 lines (30 loc) · 969 Bytes
/
plugin_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import importlib
from pathlib import Path
from typing import TypeAlias
import config
from tag import PluginTag
PluginName: TypeAlias = str
class MissingTags(Exception):
def __init__(self, tags: frozenset[PluginTag]):
self.tags = tags
class PluginNotFound(Exception):
def __init__(self, name: PluginName):
self.name = name
class Plugin:
tags: frozenset[PluginTag]
@staticmethod
def initialize(): ...
def _get_plugin_path(name: PluginName) -> str:
return f"{config.plugins_package}.{name}"
def _get_plugin(name: PluginName) -> Plugin:
try:
return importlib.import_module(_get_plugin_path(name)) #type: ignore
except ModuleNotFoundError as e:
raise PluginNotFound(name) from e
def assert_tags(existing: frozenset[PluginTag], required: frozenset[PluginTag]):
if (missing := required - existing):
raise MissingTags(missing)
def load_plugin(name: PluginName) -> Plugin:
plugin = _get_plugin(name)
plugin.initialize()
return plugin