]> git.openstreetmap.org Git - osqa.git/blob - forum/modules/__init__.py
Users should be warned before getting their karma points decreased to certain values...
[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 ImportError, e:
20             #print repr(type(e)) + m.__name__ + ":" + str(e)
21             pass
22         except:
23             import traceback
24             msg = "Error importing %s from module %s: \n %s" % (
25                 script_name, m, traceback.format_exc()
26             )
27             logging.error(msg)
28
29     return all
30
31 def get_modules_script_implementations(script_name, impl_class):
32     scripts = get_modules_script(script_name)
33     all_impls = {}
34
35     for script in scripts:
36         all_impls.update(dict([
37             (n, i) for (n, i) in [(n, getattr(script, n)) for n in dir(script)]
38             if isinstance(i, impl_class)
39         ]))
40
41     return all_impls
42
43 def get_modules_script_classes(script_name, base_class):
44     scripts = get_modules_script(script_name)
45     all_classes = {}
46
47     for script in scripts:
48         all_classes.update(dict([
49             (n, c) for (n, c) in [(n, getattr(script, n)) for n in dir(script)]
50             if isinstance(c, (type, types.ClassType)) and issubclass(c, base_class)
51         ]))
52
53     return all_classes
54
55 def get_all_handlers(name):
56      handler_files = get_modules_script('handlers')
57
58      return [
59         h for h in [
60             getattr(f, name) for f in handler_files
61             if hasattr(f, name)
62         ]
63
64         if callable(h)
65      ]
66
67 def call_all_handlers(name, *args, **kwargs):
68     all = get_all_handlers(name)
69
70     ret = []
71
72     for handler in all:
73         ret.append(handler(*args, **kwargs))
74
75     return ret
76
77 def get_handler(name, default):
78     all = get_all_handlers(name)
79     return len(all) and all[0] or default
80
81 from decorators import decorate, ReturnImediatelyException