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 logging.error("Error importing %s from module %s: \n %s" % (
35 script_name, m.__name__, traceback.format_exc()
40 def get_modules_scipt_implementations(script_name, impl_class):
41 scripts = get_modules_script(script_name)
44 for script in scripts:
45 all_impls.update(dict([
46 (n, i) for (n, i) in [(n, getattr(script, n)) for n in dir(script)]
47 if isinstance(i, impl_class)
52 def get_modules_script_classes(script_name, base_class):
53 scripts = get_modules_script(script_name)
56 for script in scripts:
57 all_classes.update(dict([
58 (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]
59 if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)
64 def get_all_handlers(name):
65 handler_files = get_modules_script('handlers')
69 getattr(f, name) for f in handler_files
76 def call_all_handlers(name, *args, **kwargs):
77 all = get_all_handlers(name)
82 ret.append(handler(*args, **kwargs))
86 def get_handler(name, default):
87 all = get_all_handlers(name)
88 return len(all) and all[0] or default
90 module_template_re = re.compile('^modules\/(\w+)\/(.*)$')
92 def module_templates_loader(name, dirs=None):
93 result = module_template_re.search(name)
95 if result is not None:
96 file_name = os.path.join(MODULES_FOLDER, result.group(1), 'templates', result.group(2))
98 if os.path.exists(file_name):
100 f = open(file_name, 'r')
103 return (source, file_name)
107 raise TemplateDoesNotExist, name
109 module_templates_loader.is_usable = True
111 from decorators import decorate, ReturnImediatelyException