diff options
Diffstat (limited to 'client/brain.py')
| -rw-r--r-- | client/brain.py | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/client/brain.py b/client/brain.py index b5e0c1d..2e7607a 100644 --- a/client/brain.py +++ b/client/brain.py @@ -1,5 +1,5 @@ import logging -from modules import * +from os import listdir def logError(): @@ -25,11 +25,44 @@ class Brain(object): mic -- used to interact with the user (for both input and output) profile -- contains information related to the user (e.g., phone number) """ + + def get_modules(): + """ + Dynamically loads all the modules in the modules folder and sorts + them by the PRIORITY key. If no PRIORITY is defined for a given + module, a priority of 0 is assumed. + """ + + folder = 'modules' + + def get_module_names(): + module_names = [m.replace('.py', '') + for m in listdir(folder) if m.endswith('.py')] + module_names.remove('__init__') + module_names.remove('app_utils') + module_names = map(lambda s: folder + '.' + s, module_names) + return module_names + + def import_module(name): + mod = __import__(name) + components = name.split('.') + for comp in components[1:]: + mod = getattr(mod, comp) + return mod + + def get_module_priority(m): + try: + return m.PRIORITY + except: + return 0 + + modules = map(import_module, get_module_names()) + modules.sort(key=get_module_priority, reverse=True) + return modules + self.mic = mic self.profile = profile - self.modules = [ - Gmail, Notifications, Birthday, Weather, HN, News, Time, Joke, Life] - self.modules.append(Unclear) + self.modules = get_modules() def query(self, text): """ |
