Skip to content

Commit b08b731

Browse files
committed
Fix to add plugin directory to system path
1 parent 0f24dac commit b08b731

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

streamdeck/__main__.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import importlib.util
44
import json
55
import logging
6+
import sys
67
from argparse import ArgumentParser
78
from pathlib import Path
89
from typing import TYPE_CHECKING, cast
@@ -68,7 +69,7 @@ def setup_cli() -> ArgumentParser:
6869

6970

7071
def determine_action_scripts(
71-
plugin_dir: Path | None,
72+
plugin_dir: Path,
7273
action_scripts: list[str] | None,
7374
) -> list[str]:
7475
"""Determine the action scripts to be loaded based on provided arguments.
@@ -89,14 +90,14 @@ def determine_action_scripts(
8990
if action_scripts is not None:
9091
return action_scripts
9192

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.
9394
# 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)
9596
try:
9697
return streamdeck_config["action_scripts"]
9798

9899
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}'."
100101
raise KeyError(msg) from e
101102

102103

@@ -145,7 +146,12 @@ def read_streamdeck_config_from_pyproject(plugin_dir: Path) -> StreamDeckConfigD
145146

146147
class ActionLoader:
147148
@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+
149155
for action_script in files:
150156
module = cls._load_module_from_file(filepath=Path(action_script))
151157
yield from cls._get_actions_from_loaded_module(module=module)
@@ -199,6 +205,9 @@ def main():
199205
parser = setup_cli()
200206
args = cast(CliArgsNamespace, parser.parse_args())
201207

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+
202211
info = json.loads(args.info)
203212
plugin_uuid = info["plugin"]["uuid"]
204213

@@ -207,11 +216,11 @@ def main():
207216
configure_streamdeck_logger(name="streamdeck", plugin_uuid=plugin_uuid)
208217

209218
action_scripts = determine_action_scripts(
210-
plugin_dir=args.plugin_dir,
219+
plugin_dir=plugin_dir,
211220
action_scripts=args.action_scripts,
212221
)
213222

214-
actions = list(ActionLoader.load_actions(files=action_scripts))
223+
actions = list(ActionLoader.load_actions(plugin_dir=plugin_dir, files=action_scripts))
215224

216225
manager = PluginManager(
217226
port=args.port,

0 commit comments

Comments
 (0)