Skip to content

Commit 515197e

Browse files
committed
feat(hot_reload): enhance extension loading by checking parent directory modules
- Added functionality to check for a setup function in the parent directory's module file, improving the extension loading process. - Introduced logging for found modules to aid in debugging and traceability of loaded extensions.
1 parent 9fe8f89 commit 515197e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/tux/services/hot_reload/file_utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def path_from_extension(extension: str, *, base_dir: Path | None = None) -> Path
2929
return base_dir / Path(*parts[1:]) / f"{parts[-1]}.py"
3030

3131

32-
def get_extension_from_path(file_path: Path, base_dir: Path) -> str | None:
32+
def get_extension_from_path(file_path: Path, base_dir: Path) -> str | None: # noqa: PLR0911
3333
"""
3434
Convert file path to extension name.
3535
@@ -68,6 +68,7 @@ def get_extension_from_path(file_path: Path, base_dir: Path) -> str | None:
6868
# Check parent directory for cog (for supporting files in subdirs)
6969
if len(parts) > 1:
7070
parent_module_name = "tux." + ".".join(parts[:-1])
71+
parent_dir_name = parts[-2] # Name of the parent directory
7172

7273
# Try parent's __init__.py for setup
7374
with suppress(ImportError, AttributeError):
@@ -83,6 +84,17 @@ def get_extension_from_path(file_path: Path, base_dir: Path) -> str | None:
8384
logger.trace(f"Found cog.py: {parent_module_name}.cog")
8485
return f"{parent_module_name}.cog"
8586

87+
# Try {parent_dir_name}.py in parent directory (e.g., info.py for info module)
88+
with suppress(ImportError, AttributeError):
89+
module_file = importlib.import_module(
90+
f"{parent_module_name}.{parent_dir_name}",
91+
)
92+
if hasattr(module_file, "setup") and callable(module_file.setup):
93+
logger.trace(
94+
f"Found {parent_dir_name}.py: {parent_module_name}.{parent_dir_name}",
95+
)
96+
return f"{parent_module_name}.{parent_dir_name}"
97+
8698
logger.trace(f"Not a loadable extension: {module_name}")
8799
return None
88100

0 commit comments

Comments
 (0)