5 from django.template import Template, TemplateDoesNotExist
\r
7 MODULES_PACKAGE = 'forum_modules'
\r
9 MODULES_FOLDER = os.path.join(os.path.dirname(__file__), '../' + MODULES_PACKAGE)
\r
12 __import__('forum_modules.%s' % f, globals(), locals(), ['forum_modules'])
\r
13 for f in os.listdir(MODULES_FOLDER)
\r
14 if os.path.isdir(os.path.join(MODULES_FOLDER, f)) and
\r
15 os.path.exists(os.path.join(MODULES_FOLDER, "%s/__init__.py" % f)) and
\r
16 not os.path.exists(os.path.join(MODULES_FOLDER, "%s/DISABLED" % f))
\r
19 def get_modules_script(script_name):
\r
22 for m in MODULE_LIST:
\r
24 all.append(__import__('%s.%s' % (m.__name__, script_name), globals(), locals(), [m.__name__]))
\r
25 except Exception, e:
\r
26 #print script_name + ":" + str(e)
\r
31 def get_modules_script_classes(script_name, base_class):
\r
32 scripts = get_modules_script(script_name)
\r
35 for script in scripts:
\r
36 all_classes.update(dict([
\r
37 (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]
\r
38 if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)
\r
43 def get_all_handlers(name):
\r
44 handler_files = get_modules_script('handlers')
\r
48 getattr(f, name) for f in handler_files
\r
55 def get_handler(name, default):
\r
56 all = get_all_handlers(name)
\r
58 return len(all) and all[0] or default
\r
60 module_template_re = re.compile('^modules\/(\w+)\/(.*)$')
\r
62 def module_templates_loader(name, dirs=None):
\r
63 result = module_template_re.search(name)
\r
65 if result is not None:
\r
66 file_name = os.path.join(MODULES_FOLDER, result.group(1), 'templates', result.group(2))
\r
68 if os.path.exists(file_name):
\r
70 f = open(file_name, 'r')
\r
73 return (source, file_name)
\r
77 raise TemplateDoesNotExist, name
\r
79 module_templates_loader.is_usable = True