Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👌 IMPROVE: Replace deprecated imp with importlib #4848

Merged
merged 7 commits into from
May 6, 2021
34 changes: 27 additions & 7 deletions aiida/restapi/translator/nodes/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
###########################################################################
"""Translator for node"""
import pkgutil
import imp
import importlib.util
import importlib.machinery
from importlib._bootstrap import _exec, _load
import sys
import inspect
import os

Expand Down Expand Up @@ -341,17 +344,34 @@ def _get_subclasses(self, parent=None, parent_class=None, recursive=True):
results = {}

for _, name, is_pkg in pkgutil.walk_packages([package_path]):
# N.B. pkgutil.walk_package requires a LIST of paths.
# N.B. pkgutil.walk_package requires a LIST of path
DirectriX01 marked this conversation as resolved.
Show resolved Hide resolved

full_path_base = os.path.join(package_path, name)

if is_pkg:
app_module = imp.load_package(full_path_base, full_path_base)
if os.path.isdir(full_path_base):
#Adds an extension to check for __init__ file in the package directory
DirectriX01 marked this conversation as resolved.
Show resolved Hide resolved
extensions = (importlib.machinery.SOURCE_SUFFIXES[:] + importlib.machinery.BYTECODE_SUFFIXES[:])
for extension in extensions:
init_path = os.path.join(full_path_base, '__init__' + extension)
if os.path.exists(init_path):
path = init_path
break
else:
raise ValueError(f'{full_path_base!r} is not a package')
#passing [] to submodule_search_locations indicates its a package and python searches for sub-modules
spec = importlib.util.spec_from_file_location(full_path_base, path, submodule_search_locations=[])
if full_path_base in sys.modules:
#executes from sys.modules
app_module = _exec(spec, sys.modules[full_path_base])
else:
#loads and executes the module
app_module = _load(spec)
else:
full_path = f'{full_path_base}.py'
# I could use load_module but it takes lots of arguments,
# then I use load_source
app_module = imp.load_source(f'rst{name}', full_path)
spec = importlib.util.spec_from_file_location(name, full_path)
app_module = importlib.util.module_from_spec(spec)
DirectriX01 marked this conversation as resolved.
Show resolved Hide resolved
sys.modules[name] = app_module
spec.loader.exec_module(app_module)

# Go through the content of the module
if not is_pkg:
Expand Down