]> git.openstreetmap.org Git - osqa.git/blob - forum/modules/__init__.py
Migrate to Django 1.6
[osqa.git] / forum / modules / __init__.py
1 import types
2 import logging
3
4 from django.conf import settings
5
6 def get_modules_folder():
7     return get_modules_folder.value
8
9 def get_modules_script(script_name):
10     all = []
11
12     for m in settings.MODULE_LIST:
13         if hasattr(m, script_name):
14             all.append(getattr(m, script_name))
15             continue
16
17         try:
18             all.append(__import__('%s.%s' % (m.__name__, script_name), globals(), locals(), [m.__name__]))
19         except Exception, e:
20             if isinstance(e, ImportError) and str(e).endswith(script_name):
21                 continue
22             logging.exception("Error importing %s from module %s", script_name, m)
23
24     return all
25
26 def get_modules_script_implementations(script_name, impl_class):
27     scripts = get_modules_script(script_name)
28     all_impls = {}
29
30     for script in scripts:
31         all_impls.update(dict([
32             (n, i) for (n, i) in [(n, getattr(script, n)) for n in dir(script)]
33             if isinstance(i, impl_class)
34         ]))
35
36     return all_impls
37
38 def get_modules_script_classes(script_name, base_class):
39     scripts = get_modules_script(script_name)
40     all_classes = {}
41
42     for script in scripts:
43         all_classes.update(dict([
44             (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]
45             if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)
46         ]))
47
48     return all_classes
49
50 def get_all_handlers(name):
51      handler_files = get_modules_script('handlers')
52
53      return [
54         h for h in [
55             getattr(f, name) for f in handler_files
56             if hasattr(f, name)
57         ]
58
59         if callable(h)
60      ]
61
62 def call_all_handlers(name, *args, **kwargs):
63     all = get_all_handlers(name)
64
65     ret = []
66
67     for handler in all:
68         ret.append(handler(*args, **kwargs))
69
70     return ret
71
72 def get_handler(name, default):
73     all = get_all_handlers(name)
74     return len(all) and all[0] or default
75
76 from decorators import decorate, ReturnImediatelyException