summaryrefslogtreecommitdiff
path: root/ebus/webapp/soup_plugin.py
blob: 68120af3e9a58f447318bf951e9abbb330ce1044 (plain)
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
# -*- coding: utf-8 -*-
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=python

import inspect

from sqlalchemy import create_engine, text
from sqlalchemy.ext.sqlsoup import SqlSoup


class SoupPlugin(object):
    name = 'soup'
    def __init__(self, url, keyword='soup'):
        self.url = url
        self.keyword = keyword

    def setup(self, app):
        for other in app.plugins:
            if not isinstance(other, SoupPlugin): continue
            if other.keyword == self.keyword:
                raise PluginError("Found another soup plugin with "\
                                  "conflicting settings (non-unique keyword).")

        self.engine = create_engine(self.url)
        self.soup = SqlSoup(self.engine)


    def apply(self, callback, context):
        print "apply"
        conf = context['config'].get('soup') or {}
        keyword = conf.get('keyword', self.keyword) 
        
        args = inspect.getargspec(context['callback'])[0]
        if keyword not in args:
            return callback
        
        def wrapper(*args, **kwargs):
            kwargs[keyword] = self.soup
            return callback(*args, **kwargs)
        
        return wrapper