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 #import sys, traceback
34 #traceback.print_exc(file=sys.stdout)
38 def get_modules_scipt_implementations(script_name, impl_class):
39 scripts = get_modules_script(script_name)
42 for script in scripts:
43 all_impls.update(dict([
44 (n, i) for (n, i) in [(n, getattr(script, n)) for n in dir(script)]
45 if isinstance(i, impl_class)
50 def get_modules_script_classes(script_name, base_class):
51 scripts = get_modules_script(script_name)
54 for script in scripts:
55 all_classes.update(dict([
56 (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]
57 if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)
62 def get_all_handlers(name):
63 handler_files = get_modules_script('handlers')
67 getattr(f, name) for f in handler_files
74 def call_all_handlers(name, *args, **kwargs):
75 all = get_all_handlers(name)
80 ret.append(handler(*args, **kwargs))
84 def get_handler(name, default):
85 all = get_all_handlers(name)
86 return len(all) and all[0] or default
88 module_template_re = re.compile('^modules\/(\w+)\/(.*)$')
90 def module_templates_loader(name, dirs=None):
91 result = module_template_re.search(name)
93 if result is not None:
94 file_name = os.path.join(MODULES_FOLDER, result.group(1), 'templates', result.group(2))
96 if os.path.exists(file_name):
98 f = open(file_name, 'r')
101 return (source, file_name)
105 raise TemplateDoesNotExist, name
107 module_templates_loader.is_usable = True