summaryrefslogtreecommitdiff
path: root/client/brain.py
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2014-05-24 18:34:54 -0700
committerschneefux <schneefux+commit@schneefux.xyz>2014-05-24 19:50:22 -0700
commite5c3058a64ae6e27d2bd323a18f02c26df1cdfdf (patch)
treef1085845632f39c091ee54efddafa6e452b9dc61 /client/brain.py
parentf49c96dcd06eced0bf468543324a76544b73ab74 (diff)
downloadjasper-client-e5c3058a64ae6e27d2bd323a18f02c26df1cdfdf.tar.gz
jasper-client-e5c3058a64ae6e27d2bd323a18f02c26df1cdfdf.zip
dynamic module loading with priorities
Diffstat (limited to 'client/brain.py')
-rw-r--r--client/brain.py41
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):
"""