1 | from Products.CacheSetup.config import log |
---|
2 | |
---|
3 | PATTERN = '__CacheSetup_%s__' |
---|
4 | |
---|
5 | def call(self, __name__, *args, **kw): |
---|
6 | return getattr(self, PATTERN % __name__)(*args, **kw) |
---|
7 | |
---|
8 | def call_pattern(self, __name__, pattern=PATTERN, *args, **kw): |
---|
9 | return getattr(self, pattern % __name__)(*args, **kw) |
---|
10 | |
---|
11 | |
---|
12 | WRAPPER = '__CacheSetup_is_wrapper_method__' |
---|
13 | ORIG_NAME = '__CacheSetup_original_method_name__' |
---|
14 | |
---|
15 | def isWrapperMethod(meth): |
---|
16 | return getattr(meth, WRAPPER, False) |
---|
17 | |
---|
18 | def wrap_method(klass, name, method, pattern=PATTERN): |
---|
19 | old_method = getattr(klass, name) |
---|
20 | if isWrapperMethod(old_method): |
---|
21 | log('Not wrapping %s.%s. Already wrapped.' % |
---|
22 | (klass.__name__, name)) |
---|
23 | return |
---|
24 | else: |
---|
25 | log('Wrapping %s.%s.' % |
---|
26 | (klass.__name__, name)) |
---|
27 | new_name = pattern % name |
---|
28 | setattr(klass, new_name, old_method) |
---|
29 | setattr(method, ORIG_NAME, new_name) |
---|
30 | setattr(method, WRAPPER, True) |
---|
31 | setattr(klass, name, method) |
---|
32 | |
---|
33 | def unwrap_method(klass, name): |
---|
34 | old_method = getattr(klass, name) |
---|
35 | if not isWrapperMethod(old_method): |
---|
36 | raise ValueError, ('Trying to unwrap non-wrapped ' |
---|
37 | 'method %s.%s.' % (klass.__name__, name)) |
---|
38 | orig_name = getattr(old_method, ORIG_NAME) |
---|
39 | new_method = getattr(klass, orig_name) |
---|
40 | delattr(klass, orig_name) |
---|
41 | setattr(klass, name, new_method) |
---|