File: bottle_beaker.py

package info (click to toggle)
python-bottle-beaker 0.1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 112 kB
  • sloc: python: 52; makefile: 3
file content (40 lines) | stat: -rw-r--r-- 1,251 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import bottle
import inspect
import beaker
from beaker import middleware


class BeakerPlugin(object):
    name = 'beaker'

    def __init__(self, keyword='beaker'):
        """
        :param keyword: Keyword used to inject beaker in a route
        """
        self.keyword = keyword

    def setup(self, app):
        """ Make sure that other installed plugins don't affect the same
            keyword argument and check if metadata is available."""
        for other in app.plugins:
            if not isinstance(other, BeakerPlugin):
                continue
            if other.keyword == self.keyword:
                raise bottle.PluginError("Found another beaker plugin "
                                         "with conflicting settings ("
                                         "non-unique keyword).")

    def apply(self, callback, context):
        args = inspect.getargspec(context['callback'])[0]

        if self.keyword not in args:
            return callback

        def wrapper(*args, **kwargs):
            kwargs[self.keyword] = beaker
            kwargs["{0}_middleware".format(self.keyword)] = middleware
            return callback(*args, **kwargs)

        return wrapper