6 from django.template import Template, TemplateDoesNotExist
7 from django.conf import settings
9 MODULES_PACKAGE = 'forum_modules'
11 MODULES_FOLDER = os.path.join(os.path.dirname(__file__), '../../' + MODULES_PACKAGE)
13 DISABLED_MODULES = getattr(settings, 'DISABLED_MODULES', [])
15 MODULE_LIST = filter(lambda m: getattr(m, 'CAN_USE', True), [
16 __import__('forum_modules.%s' % f, globals(), locals(), ['forum_modules'])
17 for f in os.listdir(MODULES_FOLDER)
18 if os.path.isdir(os.path.join(MODULES_FOLDER, f)) and
19 os.path.exists(os.path.join(MODULES_FOLDER, "%s/__init__.py" % f)) and
20 not f in DISABLED_MODULES
23 def get_modules_script(script_name):
28 all.append(__import__('%s.%s' % (m.__name__, script_name), globals(), locals(), [m.__name__]))
29 except ImportError, e:
30 #print repr(type(e)) + m.__name__ + ":" + str(e)
34 msg = "Error importing %s from module %s: \n %s" % (
35 script_name, m.__name__, traceback.format_exc()
41 def get_modules_script_implementations(script_name, impl_class):
42 scripts = get_modules_script(script_name)
45 for script in scripts:
46 all_impls.update(dict([
47 (n, i) for (n, i) in [(n, getattr(script, n)) for n in dir(script)]
48 if isinstance(i, impl_class)
53 def get_modules_script_classes(script_name, base_class):
54 scripts = get_modules_script(script_name)
57 for script in scripts:
58 all_classes.update(dict([
59 (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]
60 if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)
65 def get_all_handlers(name):
66 handler_files = get_modules_script('handlers')
70 getattr(f, name) for f in handler_files
77 def call_all_handlers(name, *args, **kwargs):
78 all = get_all_handlers(name)
83 ret.append(handler(*args, **kwargs))
87 def get_handler(name, default):
88 all = get_all_handlers(name)
89 return len(all) and all[0] or default
91 module_template_re = re.compile('^modules\/(\w+)\/(.*)$')
93 def module_templates_loader(name, dirs=None):
94 result = module_template_re.search(name)
96 if result is not None:
97 file_name = os.path.join(MODULES_FOLDER, result.group(1), 'templates', result.group(2))
99 if os.path.exists(file_name):
101 f = open(file_name, 'r')
104 return (source, file_name)
108 raise TemplateDoesNotExist, name
110 module_templates_loader.is_usable = True
112 from decorators import decorate, ReturnImediatelyException