From 61a5246b1b9d814f8952f7de2fc84a871ed535af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Samohel?= Date: Mon, 17 Mar 2025 15:18:34 +0100 Subject: [PATCH] :recycle: reorganize the fix --- client/ayon_core/lib/python_module_tools.py | 3 +- client/ayon_core/pipeline/publish/lib.py | 47 +++---------------- .../tools/publisher/models/publish.py | 2 +- 3 files changed, 9 insertions(+), 43 deletions(-) diff --git a/client/ayon_core/lib/python_module_tools.py b/client/ayon_core/lib/python_module_tools.py index d146e069a9..71daeac42a 100644 --- a/client/ayon_core/lib/python_module_tools.py +++ b/client/ayon_core/lib/python_module_tools.py @@ -28,6 +28,7 @@ def import_filepath(filepath, module_name=None): module_loader = importlib.machinery.SourceFileLoader( module_name, filepath ) + sys.modules[module_name] = module module_loader.exec_module(module) return module @@ -193,7 +194,7 @@ def is_func_signature_supported(func, *args, **kwargs): Notes: This does NOT check if the function would work with passed arguments only if they can be passed in. If function have *args, **kwargs - in paramaters, this will always return 'True'. + in parameters, this will always return 'True'. Example: >>> def my_function(my_number): diff --git a/client/ayon_core/pipeline/publish/lib.py b/client/ayon_core/pipeline/publish/lib.py index d0f90b003d..0602711524 100644 --- a/client/ayon_core/pipeline/publish/lib.py +++ b/client/ayon_core/pipeline/publish/lib.py @@ -2,14 +2,11 @@ from __future__ import annotations import os import sys -import importlib import inspect import copy -from pathlib import Path import warnings import xml.etree.ElementTree -from typing import TYPE_CHECKING, Optional, Union, List - +from typing import Optional, Union, List import ayon_api import pyblish.util @@ -17,6 +14,7 @@ import pyblish.plugin import pyblish.api from ayon_core.lib import ( + import_filepath, Logger, filter_profiles, ) @@ -29,9 +27,6 @@ from .constants import ( DEFAULT_HERO_PUBLISH_TEMPLATE, ) -if TYPE_CHECKING: - from types import ModuleType - def get_template_name_profiles( project_name, project_settings=None, logger=None @@ -215,35 +210,6 @@ def load_help_content_from_plugin(plugin): return load_help_content_from_filepath(filepath) -def _import_module(module_name: str, path: Path) -> ModuleType: - """Import module from path. - - Args: - module_name (str): Name of module. - path (Path): Path to module file. - - Returns: - ModuleType: Imported module. - - Raises: - ImportError: When module cannot be imported. - - """ - spec = importlib.util.spec_from_file_location(module_name, path) - if spec is None: - msg = f"Cannot import path '{path}' as a Python module" - raise ImportError(msg) - - module = importlib.util.module_from_spec(spec) - sys.modules[module_name] = module - if spec.loader is None: - msg = f"Cannot import path '{path}' as a Python module" - raise ImportError(msg) - spec.loader.exec_module(module) - - return module - - def publish_plugins_discover( paths: Optional[list[str]] = None) -> DiscoverResult: """Find and return available pyblish plug-ins. @@ -289,7 +255,7 @@ def publish_plugins_discover( continue try: - module = _import_module(mod_name, Path(abspath)) + module = import_filepath(abspath, mod_name) except Exception as err: # noqa: BLE001 # we need broad exception to catch all possible errors. @@ -313,9 +279,8 @@ def publish_plugins_discover( continue plugin_names.append(plugin.__name__) - - plugin.__module__ = module.__file__ - key = f"{plugin.__module__}.{plugin.__name__}" + plugin.__file__ = module.__file__ + key = f"{module.__file__}.{plugin.__name__}" plugins[key] = plugin # Include plug-ins from registration. @@ -394,7 +359,7 @@ def get_plugin_settings(plugin, project_settings, log, category=None): # Settings category determined from path # - usually path is './/plugins/publish/' # - category can be host name of addon name ('maya', 'deadline', ...) - filepath = os.path.normpath(inspect.getsourcefile(plugin)) + filepath = os.path.normpath(inspect.getfile(plugin.__class__)) split_path = filepath.rsplit(os.path.sep, 5) if len(split_path) < 4: diff --git a/client/ayon_core/tools/publisher/models/publish.py b/client/ayon_core/tools/publisher/models/publish.py index b17b450846..97a956b18f 100644 --- a/client/ayon_core/tools/publisher/models/publish.py +++ b/client/ayon_core/tools/publisher/models/publish.py @@ -279,7 +279,7 @@ class PublishReportMaker: "name": plugin.__name__, "label": label, "order": plugin.order, - "filepath": inspect.getfile(plugin.__class__), + "filepath": inspect.getfile(plugin), "docstring": docstring, "plugin_type": plugin_type, "families": list(plugin.families),