summaryrefslogtreecommitdiff
path: root/client
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
parentf49c96dcd06eced0bf468543324a76544b73ab74 (diff)
downloadjasper-client-e5c3058a64ae6e27d2bd323a18f02c26df1cdfdf.tar.gz
jasper-client-e5c3058a64ae6e27d2bd323a18f02c26df1cdfdf.zip
dynamic module loading with priorities
Diffstat (limited to 'client')
-rw-r--r--client/brain.py41
-rw-r--r--client/modules/HN.py2
-rw-r--r--client/modules/News.py2
-rw-r--r--client/modules/Unclear.py3
4 files changed, 44 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):
"""
diff --git a/client/modules/HN.py b/client/modules/HN.py
index 692abea..493fa1d 100644
--- a/client/modules/HN.py
+++ b/client/modules/HN.py
@@ -7,6 +7,8 @@ from semantic.numbers import NumberService
WORDS = ["HACKER", "NEWS", "YES", "NO", "FIRST", "SECOND", "THIRD"]
+PRIORITY = 4
+
URL = 'http://news.ycombinator.com'
diff --git a/client/modules/News.py b/client/modules/News.py
index 0cedd3a..59495a3 100644
--- a/client/modules/News.py
+++ b/client/modules/News.py
@@ -5,6 +5,8 @@ from semantic.numbers import NumberService
WORDS = ["NEWS", "YES", "NO", "FIRST", "SECOND", "THIRD"]
+PRIORITY = 3
+
URL = 'http://news.ycombinator.com'
diff --git a/client/modules/Unclear.py b/client/modules/Unclear.py
index 39f56d0..23ab2ed 100644
--- a/client/modules/Unclear.py
+++ b/client/modules/Unclear.py
@@ -1,7 +1,10 @@
+from sys import maxint
import random
WORDS = []
+PRIORITY = -(maxint + 1)
+
def handle(text, mic, profile):
"""