-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path17_plugin_loading.py
More file actions
50 lines (40 loc) · 1.42 KB
/
17_plugin_loading.py
File metadata and controls
50 lines (40 loc) · 1.42 KB
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
"""Load extra ``AC_*`` commands from a plugin file and call them.
The plugin file can ship in any pip-installable package; it only
needs to define functions whose names start with ``AC_``. They land
in the same executor that drives JSON action files, so they're
immediately usable from scripts, the socket server, the scheduler,
and the visual builder.
"""
import json
from pathlib import Path
import je_auto_control as ac
PLUGIN_PATH = Path(__file__).with_name("my_plugin.py")
ACTION_PATH = Path(__file__).with_name("uses_plugin.json")
def main() -> None:
PLUGIN_PATH.write_text(
'"""Tiny plugin that adds two new AC_* commands."""\n'
'\n'
'\n'
'def AC_say_hello(name="world"):\n'
' print(f"hello, {name}!")\n'
' return {"greeted": name}\n'
'\n'
'\n'
'def AC_double(value):\n'
' return value * 2\n',
encoding="utf-8",
)
commands = ac.load_plugin_file(str(PLUGIN_PATH))
registered = ac.register_plugin_commands(commands)
print(f"registered: {registered}")
# The new commands are now first-class — drive them from a JSON action.
actions = [
["AC_say_hello", {"name": "AutoControl user"}],
["AC_double", {"value": 21}],
]
ACTION_PATH.write_text(
json.dumps(actions, indent=2), encoding="utf-8",
)
ac.execute_files([str(ACTION_PATH)])
if __name__ == "__main__":
main()