-import os\r
-import types\r
-import re\r
-\r
-from django.template import Template, TemplateDoesNotExist\r
-\r
-MODULES_PACKAGE = 'forum_modules'\r
-\r
-MODULES_FOLDER = os.path.join(os.path.dirname(__file__), '../' + MODULES_PACKAGE)\r
-\r
-MODULE_LIST = [\r
- __import__('forum_modules.%s' % f, globals(), locals(), ['forum_modules'])\r
- for f in os.listdir(MODULES_FOLDER)\r
- if os.path.isdir(os.path.join(MODULES_FOLDER, f)) and\r
- os.path.exists(os.path.join(MODULES_FOLDER, "%s/__init__.py" % f)) and\r
- not os.path.exists(os.path.join(MODULES_FOLDER, "%s/DISABLED" % f))\r
-]\r
-\r
-def get_modules_script(script_name):\r
- all = []\r
-\r
- for m in MODULE_LIST:\r
- try:\r
- all.append(__import__('%s.%s' % (m.__name__, script_name), globals(), locals(), [m.__name__]))\r
- except Exception, e:\r
- #print script_name + ":" + str(e)\r
- pass\r
-\r
- return all\r
-\r
-def get_modules_script_classes(script_name, base_class):\r
- scripts = get_modules_script(script_name)\r
- all_classes = {}\r
-\r
- for script in scripts:\r
- all_classes.update(dict([\r
- (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]\r
- if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)\r
- ]))\r
-\r
- return all_classes\r
-\r
-def get_all_handlers(name):\r
- handler_files = get_modules_script('handlers')\r
-\r
- return [\r
- h for h in [\r
- getattr(f, name) for f in handler_files\r
- if hasattr(f, name)\r
- ]\r
-\r
- if callable(h)\r
- ]\r
-\r
-def get_handler(name, default):\r
- all = get_all_handlers(name)\r
- print(len(all))\r
- return len(all) and all[0] or default\r
-\r
-module_template_re = re.compile('^modules\/(\w+)\/(.*)$')\r
-\r
-def module_templates_loader(name, dirs=None):\r
- result = module_template_re.search(name)\r
-\r
- if result is not None:\r
- file_name = os.path.join(MODULES_FOLDER, result.group(1), 'templates', result.group(2))\r
-\r
- if os.path.exists(file_name):\r
- try:\r
- f = open(file_name, 'r')\r
- source = f.read()\r
- f.close()\r
- return (source, file_name)\r
- except:\r
- pass\r
-\r
- raise TemplateDoesNotExist, name \r
-\r
-module_templates_loader.is_usable = True
\ No newline at end of file
+import os
+import types
+import re
+
+from django.template import Template, TemplateDoesNotExist
+from django.conf import settings
+
+MODULES_PACKAGE = 'forum_modules'
+
+MODULES_FOLDER = os.path.join(os.path.dirname(__file__), '../' + MODULES_PACKAGE)
+
+DISABLED_MODULES = getattr(settings, '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
+])
+
+def get_modules_script(script_name):
+ all = []
+
+ for m in MODULE_LIST:
+ try:
+ all.append(__import__('%s.%s' % (m.__name__, script_name), globals(), locals(), [m.__name__]))
+ except Exception, e:
+ #print m.__name__ + ":" + str(e)
+ pass
+
+ return all
+
+def get_modules_scipt_implementations(script_name, impl_class):
+ scripts = get_modules_script(script_name)
+ all_impls = {}
+
+ for script in scripts:
+ all_impls.update(dict([
+ (n, i) for (n, i) in [(n, getattr(script, n)) for n in dir(script)]
+ if isinstance(i, impl_class)
+ ]))
+
+ return all_impls
+
+def get_modules_script_classes(script_name, base_class):
+ scripts = get_modules_script(script_name)
+ all_classes = {}
+
+ for script in scripts:
+ all_classes.update(dict([
+ (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]
+ if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)
+ ]))
+
+ return all_classes
+
+def get_all_handlers(name):
+ handler_files = get_modules_script('handlers')
+
+ return [
+ h for h in [
+ getattr(f, name) for f in handler_files
+ if hasattr(f, name)
+ ]
+
+ if callable(h)
+ ]
+
+def call_all_handlers(name, *args, **kwargs):
+ all = get_all_handlers(name)
+
+ ret = []
+
+ for handler in all:
+ ret.append(handler(*args, **kwargs))
+
+ return ret
+
+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