From b413a04cab65132aefe20cd9b56bf111265e497f Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:27:25 +0200 Subject: [PATCH 1/5] skip import from hosts folder --- client/ayon_core/addon/base.py | 106 +++++++++++------------------ client/ayon_core/hosts/__init__.py | 0 2 files changed, 40 insertions(+), 66 deletions(-) delete mode 100644 client/ayon_core/hosts/__init__.py diff --git a/client/ayon_core/addon/base.py b/client/ayon_core/addon/base.py index b10629ede8..6d0dd94df0 100644 --- a/client/ayon_core/addon/base.py +++ b/client/ayon_core/addon/base.py @@ -411,82 +411,56 @@ def _load_addons_in_core( ): # Add current directory at first place # - has small differences in import logic - hosts_dir = os.path.join(AYON_CORE_ROOT, "hosts") modules_dir = os.path.join(AYON_CORE_ROOT, "modules") + if not os.path.exists(modules_dir): + log.warning(( + "Could not find path when loading AYON addons \"{}\"" + ).format(modules_dir)) + return - for dirpath in {hosts_dir, modules_dir}: - if not os.path.exists(dirpath): - log.warning(( - "Could not find path when loading AYON addons \"{}\"" - ).format(dirpath)) + ignored_filenames = set(IGNORED_DEFAULT_FILENAMES) + + for filename in os.listdir(modules_dir): + # Ignore filenames + if filename in IGNORED_FILENAMES or filename in ignored_filenames: continue - is_in_modules_dir = dirpath == modules_dir - ignored_filenames = set() - if is_in_modules_dir: - ignored_filenames = set(IGNORED_DEFAULT_FILENAMES) + fullpath = os.path.join(modules_dir, filename) + basename, ext = os.path.splitext(filename) - for filename in os.listdir(dirpath): - # Ignore filenames - if filename in IGNORED_FILENAMES or filename in ignored_filenames: + if basename in ignore_addon_names: + continue + + # Validations + if os.path.isdir(fullpath): + # Check existence of init file + init_path = os.path.join(fullpath, "__init__.py") + if not os.path.exists(init_path): + log.debug(( + "Addon directory does not contain __init__.py" + " file {}" + ).format(fullpath)) continue - fullpath = os.path.join(dirpath, filename) - basename, ext = os.path.splitext(filename) + elif ext not in (".py", ): + continue - if basename in ignore_addon_names: - continue + # TODO add more logic how to define if folder is addon or not + # - check manifest and content of manifest + try: + # Don't import dynamically current directory modules + new_import_str = "{}.{}".format(modules_key, basename) - # Validations - if os.path.isdir(fullpath): - # Check existence of init file - init_path = os.path.join(fullpath, "__init__.py") - if not os.path.exists(init_path): - log.debug(( - "Addon directory does not contain __init__.py" - " file {}" - ).format(fullpath)) - continue + import_str = "ayon_core.modules.{}".format(basename) + default_module = __import__(import_str, fromlist=("", )) + sys.modules[new_import_str] = default_module + setattr(openpype_modules, basename, default_module) - elif ext not in (".py", ): - continue - - # TODO add more logic how to define if folder is addon or not - # - check manifest and content of manifest - try: - # Don't import dynamically current directory modules - new_import_str = "{}.{}".format(modules_key, basename) - if is_in_modules_dir: - import_str = "ayon_core.modules.{}".format(basename) - default_module = __import__(import_str, fromlist=("", )) - sys.modules[new_import_str] = default_module - setattr(openpype_modules, basename, default_module) - - else: - import_str = "ayon_core.hosts.{}".format(basename) - # Until all hosts are converted to be able use them as - # modules is this error check needed - try: - default_module = __import__( - import_str, fromlist=("", ) - ) - sys.modules[new_import_str] = default_module - setattr(openpype_modules, basename, default_module) - - except Exception: - log.warning( - "Failed to import host folder {}".format(basename), - exc_info=True - ) - - except Exception: - if is_in_modules_dir: - msg = "Failed to import in-core addon '{}'.".format( - basename - ) - else: - msg = "Failed to import addon '{}'.".format(fullpath) - log.error(msg, exc_info=True) + except Exception: + log.error( + f"Failed to import in-core addon '{basename}'.", + exc_info=True + ) def _load_addons(): diff --git a/client/ayon_core/hosts/__init__.py b/client/ayon_core/hosts/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 From df3670f30fc595fd14f5aab73ff8f17ec54ec4a1 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:27:32 +0200 Subject: [PATCH 2/5] use sets --- client/ayon_core/addon/base.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/client/ayon_core/addon/base.py b/client/ayon_core/addon/base.py index 6d0dd94df0..1f1e749865 100644 --- a/client/ayon_core/addon/base.py +++ b/client/ayon_core/addon/base.py @@ -28,16 +28,16 @@ from .interfaces import ( ) # Files that will be always ignored on addons import -IGNORED_FILENAMES = ( +IGNORED_FILENAMES = { "__pycache__", -) +} # Files ignored on addons import from "./ayon_core/modules" -IGNORED_DEFAULT_FILENAMES = ( +IGNORED_DEFAULT_FILENAMES = { "__init__.py", "base.py", "interfaces.py", "click_wrap.py", -) +} # When addon was moved from ayon-core codebase # - this is used to log the missing addon @@ -418,11 +418,11 @@ def _load_addons_in_core( ).format(modules_dir)) return - ignored_filenames = set(IGNORED_DEFAULT_FILENAMES) + ignored_filenames = IGNORED_FILENAMES | IGNORED_DEFAULT_FILENAMES for filename in os.listdir(modules_dir): # Ignore filenames - if filename in IGNORED_FILENAMES or filename in ignored_filenames: + if filename in ignored_filenames: continue fullpath = os.path.join(modules_dir, filename) From cbac60598985697079f78f9b7edbc5b0141f4f23 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:28:24 +0200 Subject: [PATCH 3/5] use f-strings --- client/ayon_core/addon/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/ayon_core/addon/base.py b/client/ayon_core/addon/base.py index 1f1e749865..878c50e039 100644 --- a/client/ayon_core/addon/base.py +++ b/client/ayon_core/addon/base.py @@ -438,8 +438,8 @@ def _load_addons_in_core( if not os.path.exists(init_path): log.debug(( "Addon directory does not contain __init__.py" - " file {}" - ).format(fullpath)) + f" file {fullpath}" + )) continue elif ext not in (".py", ): @@ -449,9 +449,9 @@ def _load_addons_in_core( # - check manifest and content of manifest try: # Don't import dynamically current directory modules - new_import_str = "{}.{}".format(modules_key, basename) + new_import_str = f"{modules_key}.{basename}" - import_str = "ayon_core.modules.{}".format(basename) + import_str = f"ayon_core.modules.{basename}" default_module = __import__(import_str, fromlist=("", )) sys.modules[new_import_str] = default_module setattr(openpype_modules, basename, default_module) From ef3068a4c205767e47398a225497dae13bc08a09 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:28:32 +0200 Subject: [PATCH 4/5] use direct comparison --- client/ayon_core/addon/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/ayon_core/addon/base.py b/client/ayon_core/addon/base.py index 878c50e039..8c5158a712 100644 --- a/client/ayon_core/addon/base.py +++ b/client/ayon_core/addon/base.py @@ -442,7 +442,7 @@ def _load_addons_in_core( )) continue - elif ext not in (".py", ): + elif ext != ".py": continue # TODO add more logic how to define if folder is addon or not From b14c041cf86b5bded6d4bbf26becbfe0456be166 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:37:52 +0200 Subject: [PATCH 5/5] more f-string --- client/ayon_core/addon/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/ayon_core/addon/base.py b/client/ayon_core/addon/base.py index 8c5158a712..b9ecff4233 100644 --- a/client/ayon_core/addon/base.py +++ b/client/ayon_core/addon/base.py @@ -413,9 +413,9 @@ def _load_addons_in_core( # - has small differences in import logic modules_dir = os.path.join(AYON_CORE_ROOT, "modules") if not os.path.exists(modules_dir): - log.warning(( - "Could not find path when loading AYON addons \"{}\"" - ).format(modules_dir)) + log.warning( + f"Could not find path when loading AYON addons \"{modules_dir}\"" + ) return ignored_filenames = IGNORED_FILENAMES | IGNORED_DEFAULT_FILENAMES