summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2014-09-11 15:48:13 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2014-09-11 15:48:25 +0200
commit5be928790db5b7a65b306bf0b4f535f2d321e8da (patch)
tree4b43e111eaeddffcdc0dac093e445c5bf65653ac
parentfa926e876485dfe81b288ac3ee8025c2c7f59895 (diff)
downloadjasper-client-5be928790db5b7a65b306bf0b4f535f2d321e8da.tar.gz
jasper-client-5be928790db5b7a65b306bf0b4f535f2d321e8da.zip
Cleanup of module detection in client.brain
This should also make it possible to have packages as client modules (making jasperproject/jasper-client#153 obsolete)
-rw-r--r--client/brain.py58
1 files changed, 23 insertions, 35 deletions
diff --git a/client/brain.py b/client/brain.py
index b6014de..7da7439 100644
--- a/client/brain.py
+++ b/client/brain.py
@@ -1,6 +1,9 @@
# -*- coding: utf-8-*-
import logging
-from os import listdir
+import os
+import sys
+import pkgutil
+import importlib
def logError():
@@ -27,42 +30,27 @@ class Brain(object):
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 = 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 = filter(lambda m: hasattr(m, 'WORDS'), modules)
- modules.sort(key=get_module_priority, reverse=True)
- return modules
-
self.mic = mic
self.profile = profile
- self.modules = get_modules()
+ self.modules = self.get_modules()
+
+ @classmethod
+ def get_modules(cls):
+ """
+ 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.
+ """
+
+ module_locations = [os.path.join(os.getenv('JASPER_HOME'), 'client', 'modules')]
+ module_names = [name for loader, name, ispkg in pkgutil.iter_modules(module_locations)]
+ modules = []
+ for name in module_names:
+ mod = importlib.import_module("client.modules.%s" % name)
+ if hasattr(mod, 'WORDS'):
+ modules.append(mod)
+ modules.sort(key=lambda mod: mod.PRIORITY if hasattr(mod,'PRIORITY') else 0, reverse=True)
+ return modules
def query(self, text):
"""