diff options
| -rw-r--r-- | client/brain.py | 15 | ||||
| -rw-r--r-- | client/test.py | 23 |
2 files changed, 25 insertions, 13 deletions
diff --git a/client/brain.py b/client/brain.py index f2a2efd..1ed6db5 100644 --- a/client/brain.py +++ b/client/brain.py @@ -32,13 +32,22 @@ class Brain(object): module, a priority of 0 is assumed. """ + logger = logging.getLogger(__name__) module_locations = [jasperpath.PLUGIN_PATH] + logger.debug("Looking for modules in: %s", ', '.join(["'%s'" % location for location in module_locations])) module_names = [name for loader, name, ispkg in pkgutil.walk_packages(module_locations, prefix='modules.')] modules = [] for name in module_names: - mod = importlib.import_module(name) - if hasattr(mod, 'WORDS'): - modules.append(mod) + try: + mod = importlib.import_module(name) + except: + logger.warning("Skipped module '%s' due to an error.", name, exc_info=True) + else: + if hasattr(mod, 'WORDS'): + logger.debug("Found module '%s' with words: %r", name, mod.WORDS) + modules.append(mod) + else: + logger.warning("Skipped module '%s' because it misses the WORDS constant.", name) modules.sort(key=lambda mod: mod.PRIORITY if hasattr(mod, 'PRIORITY') else 0, reverse=True) return modules diff --git a/client/test.py b/client/test.py index 184a777..0ad37a3 100644 --- a/client/test.py +++ b/client/test.py @@ -4,7 +4,7 @@ import os import sys import unittest import argparse -from mock import patch +from mock import patch, Mock from urllib2 import URLError, urlopen import test_mic @@ -37,20 +37,23 @@ class TestVocabCompiler(unittest.TestCase): languagemodel = "temp_languagemodel.lm" words = [ - 'HACKER', 'LIFE', 'FACEBOOK', 'THIRD', 'NO', 'JOKE', - 'NOTIFICATION', 'MEANING', 'TIME', 'TODAY', 'SECOND', - 'BIRTHDAY', 'KNOCK KNOCK', 'INBOX', 'OF', 'NEWS', 'YES', - 'TOMORROW', 'EMAIL', 'WEATHER', 'FIRST', 'MUSIC', 'SPOTIFY' + 'MUSIC', 'SPOTIFY' ] + mock_module = Mock() + mock_module.WORDS = [ + 'MOCK' + ] + + words.extend(mock_module.WORDS) + with patch.object(g2p, 'translateWords') as translateWords: with patch.object(vocabcompiler, 'text2lm') as text2lm: - vocabcompiler.compile(sentences, dictionary, languagemodel) + with patch.object(brain.Brain, 'get_modules', classmethod(lambda cls: [mock_module])) as modules: + vocabcompiler.compile(sentences, dictionary, languagemodel) - # 'words' is appended with ['MUSIC', 'SPOTIFY'] - # so must be > 2 to have received WORDS from modules - translateWords.assert_called_once_with(UnorderedList(words)) - self.assertTrue(text2lm.called) + translateWords.assert_called_once_with(UnorderedList(words)) + self.assertTrue(text2lm.called) os.remove(sentences) os.remove(dictionary) |
