]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/modules/__init__.py
last minute fix, bad import
[osqa.git] / forum / modules / __init__.py
index f17d00367d702d5d0af80d630e3055f4dca9c3c5..a5fdaa6745b5b75800f60fa6d493c5026c016f01 100644 (file)
@@ -1,40 +1,51 @@
 import os
 import types
-import re
-
-from django.template import Template, TemplateDoesNotExist
-from django.conf import settings
+import logging
 
 MODULES_PACKAGE = 'forum_modules'
 
-MODULES_FOLDER = os.path.join(os.path.dirname(__file__), '../../' + MODULES_PACKAGE)
+MODULE_LIST = []
+
+
+def init_modules_engine(site_src_root, disabled_modules):
+    modules_folder = os.path.join(site_src_root, MODULES_PACKAGE)
 
-DISABLED_MODULES = getattr(settings, 'DISABLED_MODULES', [])
+    MODULE_LIST.extend(filter(lambda m: getattr(m, 'CAN_USE', True), [
+            __import__('forum_modules.%s' % f, globals(), locals(), ['forum_modules'])
+            for f in os.listdir(modules_folder)
+            if os.path.isdir(os.path.join(modules_folder, f)) and
+               os.path.exists(os.path.join(modules_folder, "%s/__init__.py" % f)) and
+               not f in disabled_modules
+    ]))
 
-MODULE_LIST = filter(lambda m: getattr(m, 'CAN_USE', True), [
-        __import__('forum_modules.%s' % f, globals(), locals(), ['forum_modules'])
-        for f in os.listdir(MODULES_FOLDER)
-        if os.path.isdir(os.path.join(MODULES_FOLDER, f)) and
-           os.path.exists(os.path.join(MODULES_FOLDER, "%s/__init__.py" % f)) and
-           not f in DISABLED_MODULES
-])
+    get_modules_folder.value = modules_folder
+
+def get_modules_folder():
+    return get_modules_folder.value
 
 def get_modules_script(script_name):
     all = []
 
     for m in MODULE_LIST:
+        if hasattr(m, script_name):
+            all.append(getattr(m, script_name))
+            continue
+
         try:
             all.append(__import__('%s.%s' % (m.__name__, script_name), globals(), locals(), [m.__name__]))
         except ImportError, e:
-            #repr(type(e)) + m.__name__ + ":" + str(e)
+            #print repr(type(e)) + m.__name__ + ":" + str(e)
             pass
         except:
-            import sys, traceback
-            traceback.print_exc(file=sys.stdout)
+            import traceback
+            msg = "Error importing %s from module %s: \n %s" % (
+                script_name, m, traceback.format_exc()
+            )
+            logging.error(msg)
 
     return all
 
-def get_modules_scipt_implementations(script_name, impl_class):
+def get_modules_script_implementations(script_name, impl_class):
     scripts = get_modules_script(script_name)
     all_impls = {}
 
@@ -84,23 +95,4 @@ def get_handler(name, default):
     all = get_all_handlers(name)
     return len(all) and all[0] or default
 
-module_template_re = re.compile('^modules\/(\w+)\/(.*)$')
-
-def module_templates_loader(name, dirs=None):
-    result = module_template_re.search(name)
-
-    if result is not None:
-        file_name = os.path.join(MODULES_FOLDER, result.group(1), 'templates', result.group(2))
-
-        if os.path.exists(file_name):
-            try:
-                f = open(file_name, 'r')
-                source = f.read()
-                f.close()
-                return (source, file_name)
-            except:
-                pass
-
-    raise TemplateDoesNotExist, name 
-
-module_templates_loader.is_usable = True
+from decorators import decorate, ReturnImediatelyException