]> git.openstreetmap.org Git - osqa.git/blob - forum/modules.py
fixes osqa-54: The user was not being informed that they were trying to submit a...
[osqa.git] / forum / modules.py
1 import os
2 import types
3 import re
4
5 from django.template import Template, TemplateDoesNotExist
6 from django.conf import settings
7
8 MODULES_PACKAGE = 'forum_modules'
9
10 MODULES_FOLDER = os.path.join(os.path.dirname(__file__), '../' + MODULES_PACKAGE)
11
12 DISABLED_MODULES = getattr(settings, 'DISABLED_MODULES', [])
13
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
20 ])
21
22 def get_modules_script(script_name):
23     all = []
24
25     for m in MODULE_LIST:
26         try:
27             all.append(__import__('%s.%s' % (m.__name__, script_name), globals(), locals(), [m.__name__]))
28         except Exception, e:
29             #print m.__name__ + ":" + str(e)
30             pass
31
32     return all
33
34 def get_modules_scipt_implementations(script_name, impl_class):
35     scripts = get_modules_script(script_name)
36     all_impls = {}
37
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)
42         ]))
43
44     return all_impls
45
46 def get_modules_script_classes(script_name, base_class):
47     scripts = get_modules_script(script_name)
48     all_classes = {}
49
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)
54         ]))
55
56     return all_classes
57
58 def get_all_handlers(name):
59      handler_files = get_modules_script('handlers')
60
61      return [
62         h for h in [
63             getattr(f, name) for f in handler_files
64             if hasattr(f, name)
65         ]
66
67         if callable(h)
68      ]
69
70 def call_all_handlers(name, *args, **kwargs):
71     all = get_all_handlers(name)
72
73     ret = []
74
75     for handler in all:
76         ret.append(handler(*args, **kwargs))
77
78     return ret
79
80 def get_handler(name, default):
81     all = get_all_handlers(name)
82     return len(all) and all[0] or default
83
84 module_template_re = re.compile('^modules\/(\w+)\/(.*)$')
85
86 def module_templates_loader(name, dirs=None):
87     result = module_template_re.search(name)
88
89     if result is not None:
90         file_name = os.path.join(MODULES_FOLDER, result.group(1), 'templates', result.group(2))
91
92         if os.path.exists(file_name):
93             try:
94                 f = open(file_name, 'r')
95                 source = f.read()
96                 f.close()
97                 return (source, file_name)
98             except:
99                 pass
100
101     raise TemplateDoesNotExist, name 
102
103 module_templates_loader.is_usable = True