-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ split userland module loading into utility function
- Loading branch information
Showing
2 changed files
with
33 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Scripting utilities""" | ||
|
||
# typing | ||
from types import ModuleType | ||
|
||
# stdlib | ||
from importlib.machinery import ModuleSpec, PathFinder | ||
|
||
# local | ||
from .configuration import get_config | ||
|
||
|
||
def load_userland_module(name: str) -> ModuleType | None: | ||
"""Load module from userland scripts""" | ||
|
||
pathfinder = PathFinder() | ||
paths = get_config("ssh.userland.paths") | ||
split: list[str] = name.split(".") | ||
found: ModuleSpec | None = None | ||
mod: ModuleType | None = None | ||
|
||
for seg in split: | ||
if mod is not None: | ||
found = pathfinder.find_spec(seg, list(mod.__path__)) | ||
else: | ||
found = pathfinder.find_spec(seg, paths) | ||
|
||
if found is not None and found.loader is not None: | ||
mod = found.loader.load_module(found.name) | ||
|
||
return mod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters