5 from django.template import Template, TemplateDoesNotExist
6 from django.conf import settings
8 MODULES_PACKAGE = 'forum_modules'
10 MODULES_FOLDER = os.path.join(os.path.dirname(__file__), '../../' + MODULES_PACKAGE)
12 DISABLED_MODULES = getattr(settings, 'DISABLED_MODULES', [])
14 MODULE_LIST = filter(lambda m: getattr(m, 'CAN_USE', True), [
15 __import__('forum_modules.%s' % f, globals(), locals(), ['forum_modules'])
16 for f in os.listdir(MODULES_FOLDER)
17 if os.path.isdir(os.path.join(MODULES_FOLDER, f)) and
18 os.path.exists(os.path.join(MODULES_FOLDER, "%s/__init__.py" % f)) and
19 not f in DISABLED_MODULES
22 def get_modules_script(script_name):
27 all.append(__import__('%s.%s' % (m.__name__, script_name), globals(), locals(), [m.__name__]))
29 #print m.__name__ + ":" + str(e)
34 def get_modules_scipt_implementations(script_name, impl_class):
35 scripts = get_modules_script(script_name)
38 for script in scripts:
39 all_impls.update(dict([
40 (n, i) for (n, i) in [(n, getattr(script, n)) for n in dir(script)]
41 if isinstance(i, impl_class)
46 def get_modules_script_classes(script_name, base_class):
47 scripts = get_modules_script(script_name)
50 for script in scripts:
51 all_classes.update(dict([
52 (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]
53 if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)
58 def get_all_handlers(name):
59 handler_files = get_modules_script('handlers')
63 getattr(f, name) for f in handler_files
70 def call_all_handlers(name, *args, **kwargs):
71 all = get_all_handlers(name)
76 ret.append(handler(*args, **kwargs))
80 def get_handler(name, default):
81 all = get_all_handlers(name)
82 return len(all) and all[0] or default
84 module_template_re = re.compile('^modules\/(\w+)\/(.*)$')
86 def module_templates_loader(name, dirs=None):
87 result = module_template_re.search(name)
89 if result is not None:
90 file_name = os.path.join(MODULES_FOLDER, result.group(1), 'templates', result.group(2))
92 if os.path.exists(file_name):
94 f = open(file_name, 'r')
97 return (source, file_name)
101 raise TemplateDoesNotExist, name
103 module_templates_loader.is_usable = True