-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
38 lines (32 loc) · 966 Bytes
/
__init__.py
File metadata and controls
38 lines (32 loc) · 966 Bytes
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
from importlib import import_module
from importlib import resources
SOLUTION = dict()
def register_solution(func):
"""Decorator to register plug-ins"""
name = func.__module__
SOLUTION[name] = func
return func
def get_all_solutions():
_import_solutions()
return SOLUTION.values()
def __getattr__(name):
"""Return a named plugin"""
try:
return SOLUTION[name]
except KeyError:
_import_solutions()
if name in SOLUTION:
return SOLUTION[name]
else:
raise AttributeError(
f"module {__name__!r} has no attribute {name!r}"
) from None
def __dir__():
"""List available plug-ins"""
_import_solutions()
return list(SOLUTION.keys())
def _import_solutions():
"""Import all resources to register plug-ins"""
for name in resources.contents(__name__):
if name.endswith(".py"):
import_module(f"{__name__}.{name[:-3]}")