plugins.py 2.15 KB
Newer Older
mashun1's avatar
veros  
mashun1 committed
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from collections import namedtuple

from veros.variables import Variable
from veros.settings import Setting

VerosPlugin = namedtuple(
    "VerosPlugin",
    [
        "name",
        "module",
        "setup_entrypoint",
        "run_entrypoint",
        "settings",
        "variables",
        "dimensions",
        "diagnostics",
    ],
)


def load_plugin(module):
    from veros.diagnostics.base import VerosDiagnostic

    modname = module.__name__

    if not hasattr(module, "__VEROS_INTERFACE__"):
        raise RuntimeError(f"module {modname} is not a valid Veros plugin")

    interface = module.__VEROS_INTERFACE__

    setup_entrypoint = interface.get("setup_entrypoint")

    if not callable(setup_entrypoint):
        raise RuntimeError(f"module {modname} is missing a valid setup entrypoint")

    run_entrypoint = interface.get("run_entrypoint")

    if not callable(run_entrypoint):
        raise RuntimeError(f"module {modname} is missing a valid run entrypoint")

    name = interface.get("name", modname)

    settings = interface.get("settings", {})
    for setting, val in settings.items():
        if not isinstance(val, Setting):
            raise TypeError(f"got unexpected type {type(val)} for setting {setting}")

    variables = interface.get("variables", {})
    for variable, val in variables.items():
        if not isinstance(val, Variable):
            raise TypeError(f"got unexpected type {type(val)} for variable {variable}")

    dimensions = interface.get("dimensions", {})
    for dim, val in dimensions.items():
        if not isinstance(val, (str, int)):
            raise TypeError(f"got unexpected type {type(val)} for dimension {dim}")

    diagnostics = interface.get("diagnostics", [])
    for diagnostic in diagnostics:
        if not issubclass(diagnostic, VerosDiagnostic):
            raise TypeError(f"got unexpected type {type(diagnostic)} for diagnostic {diagnostic}")

    return VerosPlugin(
        name=name,
        module=module,
        setup_entrypoint=setup_entrypoint,
        run_entrypoint=run_entrypoint,
        settings=settings,
        variables=variables,
        dimensions=dimensions,
        diagnostics=diagnostics,
    )