3
3
import importlib .util
4
4
import json
5
5
import logging
6
+ import sys
6
7
from argparse import ArgumentParser
7
8
from pathlib import Path
8
9
from typing import TYPE_CHECKING , cast
@@ -68,7 +69,7 @@ def setup_cli() -> ArgumentParser:
68
69
69
70
70
71
def determine_action_scripts (
71
- plugin_dir : Path | None ,
72
+ plugin_dir : Path ,
72
73
action_scripts : list [str ] | None ,
73
74
) -> list [str ]:
74
75
"""Determine the action scripts to be loaded based on provided arguments.
@@ -89,14 +90,14 @@ def determine_action_scripts(
89
90
if action_scripts is not None :
90
91
return action_scripts
91
92
92
- # If `action_scripts` is None, then either plugin_dir has a value or it is None .
93
+ # If `action_scripts` is None, then either plugin_dir has a value or it is the default CWD .
93
94
# Thus either use the value given to plugin_value if it was given one, or fallback to using the current working directory.
94
- streamdeck_config = read_streamdeck_config_from_pyproject (plugin_dir = plugin_dir or Path . cwd () )
95
+ streamdeck_config = read_streamdeck_config_from_pyproject (plugin_dir = plugin_dir )
95
96
try :
96
97
return streamdeck_config ["action_scripts" ]
97
98
98
99
except KeyError as e :
99
- msg = f"'action_plugin' setting missing from streamdeck config in pyproject.toml in '{ args . plugin_dir } '."
100
+ msg = f"'action_plugin' setting missing from streamdeck config in pyproject.toml in '{ plugin_dir } '."
100
101
raise KeyError (msg ) from e
101
102
102
103
@@ -145,7 +146,12 @@ def read_streamdeck_config_from_pyproject(plugin_dir: Path) -> StreamDeckConfigD
145
146
146
147
class ActionLoader :
147
148
@classmethod
148
- def load_actions (cls : type [Self ], files : list [str ]) -> Generator [Action , None , None ]:
149
+ def load_actions (cls : type [Self ], plugin_dir : Path , files : list [str ]) -> Generator [Action , None , None ]:
150
+ # Ensure the parent directory of the plugin modules is in `sys.path`,
151
+ # so that import statements in the plugin module will work as expected.
152
+ if str (plugin_dir ) not in sys .path :
153
+ sys .path .insert (0 , str (plugin_dir ))
154
+
149
155
for action_script in files :
150
156
module = cls ._load_module_from_file (filepath = Path (action_script ))
151
157
yield from cls ._get_actions_from_loaded_module (module = module )
@@ -199,6 +205,9 @@ def main():
199
205
parser = setup_cli ()
200
206
args = cast (CliArgsNamespace , parser .parse_args ())
201
207
208
+ # If `plugin_dir` was not passed in as a cli option, then fall back to using the CWD.
209
+ plugin_dir = args .plugin_dir or Path .cwd ()
210
+
202
211
info = json .loads (args .info )
203
212
plugin_uuid = info ["plugin" ]["uuid" ]
204
213
@@ -207,11 +216,11 @@ def main():
207
216
configure_streamdeck_logger (name = "streamdeck" , plugin_uuid = plugin_uuid )
208
217
209
218
action_scripts = determine_action_scripts (
210
- plugin_dir = args . plugin_dir ,
219
+ plugin_dir = plugin_dir ,
211
220
action_scripts = args .action_scripts ,
212
221
)
213
222
214
- actions = list (ActionLoader .load_actions (files = action_scripts ))
223
+ actions = list (ActionLoader .load_actions (plugin_dir = plugin_dir , files = action_scripts ))
215
224
216
225
manager = PluginManager (
217
226
port = args .port ,
0 commit comments