8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
10 # FOR A PARTICULAR PURPOSE. |
10 # FOR A PARTICULAR PURPOSE. |
11 # |
11 # |
12 |
12 |
|
13 """PyAMS_utils.wsgi module |
|
14 |
|
15 This module provides a method decorator which can store it's value into request environment |
|
16 """ |
|
17 |
13 __docformat__ = 'restructuredtext' |
18 __docformat__ = 'restructuredtext' |
14 |
|
15 # import standard library |
|
16 |
|
17 # import interfaces |
|
18 |
|
19 # import packages |
|
20 |
19 |
21 |
20 |
22 def wsgi_environ_cache(*names): |
21 def wsgi_environ_cache(*names): |
23 """Wrap a function/method to cache its result for call into request.environ |
22 """Wrap a function/method to cache its result for call into request.environ |
24 |
23 |
25 :param [string...] names: keys to cache into environ; len(names) must |
24 :param [string...] names: keys to cache into environ; len(names) must |
26 be equal to the result's length or scalar |
25 be equal to the result's length or scalar |
27 """ |
26 """ |
28 def decorator(fn): |
27 |
|
28 def decorator(func): |
|
29 |
29 def function_wrapper(self, request): |
30 def function_wrapper(self, request): |
30 scalar = len(names) == 1 |
31 scalar = len(names) == 1 |
31 try: |
32 try: |
32 rs = [request.environ[cached_key] for cached_key in names] |
33 env = [request.environ[cached_key] for cached_key in names] |
33 except KeyError: |
34 except KeyError: |
34 rs = fn(self, request) |
35 env = func(self, request) |
35 if scalar: |
36 if scalar: |
36 rs = [rs, ] |
37 env = [env, ] |
37 request.environ.update(zip(names, rs)) |
38 request.environ.update(zip(names, env)) |
38 return rs[0] if scalar else rs |
39 return env[0] if scalar else env |
|
40 |
39 return function_wrapper |
41 return function_wrapper |
40 |
42 |
41 return decorator |
43 return decorator |