Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Look for relative imports and reload those modules
Browse files Browse the repository at this point in the history
This is proof of concept basically, a recursive function needs to be
implemented to guarantee all modules are reloaded.
  • Loading branch information
forslund committed Sep 27, 2019
1 parent b5a1ae5 commit 902de98
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions mycroft/skills/skill_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@


def reload_related_modules(module_name):
"""Reload module located in the same folder as main module.
Arguments:
module_name (str): name of the module as set in sys.modules
"""
module = sys.modules[module_name]
module_dir = dirname(module_name.__file__)
module_dir = dirname(module.__file__)

# Find all imported modules with associated files
modules = [i for _, i in module.__dict__.items()
if isinstance(i, ModuleType)]
if isinstance(i, ModuleType) and '__file__' in dir(i)]
# modules in the skill's folder are consider relatives
relatives = [m for m in modules if m.__file__.startswith(module_dir)]

# Reload them
for r in relatives:
LOG.debug('Reloading {}'.format(r.__name__))
importlib.reload(r)


Expand All @@ -50,20 +61,11 @@ def load_skill_module(path, skill_id):
skill_id: skill_id used as skill identifier in the module list
"""
module_name = skill_id.replace('.', '_')
if sys.version_info >= (3, 9):
import importlib.util
spec = importlib.util.spec_from_file_location(module_name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
else:
from importlib.machinery import SourceFileLoader
if module_name in sys.modules:
reload_related_modules(module_name)
mod = SourceFileLoader(module_name, path).load_module(name=module_name)
if mod:
return mod
else:
return None
from importlib.machinery import SourceFileLoader
if module_name in sys.modules:
reload_related_modules(module_name)
mod = SourceFileLoader(module_name, path).load_module(name=module_name)
return mod


def _get_last_modified_time(path):
Expand Down

0 comments on commit 902de98

Please sign in to comment.