From 1ec9106f0124d6d69ed7a90429d791b5ad46267d Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 4 Mar 2025 16:06:07 +0100 Subject: [PATCH 01/15] Support `list[str]` for leaf entries in `core/project_folder_structure` settings to define folder names instead of requiring dicts with empty values --- client/ayon_core/pipeline/project_folders.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/client/ayon_core/pipeline/project_folders.py b/client/ayon_core/pipeline/project_folders.py index 902b969457..df4353c503 100644 --- a/client/ayon_core/pipeline/project_folders.py +++ b/client/ayon_core/pipeline/project_folders.py @@ -82,6 +82,14 @@ def create_project_folders(project_name, basic_paths=None): def _list_path_items(folder_structure): output = [] + + # Allow leaf folders of the `project_folder_structure` to use a list of + # strings instead of a dictionary of keys with empty values. + if isinstance(folder_structure, list): + assert all(isinstance(item, str) for item in folder_structure) + return [folder_structure] + + # Process key, value as key for folder names and value its subfolders for key, value in folder_structure.items(): if not value: output.append(key) From b9adc29be190e9c2ecada1f64278669c53cf38ed Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 4 Mar 2025 16:06:45 +0100 Subject: [PATCH 02/15] Add some type hints --- client/ayon_core/pipeline/project_folders.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/client/ayon_core/pipeline/project_folders.py b/client/ayon_core/pipeline/project_folders.py index df4353c503..a6f903701f 100644 --- a/client/ayon_core/pipeline/project_folders.py +++ b/client/ayon_core/pipeline/project_folders.py @@ -1,6 +1,7 @@ import os import re import json +from typing import Dict, Any, List, Union from ayon_core.settings import get_project_settings from ayon_core.lib import Logger @@ -9,7 +10,7 @@ from .anatomy import Anatomy from .template_data import get_project_template_data -def concatenate_splitted_paths(split_paths, anatomy): +def concatenate_splitted_paths(split_paths, anatomy: Anatomy): log = Logger.get_logger("concatenate_splitted_paths") pattern_array = re.compile(r"\[.*\]") output = [] @@ -47,7 +48,7 @@ def concatenate_splitted_paths(split_paths, anatomy): return output -def fill_paths(path_list, anatomy): +def fill_paths(path_list: List[str], anatomy: Anatomy): format_data = get_project_template_data(project_name=anatomy.project_name) format_data["root"] = anatomy.roots filled_paths = [] @@ -59,7 +60,7 @@ def fill_paths(path_list, anatomy): return filled_paths -def create_project_folders(project_name, basic_paths=None): +def create_project_folders(project_name: str, basic_paths=None): log = Logger.get_logger("create_project_folders") anatomy = Anatomy(project_name) if basic_paths is None: @@ -80,7 +81,8 @@ def create_project_folders(project_name, basic_paths=None): os.makedirs(path) -def _list_path_items(folder_structure): +def _list_path_items( + folder_structure: Union[Dict[str, Any], List[str]]): output = [] # Allow leaf folders of the `project_folder_structure` to use a list of @@ -107,7 +109,7 @@ def _list_path_items(folder_structure): return output -def get_project_basic_paths(project_name): +def get_project_basic_paths(project_name: str): project_settings = get_project_settings(project_name) folder_structure = ( project_settings["core"]["project_folder_structure"] From 7baf208c61f992a30de62f29a3aef484207ac542 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Wed, 5 Mar 2025 14:51:12 +0100 Subject: [PATCH 03/15] Fix `List[str]` entries --- client/ayon_core/pipeline/project_folders.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/ayon_core/pipeline/project_folders.py b/client/ayon_core/pipeline/project_folders.py index a6f903701f..37197495f9 100644 --- a/client/ayon_core/pipeline/project_folders.py +++ b/client/ayon_core/pipeline/project_folders.py @@ -89,7 +89,7 @@ def _list_path_items( # strings instead of a dictionary of keys with empty values. if isinstance(folder_structure, list): assert all(isinstance(item, str) for item in folder_structure) - return [folder_structure] + return [[path] for path in folder_structure] # Process key, value as key for folder names and value its subfolders for key, value in folder_structure.items(): @@ -119,4 +119,6 @@ def get_project_basic_paths(project_name: str): if isinstance(folder_structure, str): folder_structure = json.loads(folder_structure) - return _list_path_items(folder_structure) + result = _list_path_items(folder_structure) + print(result) + return [] From 1b4f6bea20b2c6e7abac4eb5b57b9377ae78db90 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Wed, 5 Mar 2025 15:05:50 +0100 Subject: [PATCH 04/15] Revert debug code --- client/ayon_core/pipeline/project_folders.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/client/ayon_core/pipeline/project_folders.py b/client/ayon_core/pipeline/project_folders.py index 37197495f9..570442ff0c 100644 --- a/client/ayon_core/pipeline/project_folders.py +++ b/client/ayon_core/pipeline/project_folders.py @@ -119,6 +119,4 @@ def get_project_basic_paths(project_name: str): if isinstance(folder_structure, str): folder_structure = json.loads(folder_structure) - result = _list_path_items(folder_structure) - print(result) - return [] + return _list_path_items(folder_structure) From ab682da2b83b1e405ed776d6b18ca8234e83d302 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Mon, 10 Mar 2025 11:51:29 +0100 Subject: [PATCH 05/15] Raise ValueError instead of using assertion --- client/ayon_core/pipeline/project_folders.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/ayon_core/pipeline/project_folders.py b/client/ayon_core/pipeline/project_folders.py index 570442ff0c..7386382d56 100644 --- a/client/ayon_core/pipeline/project_folders.py +++ b/client/ayon_core/pipeline/project_folders.py @@ -88,7 +88,9 @@ def _list_path_items( # Allow leaf folders of the `project_folder_structure` to use a list of # strings instead of a dictionary of keys with empty values. if isinstance(folder_structure, list): - assert all(isinstance(item, str) for item in folder_structure) + if not all(isinstance(item, str) for item in folder_structure): + raise ValueError( + f"List items must all be strings. Got: {folder_structure}") return [[path] for path in folder_structure] # Process key, value as key for folder names and value its subfolders From eb17eebb99460963ef4888c039975ede350596b5 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Mon, 10 Mar 2025 12:28:49 +0100 Subject: [PATCH 06/15] Update client/ayon_core/pipeline/project_folders.py Co-authored-by: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> --- client/ayon_core/pipeline/project_folders.py | 1 + 1 file changed, 1 insertion(+) diff --git a/client/ayon_core/pipeline/project_folders.py b/client/ayon_core/pipeline/project_folders.py index 7386382d56..1e4d807c49 100644 --- a/client/ayon_core/pipeline/project_folders.py +++ b/client/ayon_core/pipeline/project_folders.py @@ -1,3 +1,4 @@ +from __future__ import annotations import os import re import json From 8008ab02b543b2d42a0dfbb91bd6afca315d28cf Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Mon, 10 Mar 2025 12:29:45 +0100 Subject: [PATCH 07/15] Use future annotations style --- client/ayon_core/pipeline/project_folders.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/ayon_core/pipeline/project_folders.py b/client/ayon_core/pipeline/project_folders.py index 1e4d807c49..def2af9ba1 100644 --- a/client/ayon_core/pipeline/project_folders.py +++ b/client/ayon_core/pipeline/project_folders.py @@ -2,7 +2,7 @@ from __future__ import annotations import os import re import json -from typing import Dict, Any, List, Union +from typing import Any, Union from ayon_core.settings import get_project_settings from ayon_core.lib import Logger @@ -49,7 +49,7 @@ def concatenate_splitted_paths(split_paths, anatomy: Anatomy): return output -def fill_paths(path_list: List[str], anatomy: Anatomy): +def fill_paths(path_list: list[str], anatomy: Anatomy): format_data = get_project_template_data(project_name=anatomy.project_name) format_data["root"] = anatomy.roots filled_paths = [] @@ -83,7 +83,7 @@ def create_project_folders(project_name: str, basic_paths=None): def _list_path_items( - folder_structure: Union[Dict[str, Any], List[str]]): + folder_structure: Union[dict[str, Any], list[str]]): output = [] # Allow leaf folders of the `project_folder_structure` to use a list of From 60c9229f41a604243a71c4ffe65bf6e5b0b7e686 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Thu, 13 Mar 2025 16:47:08 +0100 Subject: [PATCH 08/15] use differnt varible name for aov frames --- client/ayon_core/pipeline/farm/pyblish_functions.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/client/ayon_core/pipeline/farm/pyblish_functions.py b/client/ayon_core/pipeline/farm/pyblish_functions.py index 0261a0c2b5..c6f3ae7115 100644 --- a/client/ayon_core/pipeline/farm/pyblish_functions.py +++ b/client/ayon_core/pipeline/farm/pyblish_functions.py @@ -808,14 +808,14 @@ def _create_instances_for_aov( frames_to_render is not None and isinstance(collected_files, (list, tuple)) # not single file ): - frames_to_render = convert_frames_str_to_list(frames_to_render) + aov_frames_to_render = convert_frames_str_to_list(frames_to_render) collections, _ = clique.assemble(collected_files) collected_files = _get_real_files_to_render( - collections[0], frames_to_render) + collections[0], aov_frames_to_render) else: frame_start = int(skeleton.get("frameStartHandle")) frame_end = int(skeleton.get("frameEndHandle")) - frames_to_render = list(range(frame_start, frame_end + 1)) + aov_frames_to_render = list(range(frame_start, frame_end + 1)) dynamic_data = { "aov": aov, @@ -937,8 +937,8 @@ def _create_instances_for_aov( "name": ext, "ext": ext, "files": collected_files, - "frameStart": frames_to_render[0], - "frameEnd": frames_to_render[-1], + "frameStart": aov_frames_to_render[0], + "frameEnd": aov_frames_to_render[-1], # If expectedFile are absolute, we need only filenames "stagingDir": staging_dir, "fps": new_instance.get("fps"), From 4a7f79a7bad5b533eb9112ab3ea0ada70a243566 Mon Sep 17 00:00:00 2001 From: Ynbot Date: Thu, 13 Mar 2025 16:06:47 +0000 Subject: [PATCH 09/15] [Automated] Add generated package files from main --- client/ayon_core/version.py | 2 +- package.py | 2 +- pyproject.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/ayon_core/version.py b/client/ayon_core/version.py index 332001aef7..7717a7a215 100644 --- a/client/ayon_core/version.py +++ b/client/ayon_core/version.py @@ -1,3 +1,3 @@ # -*- coding: utf-8 -*- """Package declaring AYON addon 'core' version.""" -__version__ = "1.1.4+dev" +__version__ = "1.1.5" diff --git a/package.py b/package.py index 2c9072d443..69e4b85ef2 100644 --- a/package.py +++ b/package.py @@ -1,6 +1,6 @@ name = "core" title = "Core" -version = "1.1.4+dev" +version = "1.1.5" client_dir = "ayon_core" diff --git a/pyproject.toml b/pyproject.toml index 0bbe0f90e1..eca50f349d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ [tool.poetry] name = "ayon-core" -version = "1.1.4+dev" +version = "1.1.5" description = "" authors = ["Ynput Team "] readme = "README.md" From 7c4fb2d2e2f317a3f63c475fee6d4018e5ced990 Mon Sep 17 00:00:00 2001 From: Ynbot Date: Thu, 13 Mar 2025 16:07:28 +0000 Subject: [PATCH 10/15] [Automated] Update version in package.py for develop --- client/ayon_core/version.py | 2 +- package.py | 2 +- pyproject.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/ayon_core/version.py b/client/ayon_core/version.py index 7717a7a215..de5c199428 100644 --- a/client/ayon_core/version.py +++ b/client/ayon_core/version.py @@ -1,3 +1,3 @@ # -*- coding: utf-8 -*- """Package declaring AYON addon 'core' version.""" -__version__ = "1.1.5" +__version__ = "1.1.5+dev" diff --git a/package.py b/package.py index 69e4b85ef2..250b77fe52 100644 --- a/package.py +++ b/package.py @@ -1,6 +1,6 @@ name = "core" title = "Core" -version = "1.1.5" +version = "1.1.5+dev" client_dir = "ayon_core" diff --git a/pyproject.toml b/pyproject.toml index eca50f349d..94badd2f1a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ [tool.poetry] name = "ayon-core" -version = "1.1.5" +version = "1.1.5+dev" description = "" authors = ["Ynput Team "] readme = "README.md" From b72a1e1d28440670feec11477581041dae880041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Samohel?= Date: Mon, 17 Mar 2025 14:19:18 +0100 Subject: [PATCH 11/15] :bug: fix publish plugin discovery and some typos and unused imports along the way --- client/ayon_core/pipeline/publish/lib.py | 65 ++++++++++++++----- .../tools/publisher/models/publish.py | 2 +- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/client/ayon_core/pipeline/publish/lib.py b/client/ayon_core/pipeline/publish/lib.py index cc5f67c74b..d0f90b003d 100644 --- a/client/ayon_core/pipeline/publish/lib.py +++ b/client/ayon_core/pipeline/publish/lib.py @@ -1,10 +1,15 @@ +"""Library functions for publishing.""" +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 Optional, Union, List +from typing import TYPE_CHECKING, Optional, Union, List + import ayon_api import pyblish.util @@ -13,7 +18,6 @@ import pyblish.api from ayon_core.lib import ( Logger, - import_filepath, filter_profiles, ) from ayon_core.settings import get_project_settings @@ -25,6 +29,9 @@ 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 @@ -163,7 +170,7 @@ class HelpContent: def load_help_content_from_filepath(filepath): """Load help content from xml file. - Xml file may containt errors and warnings. + Xml file may contain errors and warnings. """ errors = {} warnings = {} @@ -208,8 +215,38 @@ def load_help_content_from_plugin(plugin): return load_help_content_from_filepath(filepath) -def publish_plugins_discover(paths=None): - """Find and return available pyblish plug-ins +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. Overridden function from `pyblish` module to be able to collect crashed files and reason of their crash. @@ -252,17 +289,13 @@ def publish_plugins_discover(paths=None): continue try: - module = import_filepath(abspath, mod_name) + module = _import_module(mod_name, Path(abspath)) - # Store reference to original module, to avoid - # garbage collection from collecting it's global - # imports, such as `import os`. - sys.modules[abspath] = module - - except Exception as err: + except Exception as err: # noqa: BLE001 + # we need broad exception to catch all possible errors. result.crashed_file_paths[abspath] = sys.exc_info() - log.debug("Skipped: \"%s\" (%s)", mod_name, err) + log.debug('Skipped: "%s" (%s)', mod_name, err) continue for plugin in pyblish.plugin.plugins_from_module(module): @@ -282,7 +315,7 @@ def publish_plugins_discover(paths=None): plugin_names.append(plugin.__name__) plugin.__module__ = module.__file__ - key = "{0}.{1}".format(plugin.__module__, plugin.__name__) + key = f"{plugin.__module__}.{plugin.__name__}" plugins[key] = plugin # Include plug-ins from registration. @@ -427,7 +460,7 @@ def filter_pyblish_plugins(plugins): log = Logger.get_logger("filter_pyblish_plugins") # TODO: Don't use host from 'pyblish.api' but from defined host by us. - # - kept becau on farm is probably used host 'shell' which propably + # - kept because on farm is probably used host 'shell' which probably # affect how settings are applied there host_name = pyblish.api.current_host() project_name = os.environ.get("AYON_PROJECT_NAME") @@ -529,7 +562,7 @@ def filter_instances_for_context_plugin(plugin, context): Args: plugin (pyblish.api.Plugin): Plugin with filters. - context (pyblish.api.Context): Pyblish context with insances. + context (pyblish.api.Context): Pyblish context with instances. Returns: Iterator[pyblish.lib.Instance]: Iteration of valid instances. diff --git a/client/ayon_core/tools/publisher/models/publish.py b/client/ayon_core/tools/publisher/models/publish.py index 97a956b18f..b17b450846 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), + "filepath": inspect.getfile(plugin.__class__), "docstring": docstring, "plugin_type": plugin_type, "families": list(plugin.families), 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 12/15] :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), From fb64de8d3276b94af27a6224089831981f76c25b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Samohel?= Date: Mon, 17 Mar 2025 15:31:39 +0100 Subject: [PATCH 13/15] :recycle: make changes more backward safe --- client/ayon_core/lib/python_module_tools.py | 20 +++++++++++++++++--- client/ayon_core/pipeline/publish/lib.py | 3 ++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/client/ayon_core/lib/python_module_tools.py b/client/ayon_core/lib/python_module_tools.py index 71daeac42a..7ff8758a90 100644 --- a/client/ayon_core/lib/python_module_tools.py +++ b/client/ayon_core/lib/python_module_tools.py @@ -1,6 +1,8 @@ +"""Tools for working with python modules and classes.""" import os import sys import types +from typing import Optional import importlib import inspect import logging @@ -8,13 +10,22 @@ import logging log = logging.getLogger(__name__) -def import_filepath(filepath, module_name=None): +def import_filepath( + filepath: str, + module_name: Optional[str]=None, + sys_module_name: Optional[str]=None) -> types.ModuleType: """Import python file as python module. Args: filepath (str): Path to python file. module_name (str): Name of loaded module. Only for Python 3. By default is filled with filename of filepath. + sys_module_name (str): Name of module in `sys.modules` where to store + loaded module. By default is None so module is not added to + `sys.modules`. + + Todo (antirotor): We should add the module to the sys.modules always but + we need to be careful about it and test it properly. """ if module_name is None: @@ -28,7 +39,9 @@ def import_filepath(filepath, module_name=None): module_loader = importlib.machinery.SourceFileLoader( module_name, filepath ) - sys.modules[module_name] = module + # only add to sys.modules if requested + if sys_module_name: + sys.modules[sys_module_name] = module module_loader.exec_module(module) return module @@ -127,7 +140,8 @@ def classes_from_module(superclass, module): return classes -def import_module_from_dirpath(dirpath, folder_name, dst_module_name=None): +def import_module_from_dirpath( + dirpath, folder_name, dst_module_name=None): """Import passed directory as a python module. Imported module can be assigned as a child attribute of already loaded diff --git a/client/ayon_core/pipeline/publish/lib.py b/client/ayon_core/pipeline/publish/lib.py index 0602711524..048109e3ea 100644 --- a/client/ayon_core/pipeline/publish/lib.py +++ b/client/ayon_core/pipeline/publish/lib.py @@ -255,7 +255,8 @@ def publish_plugins_discover( continue try: - module = import_filepath(abspath, mod_name) + module = import_filepath( + abspath, mod_name, sys_module_name=mod_name) except Exception as err: # noqa: BLE001 # we need broad exception to catch all possible errors. From d31374043464ef4bb92bd3c4f04b41ddaf5a90f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Samohel?= <33513211+antirotor@users.noreply.github.com> Date: Tue, 18 Mar 2025 11:22:17 +0100 Subject: [PATCH 14/15] Update client/ayon_core/lib/python_module_tools.py Co-authored-by: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> --- client/ayon_core/lib/python_module_tools.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/ayon_core/lib/python_module_tools.py b/client/ayon_core/lib/python_module_tools.py index 7ff8758a90..a6dc8031c7 100644 --- a/client/ayon_core/lib/python_module_tools.py +++ b/client/ayon_core/lib/python_module_tools.py @@ -12,8 +12,8 @@ log = logging.getLogger(__name__) def import_filepath( filepath: str, - module_name: Optional[str]=None, - sys_module_name: Optional[str]=None) -> types.ModuleType: + module_name: Optional[str] = None, + sys_module_name: Optional[str] = None) -> types.ModuleType: """Import python file as python module. Args: From 44e32c5bfe852c8605b7707a56e53b525714dd4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Samohel?= <33513211+antirotor@users.noreply.github.com> Date: Tue, 18 Mar 2025 11:22:24 +0100 Subject: [PATCH 15/15] Update client/ayon_core/pipeline/publish/lib.py Co-authored-by: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> --- client/ayon_core/pipeline/publish/lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/ayon_core/pipeline/publish/lib.py b/client/ayon_core/pipeline/publish/lib.py index 048109e3ea..49ecab2221 100644 --- a/client/ayon_core/pipeline/publish/lib.py +++ b/client/ayon_core/pipeline/publish/lib.py @@ -360,7 +360,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.getfile(plugin.__class__)) + filepath = os.path.normpath(inspect.getfile(plugin)) split_path = filepath.rsplit(os.path.sep, 5) if len(split_path) < 4: