/
decorators.py
850 lines (651 loc) · 29.8 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# -*- coding: utf-8 -*-
"""
Decorators use by the TurboGears controllers.
Not all of these decorators are traditional wrappers. They are much simplified
from the TurboGears 1 decorators, because all they do is register attributes on
the functions they wrap, and then the DecoratedController provides the hooks
needed to support these decorators.
"""
from warnings import warn
from decorator import decorator
from formencode import variabledecode
from paste.util.mimeparse import best_match
from pylons.configuration import config
from pylons import request, response
from pylons.controllers.util import abort
from webob.exc import HTTPMethodNotAllowed
from tg import tmpl_context
from tg.paginate import Page
from tg.util import Bunch, partial
from tg.configuration.sqla.balanced_session import force_request_engine
from tg.flash import flash
from tg.predicates import NotAuthorizedError
from pylons.decorators.cache import beaker_cache
import logging
log = logging.getLogger(__name__)
class Decoration(object):
""" Simple class to support 'simple registration' type decorators
"""
def __init__(self, controller):
self.controller = controller
self.engines = {}
self.engines_keys = []
self.custom_engines = {}
self.render_custom_format = None
self.validation = None
self.inherit = False
self.hooks = dict(before_validate=[],
before_call=[],
before_render=[],
after_render=[])
def get_decoration(cls, func):
if not hasattr(func, 'decoration'):
func.decoration = cls(func)
return func.decoration
get_decoration = classmethod(get_decoration)
@property
def exposed(self):
return bool(self.engines) or bool(self.custom_engines)
def merge(self, deco):
self.engines = dict(deco.engines.items() + self.engines.items())
self.engines_keys = sorted(self.engines, reverse=True)
self.custom_engines = dict(deco.custom_engines.items() + self.engines.items())
#inherit all the parent hooks
#parent hooks before current hooks so that they get called before
for hook_name, hooks in deco.hooks.items():
self.hooks[hook_name] = hooks + self.hooks[hook_name]
if not self.validation:
self.validation = deco.validation
def run_hooks(self, hook, *l, **kw):
for func in config.get('hooks', {}).get(hook, []):
func(*l, **kw)
for func in self.hooks[hook]:
func(*l, **kw)
def wrap_controller(self, controller):
controller_callable = controller
for wrapper in config.get('controller_wrappers', []):
controller_callable = wrapper(self, controller_callable)
return controller_callable
def register_template_engine(self,
content_type, engine, template, exclude_names, render_params):
"""Registers an engine on the controller.
Multiple engines can be registered, but only one engine per
content_type. If no content type is specified the engine is
registered at */* which is the default, and will be used
whenever no content type is specified.
exclude_names keeps track of a list of keys which will be
removed from the controller's dictionary before it is loaded
into the template. This allows you to exclude some information
from JSONification, and other 'automatic' engines which don't
require a template.
render_params registers extra parameters which will be sent
to the rendering method. This allows you to influence things
like the rendering method or the injected doctype.
"""
default_renderer = config.get('default_renderer')
available_renderers = config.get('renderers', [])
if engine and not available_renderers:
log.warn('Renderers not registered yet while exposing template %s for engine %s, '
'skipping engine availability check', template, engine)
if engine and available_renderers and engine not in available_renderers:
log.debug('Registering template %s for engine %s not available. Skipping it', template, engine)
return
content_type = content_type or '*/*'
if content_type in self.engines and engine != default_renderer:
#Avoid overwriting the default renderer when there is already a template registered
return
self.engines[content_type] = (engine, template, exclude_names, render_params or {})
# this is a work-around to make text/html prominent in respect
# to other common choices when they have the same weight for
# paste.util.mimeparse.best_match.
self.engines_keys = sorted(self.engines, reverse=True)
def register_custom_template_engine(self, custom_format,
content_type, engine, template, exclude_names, render_params):
"""Registers a custom engine on the controller.
Multiple engines can be registered, but only one engine per
custom_format.
The engine is registered when @expose is used with the
custom_format parameter and controllers render using this
engine when the use_custom_format() function is called
with the corresponding custom_format.
exclude_names keeps track of a list of keys which will be
removed from the controller's dictionary before it is loaded
into the template. This allows you to exclude some information
from JSONification, and other 'automatic' engines which don't
require a template.
render_params registers extra parameters which will be sent
to the rendering method. This allows you to influence things
like the rendering method or the injected doctype.
"""
self.custom_engines[custom_format or '"*/*"'] = (
content_type, engine, template, exclude_names, render_params or {})
def lookup_template_engine(self, request):
"""Return the template engine data.
Provides a convenience method to get the proper engine,
content_type, template, and exclude_names for a particular
tg_format (which is pulled off of the request headers).
"""
if hasattr(request, 'response_type'
) and request.response_type in self.engines:
accept_types = request.response_type
else:
accept_types = request.headers.get('accept', '*/*')
try:
render_custom_format = request._render_custom_format[
self.controller]
except:
render_custom_format = self.render_custom_format
if render_custom_format:
(content_type, engine, template, exclude_names, render_params
) = self.custom_engines[render_custom_format]
else:
if self.engines:
content_type = best_match(self.engines_keys, accept_types)
else:
content_type = 'text/html'
if content_type == 'CUSTOM/LEAVE':
warn('@expose(CUSTOM_CONTENT_TYPE) is no longer needed'
' and should be replaced with @expose()')
# check for overridden content type from the controller call
controller_content_type = response.headers.get('Content-Type')
if controller_content_type:
# make sure we handle types like 'text/html; charset=utf-8'
content_type = controller_content_type.split(';', 1)[0]
# check for overridden templates
try:
(engine, template, exclude_names, render_params
) = request._override_mapping[
self.controller][content_type.split(';', 1)[0]]
except (AttributeError, KeyError):
(engine, template, exclude_names, render_params
) = self.engines.get(content_type, (None,) * 4)
if 'charset' not in content_type and (content_type.startswith('text')
or content_type in ('application/xhtml+xml',
'application/xml', 'application/json')):
content_type = '%s; charset=utf-8' % content_type
return content_type, engine, template, exclude_names, render_params
def register_hook(self, hook_name, func):
"""Registers the specified function as a hook.
We now have four core hooks that can be applied by adding
decorators: before_validate, before_call, before_render, and
after_render. register_hook attaches the function to the hook
which get's called at the appropriate time in the request life
cycle.)
"""
self.hooks[hook_name].append(func)
class _hook_decorator(object):
"""Superclass for all the specific TG2 hook validators.
Its `hook_name` must be overridden by a specific hook.
"""
hook_name = None
def __init__(self, hook_func):
if hasattr(hook_func, '__name__'):
self.__name__ = hook_func.__name__
if hasattr(hook_func, '__doc__'):
self.__doc__ = hook_func.__doc__
self.hook_func = hook_func
def __call__(self, func):
deco = Decoration.get_decoration(func)
deco.register_hook(self.hook_name, self.hook_func)
return func
class before_validate(_hook_decorator):
"""A list of callables to be run before validation is performed."""
hook_name = 'before_validate'
class before_call(_hook_decorator):
"""A list of callables to be run before the controller method is called."""
hook_name = 'before_call'
class before_render(_hook_decorator):
"""A list of callables to be run before the template is rendered."""
hook_name = 'before_render'
class after_render(_hook_decorator):
"""A list of callables to be run after the template is rendered.
Will be run before it is returned returned up the WSGI stack.
"""
hook_name = 'after_render'
class expose(object):
"""Register attributes on the decorated function.
:Parameters:
template
Assign a template, you could use the syntax 'genshi:template'
to use different templates.
The default template engine is genshi.
content_type
Assign content type.
The default content type is 'text/html'.
exclude_names
Assign exclude names
custom_format
Registers as a custom format which can later be activated calling
use_custom_format
render_params
Assign parameters that shall be passed to the rendering method.
inherit
Inherit all the decorations from the same method in the parent
class. This will let the exposed method expose the same template
as the overridden method template and keep the same hooks and
validation that the parent method had.
The expose decorator registers a number of attributes on the
decorated function, but does not actually wrap the function the way
TurboGears 1.0 style expose decorators did.
This means that we don't have to play any kind of special tricks to
maintain the signature of the exposed function.
The exclude_names parameter is new, and it takes a list of keys that
ought to be scrubbed from the dictionary before passing it on to the
rendering engine. This is particularly useful for JSON.
The render_parameters is also new. It takes a dictionary of arguments
that ought to be sent to the rendering engine, like this::
render_params={'method': 'xml', 'doctype': None}
Expose decorator can be stacked like this::
@expose('json', exclude_names='d')
@expose('kid:blogtutorial.templates.test_form',
content_type='text/html')
@expose('kid:blogtutorial.templates.test_form_xml',
content_type='text/xml', custom_format='special_xml')
def my_exposed_method(self):
return dict(a=1, b=2, d="username")
The expose('json') syntax is a special case. json is a
rendering engine, but unlike others it does not require a template,
and expose assumes that it matches content_type='application/json'
If you want to declare a desired content_type in a url, you
can use the mime-type style dotted notation::
"/mypage.json" ==> for json
"/mypage.html" ==> for text/html
"/mypage.xml" ==> for xml.
If you're doing an http post, you can also declare the desired
content type in the accept headers, with standard content type
strings.
By default expose assumes that the template is for html. All other
content_types must be explicitly matched to a template and engine.
The last expose decorator example uses the custom_format parameter
which takes an arbitrary value (in this case 'special_xml').
You can then use the`use_custom_format` function within the method
to decide which of the 'custom_format' registered expose decorators
to use to render the template.
"""
def __init__(self, template='', content_type=None, exclude_names=None,
custom_format=None, render_params=None, inherit=False):
if exclude_names is None:
exclude_names = []
if template in config.get('renderers', []):
engine, template = template, ''
elif ':' in template:
engine, template = template.split(':', 1)
elif template:
# Use the default templating engine from the config
engine = config.get('default_renderer')
else:
engine, template = None, None
if content_type is None:
if engine == 'json':
content_type = 'application/json'
else:
content_type = 'text/html'
if engine in ('json', 'amf') and 'tmpl_context' not in exclude_names:
exclude_names.append('tmpl_context')
self.engine = engine
self.template = template
self.content_type = content_type
self.exclude_names = exclude_names
self.custom_format = custom_format
self.render_params = render_params
self.inherit = inherit
def __call__(self, func):
deco = Decoration.get_decoration(func)
if self.inherit:
deco.inherit = True
if not self.template and not self.engine:
return func
if self.custom_format:
deco.register_custom_template_engine(
self.custom_format, self.content_type, self.engine,
self.template, self.exclude_names, self.render_params)
else:
deco.register_template_engine(
self.content_type, self.engine,
self.template, self.exclude_names, self.render_params)
return func
def use_custom_format(controller, custom_format):
"""Use use_custom_format in a controller in order to change
the active @expose decorator when available."""
deco = Decoration.get_decoration(controller)
# Check the custom_format passed is available for use
if custom_format not in deco.custom_engines:
raise ValueError("'%s' is not a valid custom_format" % custom_format)
try:
render_custom_format = request._render_custom_format
except AttributeError:
render_custom_format = request._render_custom_format = {}
render_custom_format[controller.im_func] = custom_format
def override_template(controller, template):
"""Override the template to be used.
Use override_template in a controller in order to change the template
that will be used to render the response dictionary dynamically.
The template string passed in requires that
you include the template engine name, even if you're using the default.
So you have to pass in a template id string like::
"genshi:myproject.templates.index2"
future versions may make the `genshi:` optional if you want to use
the default engine.
"""
try:
engines = controller.decoration.engines
except:
return
for content_type, content_engine in engines.iteritems():
tmpl = template.split(':', 1)
tmpl.extend(content_engine[2:])
try:
override_mapping = request._override_mapping
except AttributeError:
override_mapping = request._override_mapping = {}
override_mapping.setdefault(controller.im_func, {}).update({content_type: tmpl})
class validate(object):
"""Registers which validators ought to be applied.
If you want to validate the contents of your form,
you can use the ``@validate()`` decorator to register
the validators that ought to be called.
:Parameters:
validators
Pass in a dictionary of FormEncode validators.
The keys should match the form field names.
error_handler
Pass in the controller method which shoudl be used
to handle any form errors
form
Pass in a ToscaWidget based form with validators
The first positional parameter can either be a dictonary of validators,
a FormEncode schema validator, or a callable which acts like a FormEncode
validator.
"""
def __init__(self, validators=None, error_handler=None, form=None):
if form:
self.validators = form
if validators:
self.validators = validators
self.error_handler = error_handler
def __call__(self, func):
deco = Decoration.get_decoration(func)
deco.validation = self
return func
class paginate(object):
"""Paginate a given collection.
This decorator is mainly exposing the functionality
of :func:`webhelpers.paginate`.
:Usage:
You use this decorator as follows::
class MyController(object):
@expose()
@paginate("collection")
def sample(self, *args):
collection = get_a_collection()
return dict(collection=collection)
To render the actual pager, use::
${tmpl_context.paginators.<name>.pager()}
It is possible to have several :func:`paginate`-decorators for
one controller action to paginate several collections independently
from each other. If this is desired, don't forget to set the :attr:`use_prefix`-parameter
to :const:`True`.
:Parameters:
name
the collection to be paginated.
items_per_page
the number of items to be rendered. Defaults to 10
max_items_per_page
the maximum number of items allowed to be set via parameter.
Defaults to 0 (does not allow to change that value).
use_prefix
if True, the parameters the paginate
decorator renders and reacts to are prefixed with
"<name>_". This allows for multi-pagination.
"""
def __init__(self, name, use_prefix=False,
items_per_page=10, max_items_per_page=0):
self.name = name
prefix = use_prefix and name + '_' or ''
self.page_param = prefix + 'page'
self.items_per_page_param = prefix + 'items_per_page'
self.items_per_page = items_per_page
self.max_items_per_page = max_items_per_page
def __call__(self, func):
decoration = Decoration.get_decoration(func)
decoration.register_hook('before_validate', self.before_validate)
decoration.register_hook('before_render', self.before_render)
return func
def before_validate(self, remainder, params):
page_param = params.pop(self.page_param, None)
if page_param:
try:
page = int(page_param)
if page < 1:
raise ValueError
except ValueError:
page = 1
else:
page = 1
try:
paginators_data = request.paginators
except:
paginators_data = request.paginators = {'_tg_paginators_params':{}}
paginators_data['_tg_paginators_params'][self.page_param] = page_param
paginators_data[self.name] = paginator = Bunch()
paginator.paginate_page = page or 1
items_per_page = params.pop(self.items_per_page_param, None)
if items_per_page:
try:
items_per_page = min(
int(items_per_page), self.max_items_per_page)
if items_per_page < 1:
raise ValueError
except ValueError:
items_per_page = self.items_per_page
else:
items_per_page = self.items_per_page
paginator.paginate_items_per_page = items_per_page
paginator.paginate_params = params.copy()
paginator.paginate_params.update(paginators_data['_tg_paginators_params'])
if items_per_page != self.items_per_page:
paginator.paginate_params[self.items_per_page_param] = items_per_page
def before_render(self, remainder, params, output):
if not isinstance(output, dict) or not self.name in output:
return
paginator = request.paginators[self.name]
collection = output[self.name]
page = Page(collection, paginator.paginate_page,
paginator.paginate_items_per_page, controller='/')
page.kwargs = paginator.paginate_params
if self.page_param != 'name':
page.pager = partial(page.pager, page_param=self.page_param)
if not getattr(tmpl_context, 'paginators', None):
tmpl_context.paginators = Bunch()
tmpl_context.paginators[self.name] = output[self.name] = page
@decorator
def postpone_commits(func, *args, **kwargs):
"""Turns SQLAlchemy commits into flushes in the decorated method.
This has the end-result of postponing the commit to the normal TG2
transaction boundary. """
#TODO: Test and document this.
s = config.get('DBSession', None)
assert hasattr(s, 'commit')
old_commit = s.commit
s.commit = s.flush
retval = func(*args, **kwargs)
s.commit = old_commit
return retval
@before_validate
def https(remainder, params):
"""Ensure that the decorated method is always called with https."""
from tg.controllers import redirect
if request.scheme.lower() == 'https': return
if request.method.upper() == 'GET':
redirect('https' + request.url[len(request.scheme):])
raise HTTPMethodNotAllowed(headers=dict(Allow='GET')).exception
@before_validate
def variable_decode(remainder, params):
"""Best-effort formencode.variabledecode on the params before validation.
If any exceptions are raised due to invalid parameter names, they are
silently ignored, hopefully to be caught by the actual validator.
Note that this decorator will *add* parameters to the method, not remove.
So for instance a method will move from {'foo-1':'1', 'foo-2':'2'}
to {'foo-1':'1', 'foo-2':'2', 'foo':['1', '2']}.
"""
try:
new_params = variabledecode.variable_decode(params)
params.update(new_params)
except:
pass
@before_validate
def without_trailing_slash(remainder, params):
"""This decorator allows you to ensure that the URL does not end in "/".
The decorator accomplish this by redirecting to the correct URL.
:Usage:
You use this decorator as follows::
class MyController(object):
@without_trailing_slash
@expose()
def sample(self, *args):
return "found sample"
In the above example http://localhost:8080/sample/ redirects to http://localhost:8080/sample
In addition, the URL http://localhost:8080/sample/1/ redirects to http://localhost:8080/sample/1
"""
if request.method == 'GET' and request.path.endswith('/') and not(request.response_type) and len(request.params)==0:
from tg.controllers import redirect
redirect(request.url[:-1])
@before_validate
def with_trailing_slash(remainder, params):
"""This decorator allows you to ensure that the URL ends in "/".
The decorator accomplish this by redirecting to the correct URL.
:Usage:
You use this decorator as follows::
class MyController(object):
@with_trailing_slash
@expose()
def sample(self, *args):
return "found sample"
In the above example http://localhost:8080/sample redirects to http://localhost:8080/sample/
In addition, the URL http://localhost:8080/sample/1 redirects to http://localhost:8080/sample/1/
"""
if (request.method == 'GET'
and not(request.path.endswith('/'))
and not(request.response_type)
and len(request.params)==0):
from tg.controllers import redirect
redirect(request.url+'/')
#{ Authorization decorators
class _BaseProtectionDecorator(object):
default_denial_handler = None
def __init__(self, predicate, denial_handler=None, smart_denial=False):
"""Verify that the predicate is met.
:param predicate: An object with a check_authorization(environ) method which
must raise a tg.predicates.NotAuthorizedError if not met.
:param denial_handler: The callable to be run if authorization is
denied (overrides :attr:`default_denial_handler` if defined).
If called, ``denial_handler`` will be passed a positional argument
which represents a message on why authorization was denied.
"""
self.predicate = predicate
self.denial_handler = denial_handler or self.default_denial_handler
self.smart_denial = smart_denial
class require(_BaseProtectionDecorator):
"""TurboGears-specific action protector.
The default authorization denial handler of this protector will flash
the message of the unmet predicate with ``warning`` or ``error`` as the
flash status if the HTTP status code is 401 or 403, respectively.
See :class:`allow_only` for controller-wide authorization.
"""
def __call__(self, action_):
return decorator(self.wrap_action, action_)
def wrap_action(self, action_, *args, **kwargs):
req = request._current_obj()
try:
self.predicate.check_authorization(req.environ)
except NotAuthorizedError, e:
reason = unicode(e)
if req.environ.get('repoze.who.identity'):
# The user is authenticated.
code = 403
else:
# The user is not authenticated.
code = 401
if self.denial_handler:
response.status = code
return self.denial_handler(reason)
abort(code, comment=reason)
return action_(*args, **kwargs)
def default_denial_handler(self, reason):
"""Authorization denial handler for protectors."""
status = 'warning' if response.status_int == 401 else 'error'
if not self.smart_denial:
flash(reason, status=status)
else:
if response.content_type not in ['application/json', 'text/xml']:
flash(reason, status=status)
abort(response.status_int, reason)
class allow_only(_BaseProtectionDecorator):
"""TurboGears controller wide protector.
The default authorization denial handler of this protector will flash
the message of the unmet predicate with ``warning`` or ``error`` as the
flash status if the HTTP status code is 401 or 403, respectively, since
by default the ``__before__`` method of the controller is decorated with
:class:`require`.
If the controller class has the ``_failed_authorization`` *class method*,
it will replace the default denial handler.
"""
protector = require
def __call__(self, cls, *args, **kwargs):
if hasattr(self.protector, 'predicate'):
cls.allow_only = self.protector.predicate
if hasattr(cls, '_failed_authorization'):
self.denial_handler = cls._failed_authorization
sup = super(allow_only, self)
if hasattr(sup, '__call__'):
return super(allow_only, self).__call__(cls, *args, **kwargs)
class cached_property(object):
"""Cached property executing the getter only once."""
def __init__(self, func):
self.__name__ = func.__name__
self.__module__ = func.__module__
self.__doc__ = func.__doc__
self.func = func
def __get__(self, obj, type=None):
if obj is None:
return self
try:
value = obj.__dict__[self.__name__]
except KeyError:
value = obj.__dict__[self.__name__] = self.func(obj)
return value
class with_engine(object):
"""
Decorator to force usage of a specific database engine
in TurboGears SQLAlchemy BalancedSession.
:param engine_name: 'master' or the name of one of the slaves, if is ``None``
it will not force any specific engine.
:param master_params: A dictionary or GET parameters that when present will force
usage of the master node. The keys of the dictionary will be the
name of the parameters to look for, while the values must be whenever
to pop the paramter from the parameters passed to the controller (True/False).
If `master_params` is a list then it is converted to a dictionary where
the keys are the entries of the list and the value is always True.
"""
def __init__(self, engine_name=None, master_params={}):
self.engine_name = engine_name
if not hasattr(master_params, 'keys'):
self.master_params = dict((p, True) for p in master_params)
else:
self.master_params = master_params
def before_validate(self, remainder, params):
force_request_engine(self.engine_name)
for p, pop in self.master_params.items():
if p in params:
if pop:
v = params.pop(p, None)
else:
v = params.get(p)
if v:
force_request_engine('master')
break
def __call__(self, func):
decoration = Decoration.get_decoration(func)
decoration.register_hook('before_validate', self.before_validate)
return func
#}