-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from markmckinnon/New-Plugin-Loader
Update to new plugin loader
- Loading branch information
Showing
43 changed files
with
430 additions
and
206 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,24 @@ | ||
import plugin_loader | ||
|
||
# Hey PyInstaller? Yeah you! Take a look at these plugins! I know they're not actually imported anywhere but you | ||
# better believe that they will be a runtime, so, if you wouldn't mind, it'd be fantastic if you pretended that | ||
# they're imported normally and pick up *their* imports. OK? Great. Fantastic. | ||
|
||
print("Hooking plugins for pyinstaller") | ||
|
||
loader = plugin_loader.PluginLoader() | ||
|
||
tmp = [] | ||
|
||
for py_file in plugin_loader.PLUGINPATH.glob("*.py"): | ||
mod = plugin_loader.PluginLoader.load_module_lazy(py_file) | ||
try: | ||
mod_artifacts = mod.__artifacts__ | ||
except AttributeError: | ||
pass # any unconverted plugins still get checked out so they don't break the loader during runtime | ||
|
||
tmp.append("scripts.artifacts." + mod.__name__) # TODO this is a hack, if we ever move plugins this breaks | ||
|
||
print(f"{len(tmp)} plugins loaded as hidden imports") | ||
|
||
hiddenimports = list(tmp) |
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
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
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,65 @@ | ||
import pathlib | ||
import dataclasses | ||
import typing | ||
import importlib.util | ||
|
||
#PLUGINPATH = pathlib.Path("./scripts/artifacts") | ||
# a bit long-winded to make compatible with PyInstaller | ||
PLUGINPATH = pathlib.Path(__file__).resolve().parent / pathlib.Path("scripts/artifacts") | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class PluginSpec: | ||
name: str | ||
module_name: str | ||
category: str | ||
search: str | ||
method: typing.Callable # todo define callable signature | ||
|
||
|
||
class PluginLoader: | ||
def __init__(self, plugin_path: typing.Optional[pathlib.Path] = None): | ||
self._plugin_path = plugin_path or PLUGINPATH | ||
self._plugins: dict[str, PluginSpec] = {} | ||
self._load_plugins() | ||
|
||
@staticmethod | ||
def load_module_lazy(path: pathlib.Path): | ||
spec = importlib.util.spec_from_file_location(path.stem, path) | ||
loader = importlib.util.LazyLoader(spec.loader) | ||
spec.loader = loader | ||
mod = importlib.util.module_from_spec(spec) | ||
loader.exec_module(mod) | ||
return mod | ||
|
||
def _load_plugins(self): | ||
for py_file in self._plugin_path.glob("*.py"): | ||
mod = PluginLoader.load_module_lazy(py_file) | ||
try: | ||
mod_artifacts = mod.__artifacts__ | ||
except AttributeError: | ||
continue # no artifacts defined in this plugin | ||
|
||
for name, (category, search, func) in mod_artifacts.items(): | ||
#self._plugins.append(PluginSpec(name, search, func)) | ||
if name in self._plugins: | ||
raise KeyError("Duplicate plugin {name} ") | ||
self._plugins[name] = PluginSpec(name, py_file.stem, category, search, func) | ||
|
||
@property | ||
def plugins(self) -> typing.Iterable[PluginSpec]: | ||
yield from self._plugins.values() | ||
|
||
def __getitem__(self, item: str) -> PluginSpec: | ||
return self._plugins[item] | ||
|
||
def __contains__(self, item): | ||
return item in self._plugins | ||
|
||
def __len__(self): | ||
return len(self._plugins) | ||
|
||
|
||
|
||
|
||
|
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
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
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
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
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
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
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
Oops, something went wrong.