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__]))
28 except ImportError, e:
29 #repr(type(e)) + m.__name__ + ":" + str(e)
33 traceback.print_exc(file=sys.stdout)
37 def get_modules_scipt_implementations(script_name, impl_class):
38 scripts = get_modules_script(script_name)
41 for script in scripts:
42 all_impls.update(dict([
43 (n, i) for (n, i) in [(n, getattr(script, n)) for n in dir(script)]
44 if isinstance(i, impl_class)
49 def get_modules_script_classes(script_name, base_class):
50 scripts = get_modules_script(script_name)
53 for script in scripts:
54 all_classes.update(dict([
55 (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]
56 if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)
61 def get_all_handlers(name):
62 handler_files = get_modules_script('handlers')
66 getattr(f, name) for f in handler_files
73 def call_all_handlers(name, *args, **kwargs):
74 all = get_all_handlers(name)
79 ret.append(handler(*args, **kwargs))
83 def get_handler(name, default):
84 all = get_all_handlers(name)
85 return len(all) and all[0] or default
87 module_template_re = re.compile('^modules\/(\w+)\/(.*)$')
89 def module_templates_loader(name, dirs=None):
90 result = module_template_re.search(name)
92 if result is not None:
93 file_name = os.path.join(MODULES_FOLDER, result.group(1), 'templates', result.group(2))
95 if os.path.exists(file_name):
97 f = open(file_name, 'r')
100 return (source, file_name)
104 raise TemplateDoesNotExist, name
106 module_templates_loader.is_usable = True