Skip to content

Commit

Permalink
Merge pull request #2679 from forslund/refactor/remove-imp
Browse files Browse the repository at this point in the history
Refactor/remove imp
  • Loading branch information
krisgesling committed Aug 24, 2020
2 parents 305623a + e2a7fe9 commit 3eee84f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
21 changes: 14 additions & 7 deletions mycroft/audio/audioservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import imp
import importlib
import sys
import time
from os import listdir
Expand All @@ -31,7 +31,7 @@
sys.path.append(abspath(dirname(__file__)))


def create_service_descriptor(service_folder):
def create_service_spec(service_folder):
"""Prepares a descriptor that can be used together with imp.
Args:
Expand All @@ -40,7 +40,11 @@ def create_service_descriptor(service_folder):
Returns:
Dict with import information
"""
info = imp.find_module(MAINMODULE, [service_folder])
module_name = basename(service_folder)
path = join(service_folder, MAINMODULE + '.py')
spec = importlib.util.spec_from_file_location(module_name, path)
mod = importlib.util.module_from_spec(spec)
info = {'spec': spec, 'mod': mod, 'module_name': module_name}
return {"name": basename(service_folder), "info": info}


Expand All @@ -67,15 +71,15 @@ def get_services(services_folder):
not MAINMODULE + ".py" in listdir(name)):
continue
try:
services.append(create_service_descriptor(name))
services.append(create_service_spec(name))
except Exception:
LOG.error('Failed to create service from ' + name,
exc_info=True)
if (not isdir(location) or
not MAINMODULE + ".py" in listdir(location)):
continue
try:
services.append(create_service_descriptor(location))
services.append(create_service_spec(location))
except Exception:
LOG.error('Failed to create service from ' + location,
exc_info=True)
Expand All @@ -100,8 +104,11 @@ def load_services(config, bus, path=None):
for descriptor in service_directories:
LOG.info('Loading ' + descriptor['name'])
try:
service_module = imp.load_module(descriptor["name"] + MAINMODULE,
*descriptor["info"])
service_module = descriptor['info']['mod']
spec = descriptor['info']['spec']
module_name = descriptor['info']['module_name']
sys.modules[module_name] = service_module
spec.loader.exec_module(service_module)
except Exception as e:
LOG.error('Failed to import module ' + descriptor['name'] + '\n' +
repr(e))
Expand Down
16 changes: 4 additions & 12 deletions test/integrationtests/skills/discover_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

import glob
import os
from os.path import exists, join, expanduser, abspath
import imp
from os.path import join, expanduser, abspath

from mycroft.configuration import Configuration
from test.integrationtests.skills.skill_tester import MockSkillsLoader
from test.integrationtests.skills.skill_tester import SkillTest

from .runner import load_test_environment


def discover_tests(skills_dir):
""" Find all tests for the skills in the default skill path,
Expand All @@ -43,16 +44,7 @@ def discover_tests(skills_dir):

for skill in skills:
# Load test environment file
test_env = None
if exists(os.path.join(skill, 'test/__init__.py')):
module = imp.load_source(skill + '.test_env',
os.path.join(skill, 'test/__init__.py'))
if (hasattr(module, 'test_runner') and
callable(module.test_runner) or
hasattr(module, 'test_setup') and
callable(module.test_setup)):
test_env = module

test_env = load_test_environment(skill)
# Find all intent test files
test_intent_files = [
(f, test_env) for f
Expand Down
37 changes: 27 additions & 10 deletions test/integrationtests/skills/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import os
from os.path import exists
import sys
import imp
import importlib
import argparse
from test.integrationtests.skills.skill_tester import MockSkillsLoader
from test.integrationtests.skills.skill_tester import SkillTest
Expand All @@ -47,6 +47,31 @@
sys.argv = sys.argv[:1]


def load_test_environment(skill):
"""Load skill's test environment if present
Arguments:
skill (str): path to skill root folder
Returns:
Module if a valid test environment module was found else None
"""
test_env = None
test_env_path = os.path.join(skill, 'test/__init__.py')
if exists(test_env_path):
skill_env = skill + '.test_env'
spec = importlib.util.spec_from_file_location(skill_env, test_env_path)
module = importlib.util.module_from_spec(spec)
sys.modules[skill_env] = module
spec.loader.exec_module(module)
if (hasattr(module, 'test_runner') and
callable(module.test_runner) or
hasattr(module, 'test_setup') and
callable(module.test_setup)):
test_env = module
return test_env


def discover_tests():
"""Find skills with test files
Expand Down Expand Up @@ -74,15 +99,7 @@ def discover_tests():
tests[skill] = test_intent_files

# Load test environment script
test_env = None
if exists(os.path.join(skill, 'test/__init__.py')):
module = imp.load_source(skill + '.test_env',
os.path.join(skill, 'test/__init__.py'))
if (hasattr(module, 'test_runner') and
callable(module.test_runner) or
hasattr(module, 'test_setup') and
callable(module.test_setup)):
test_env = module
test_env = load_test_environment(skill)
test_envs[skill] = test_env

return tests, test_envs
Expand Down

0 comments on commit 3eee84f

Please sign in to comment.