1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-17 17:42:20 +03:00

Merge pull request #2217 from HullSeals/enhancement/1462/loadmodule-replacement

[#1462] Remove Deprecated load_module() Function
This commit is contained in:
David Sangrey 2024-05-24 18:39:38 -04:00 committed by GitHub
commit 2d58ef990b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 15 deletions

View File

@ -53,7 +53,7 @@ appcmdname = 'EDMC'
# <https://semver.org/#semantic-versioning-specification-semver> # <https://semver.org/#semantic-versioning-specification-semver>
# Major.Minor.Patch(-prerelease)(+buildmetadata) # Major.Minor.Patch(-prerelease)(+buildmetadata)
# NB: Do *not* import this, use the functions appversion() and appversion_nobuild() # NB: Do *not* import this, use the functions appversion() and appversion_nobuild()
_static_appversion = '5.10.6' _static_appversion = '5.11.0-alpha3'
_cached_version: semantic_version.Version | None = None _cached_version: semantic_version.Version | None = None
copyright = '© 2015-2019 Jonathan Harris, 2020-2024 EDCD' copyright = '© 2015-2019 Jonathan Harris, 2020-2024 EDCD'

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, # Replaces older load_module() code. Includes a safety check that the module name is set.
loadfile if spec is not None and spec.loader is 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