1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-10 20:32:12 +03:00

[#1462] LoadModule Replacement, Take Two

The previous attempt was almost correct - but we needed to ensure that the system was aware of the module in order to handle it correctly.
This commit is contained in:
David Sangrey 2024-04-30 23:16:51 -04:00
parent a830f40454
commit c48df655d1
No known key found for this signature in database
GPG Key ID: 3AEADBB0186884BC

32
plug.py
View File

@ -47,14 +47,14 @@ last_error = LastError()
class Plugin: class Plugin:
"""An EDMC plugin.""" """An EDMC plugin."""
def __init__(self, name: str, loadfile: str | None, plugin_logger: logging.Logger | None): def __init__(self, name: str, loadfile: str | None, plugin_logger: logging.Logger | None): # noqa: CCR001
""" """
Load a single plugin. Load a single plugin.
:param name: Base name of the file being loaded from. :param name: Base name of the file being loaded from.
:param loadfile: Full path/filename of the plugin. :param loadfile: Full path/filename of the plugin.
:param plugin_logger: The logging instance for this plugin to use. :param plugin_logger: The logging instance for this plugin to use.
:raises Exception: Typically ImportError or OSError :raises Exception: Typically, ImportError or OSError
""" """
self.name: str = name # Display name. self.name: str = name # Display name.
self.folder: str | None = name # basename of plugin folder. None for internal plugins. self.folder: str | None = name # basename of plugin folder. None for internal plugins.
@ -66,19 +66,23 @@ class Plugin:
try: try:
filename = 'plugin_' filename = 'plugin_'
filename += name.encode(encoding='ascii', errors='replace').decode('utf-8').replace('.', '_') filename += name.encode(encoding='ascii', errors='replace').decode('utf-8').replace('.', '_')
module = importlib.machinery.SourceFileLoader( spec = importlib.util.spec_from_file_location(filename, loadfile)
filename,
loadfile if spec is not None and spec.loader is not None: # Check if spec and spec.loader are not None
).load_module() module = importlib.util.module_from_spec(spec)
if getattr(module, 'plugin_start3', None): sys.modules[module.__name__] = module
newname = module.plugin_start3(os.path.dirname(loadfile)) spec.loader.exec_module(module)
self.name = str(newname) if newname else self.name if getattr(module, 'plugin_start3', None):
self.module = module newname = module.plugin_start3(os.path.dirname(loadfile))
elif getattr(module, 'plugin_start', None): self.name = str(newname) if newname else self.name
logger.warning(f'plugin {name} needs migrating\n') self.module = module
PLUGINS_not_py3.append(self) elif getattr(module, 'plugin_start', None):
logger.warning(f'plugin {name} needs migrating\n')
PLUGINS_not_py3.append(self)
else:
logger.error(f'plugin {name} has no plugin_start3() function')
else: else:
logger.error(f'plugin {name} has no plugin_start3() function') logger.error(f'Failed to load Plugin "{name}" from file "{loadfile}"')
except Exception: except Exception:
logger.exception(f': Failed for Plugin "{name}"') logger.exception(f': Failed for Plugin "{name}"')
raise raise