simplified modules file validations and imports

This commit is contained in:
Jakub Trllo 2022-05-03 12:33:26 +02:00
parent be1a10670d
commit 80c7d177a6

View file

@ -290,49 +290,16 @@ def _load_modules():
log = PypeLogger.get_logger("ModulesLoader")
current_dir = os.path.abspath(os.path.dirname(__file__))
processed_paths = set()
processed_paths.add(current_dir)
# Import default modules imported from 'openpype.modules'
for filename in os.listdir(current_dir):
# Ignore filenames
if (
filename in IGNORED_FILENAMES
or filename in IGNORED_DEFAULT_FILENAMES
):
continue
fullpath = os.path.join(current_dir, filename)
basename, ext = os.path.splitext(filename)
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((
"Module directory does not contain __init__.py file {}"
).format(fullpath))
continue
elif ext not in (".py", ):
continue
try:
import_str = "openpype.modules.{}".format(basename)
new_import_str = "{}.{}".format(modules_key, basename)
default_module = __import__(import_str, fromlist=("", ))
sys.modules[new_import_str] = default_module
setattr(openpype_modules, basename, default_module)
except Exception:
msg = (
"Failed to import default module '{}'."
).format(basename)
log.error(msg, exc_info=True)
# Look for OpenPype modules in paths defined with `get_module_dirs`
# - dynamically imported OpenPype modules and addons
for dirpath in get_module_dirs():
module_dirs = get_module_dirs()
# Add current directory at first place
# - has small differences in import logic
current_dir = os.path.abspath(os.path.dirname(__file__))
module_dirs.insert(0, current_dir)
processed_paths = set()
for dirpath in module_dirs:
# Skip already processed paths
if dirpath in processed_paths:
continue
@ -344,39 +311,42 @@ def _load_modules():
).format(dirpath))
continue
is_in_current_dir = dirpath == current_dir
for filename in os.listdir(dirpath):
# Ignore filenames
if filename in IGNORED_FILENAMES:
continue
if (
is_in_current_dir
and filename in IGNORED_DEFAULT_FILENAMES
):
continue
fullpath = os.path.join(dirpath, filename)
basename, ext = os.path.splitext(filename)
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((
"Module directory does not contain __init__.py file {}"
).format(fullpath))
continue
elif ext not in (".py", ):
continue
# TODO add more logic how to define if folder is module or not
# - check manifest and content of manifest
try:
if os.path.isdir(fullpath):
# Module without init file can't be used as OpenPype module
# because the module class could not be imported
init_file = os.path.join(fullpath, "__init__.py")
if not os.path.exists(init_file):
log.info((
"Skipping module directory because of"
" missing \"__init__.py\" file. \"{}\""
if is_in_current_dir:
# Don't import dynamically
import_str = "openpype.modules.{}".format(basename)
new_import_str = "{}.{}".format(modules_key, basename)
default_module = __import__(import_str, fromlist=("", ))
sys.modules[new_import_str] = default_module
setattr(openpype_modules, basename, default_module)
elif 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((
"Module directory does not contan __init__.py"
" file {}"
).format(fullpath))
continue
import_module_from_dirpath(dirpath, filename, modules_key)
elif ext in (".py", ):
@ -384,10 +354,13 @@ def _load_modules():
setattr(openpype_modules, basename, module)
except Exception:
log.error(
"Failed to import '{}'.".format(fullpath),
exc_info=True
)
if is_in_current_dir:
msg = "Failed to import default module '{}'.".format(
basename
)
else:
msg = "Failed to import '{}'.".format(fullpath)
log.error(msg, exc_info=True)
class _OpenPypeInterfaceMeta(ABCMeta):