Statistics
| Branch: | Tag: | Revision:

root / pithos / backends / lib / policy.py @ 0f9d752c

History | View | Annotate | Download (2.5 kB)

1 0f9d752c Antony Chazapis
# Copyright 2011 GRNET S.A. All rights reserved.
2 0f9d752c Antony Chazapis
# 
3 0f9d752c Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 0f9d752c Antony Chazapis
# without modification, are permitted provided that the following
5 0f9d752c Antony Chazapis
# conditions are met:
6 0f9d752c Antony Chazapis
# 
7 0f9d752c Antony Chazapis
#   1. Redistributions of source code must retain the above
8 0f9d752c Antony Chazapis
#      copyright notice, this list of conditions and the following
9 0f9d752c Antony Chazapis
#      disclaimer.
10 0f9d752c Antony Chazapis
# 
11 0f9d752c Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 0f9d752c Antony Chazapis
#      copyright notice, this list of conditions and the following
13 0f9d752c Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 0f9d752c Antony Chazapis
#      provided with the distribution.
15 0f9d752c Antony Chazapis
# 
16 0f9d752c Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 0f9d752c Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 0f9d752c Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 0f9d752c Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 0f9d752c Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 0f9d752c Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 0f9d752c Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 0f9d752c Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 0f9d752c Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 0f9d752c Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 0f9d752c Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 0f9d752c Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 0f9d752c Antony Chazapis
# 
29 0f9d752c Antony Chazapis
# The views and conclusions contained in the software and
30 0f9d752c Antony Chazapis
# documentation are those of the authors and should not be
31 0f9d752c Antony Chazapis
# interpreted as representing official policies, either expressed
32 0f9d752c Antony Chazapis
# or implied, of GRNET S.A.
33 0f9d752c Antony Chazapis
34 0f9d752c Antony Chazapis
from dbworker import DBWorker
35 0f9d752c Antony Chazapis
36 0f9d752c Antony Chazapis
37 0f9d752c Antony Chazapis
class Policy(DBWorker):
38 0f9d752c Antony Chazapis
    """Paths can be assigned key, value pairs, representing policy."""
39 0f9d752c Antony Chazapis
    
40 0f9d752c Antony Chazapis
    def __init__(self, **params):
41 0f9d752c Antony Chazapis
        DBWorker.__init__(self, **params)
42 0f9d752c Antony Chazapis
        execute = self.execute
43 0f9d752c Antony Chazapis
        
44 0f9d752c Antony Chazapis
        execute(""" create table if not exists policy
45 0f9d752c Antony Chazapis
                          ( path  text,
46 0f9d752c Antony Chazapis
                            key   text,
47 0f9d752c Antony Chazapis
                            value text,
48 0f9d752c Antony Chazapis
                            primary key (path, key) ) """)
49 0f9d752c Antony Chazapis
    
50 0f9d752c Antony Chazapis
    def policy_set(self, path, policy):
51 0f9d752c Antony Chazapis
        q = "insert or replace into policy (path, key, value) values (?, ?, ?)"
52 0f9d752c Antony Chazapis
        self.executemany(q, ((path, k, v) for k, v in policy.iteritems()))
53 0f9d752c Antony Chazapis
    
54 0f9d752c Antony Chazapis
    def policy_unset(self, path):
55 0f9d752c Antony Chazapis
        q = "delete from policy where path = ?"
56 0f9d752c Antony Chazapis
        self.execute(q, (path,))
57 0f9d752c Antony Chazapis
    
58 0f9d752c Antony Chazapis
    def policy_get(self, path):
59 0f9d752c Antony Chazapis
        q = "select key, value from policy where path = ?"
60 0f9d752c Antony Chazapis
        self.execute(q, (path,))
61 0f9d752c Antony Chazapis
        return dict(self.fetchall())