Skip to content

Commit

Permalink
refactor: remove load_dir for config (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Nov 16, 2024
1 parent 20afad2 commit 8906153
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
49 changes: 28 additions & 21 deletions dooit/ui/api/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,49 @@
import sys
from typing import TYPE_CHECKING
from pathlib import Path
from contextlib import contextmanager

if TYPE_CHECKING: # pragma: no cover
from .plug import PluginManager


def register(api: "PluginManager", path: Path) -> None:
spec = importlib.util.spec_from_file_location("", path)
@contextmanager
def temporary_sys_path(path: Path):
"""Context manager to temporarily add a directory to sys.path."""

if spec and spec.loader:
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
for obj in vars(foo).values():
api.register(obj)
# Temporarily add the parent directory to the path
# to allow imports from other files in the same directory

parent_path = str(path.parent.absolute())
if parent_path not in sys.path:
sys.path.insert(0, parent_path)
try:
yield
finally:
if parent_path in sys.path:
sys.path.remove(parent_path)

def load_file(api: "PluginManager", path: Path) -> bool:
if not path.exists():
return False

if path.suffix == ".py":
register(api, path)
def register(api: "PluginManager", path: Path) -> None:
module_name = f"dynamic_{path.stem}"
spec = importlib.util.spec_from_file_location(module_name, path)

return True
if spec and spec.loader:
parent_path = str(path.parent.absolute())
sys.path.append(parent_path)

with temporary_sys_path(path):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for obj in vars(module).values():
api.register(obj)

def load_dir(api: "PluginManager", path: Path) -> bool:
# allows users to import from the directory
sys.path.append(str(path.resolve()))

def load_file(api: "PluginManager", path: Path) -> bool:
if not path.exists():
return False

for file in path.iterdir():
if file.is_dir():
return load_dir(api, file)

load_file(api, file)
if path.suffix == ".py":
register(api, path)

return True
4 changes: 2 additions & 2 deletions dooit/ui/api/plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from dooit.ui.api.event_handlers import DOOIT_EVENT_ATTR, DOOIT_TIMER_ATTR
from dooit.ui.api.events import DooitEvent
from .loader import load_dir, load_file
from .loader import load_file

if TYPE_CHECKING: # pragma: no cover
from dooit.ui.api.dooit_api import DooitAPI
Expand All @@ -33,7 +33,7 @@ def __init__(self, api: "DooitAPI") -> None:

def scan(self):
load_file(self, DEFAULT_CONFIG)
load_dir(self, CONFIG_FOLDER)
load_file(self, CONFIG_FOLDER / "config.py")

def _update_dooit_value(self, obj, *params):
res = obj(self.api, *params)
Expand Down
11 changes: 0 additions & 11 deletions tests/test_ui/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,3 @@ async def test_loader():

incorrect_path = temp_folder / "incorrect_path"
assert not loader.load_file(app.api.plugin_manager, incorrect_path)

incorrect_folder = temp_folder / "incorrect_folder"
assert not loader.load_dir(app.api.plugin_manager, incorrect_path)

subfolder = incorrect_folder / "subfolder"
test_file = subfolder / "test_file.py"

subfolder.mkdir(parents=True, exist_ok=True)
test_file.touch()

assert loader.load_dir(app.api.plugin_manager, incorrect_folder)

0 comments on commit 8906153

Please sign in to comment.