root / pithos / backends / lib / xfeatures.py @ a9b3f29d
History | View | Annotate | Download (6.1 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from dbworker import DBWorker |
35 |
|
36 |
|
37 |
class XFeatures(DBWorker): |
38 |
"""XFeatures are path properties that allow non-nested
|
39 |
inheritance patterns. Currently used for storing permissions.
|
40 |
"""
|
41 |
|
42 |
def __init__(self, **params): |
43 |
DBWorker.__init__(self, **params)
|
44 |
execute = self.execute
|
45 |
|
46 |
execute(""" pragma foreign_keys = on """)
|
47 |
|
48 |
execute(""" create table if not exists xfeatures
|
49 |
( feature_id integer primary key,
|
50 |
path text ) """)
|
51 |
execute(""" create unique index if not exists idx_features_path
|
52 |
on xfeatures(path) """)
|
53 |
|
54 |
execute(""" create table if not exists xfeaturevals
|
55 |
( feature_id integer,
|
56 |
key integer,
|
57 |
value text,
|
58 |
primary key (feature_id, key, value)
|
59 |
foreign key (feature_id) references xfeatures(feature_id)
|
60 |
on delete cascade ) """)
|
61 |
|
62 |
def xfeature_inherit(self, path): |
63 |
"""Return the (path, feature) inherited by the path, or None."""
|
64 |
|
65 |
q = ("select path, feature_id from xfeatures "
|
66 |
"where path <= ? "
|
67 |
"order by path desc limit 1")
|
68 |
self.execute(q, (path,))
|
69 |
r = self.fetchone()
|
70 |
if r is not None and path.startswith(r[0]): |
71 |
return r
|
72 |
return None |
73 |
|
74 |
def xfeature_list(self, path): |
75 |
"""Return the list of the (prefix, feature) pairs matching path.
|
76 |
A prefix matches path if either the prefix includes the path,
|
77 |
or the path includes the prefix.
|
78 |
"""
|
79 |
|
80 |
inherited = self.xfeature_inherit(path)
|
81 |
if inherited:
|
82 |
return [inherited]
|
83 |
|
84 |
q = ("select path, feature_id from xfeatures "
|
85 |
"where path like ? and path != ? order by path")
|
86 |
self.execute(q, (path + '%', path,)) |
87 |
return self.fetchall() |
88 |
|
89 |
def xfeature_create(self, path): |
90 |
"""Create and return a feature for path.
|
91 |
If the path already inherits a feature or
|
92 |
bestows to paths already inheriting a feature,
|
93 |
create no feature and return None.
|
94 |
"""
|
95 |
|
96 |
prefixes = self.xfeature_list(path)
|
97 |
pl = len(prefixes)
|
98 |
if (pl > 1) or (pl == 1 and prefixes[0][0] != path): |
99 |
return None |
100 |
q = "insert into xfeatures (path) values (?)"
|
101 |
id = self.execute(q, (path,)).lastrowid
|
102 |
return id |
103 |
|
104 |
def xfeature_destroy(self, path): |
105 |
"""Destroy a feature and all its key, value pairs."""
|
106 |
|
107 |
q = "delete from xfeatures where path = ?"
|
108 |
self.execute(q, (path,))
|
109 |
|
110 |
def feature_list(self, feature): |
111 |
"""Return the list of all key, value pairs
|
112 |
associated with a feature.
|
113 |
"""
|
114 |
|
115 |
q = "select key, value from xfeaturevals where feature = ?"
|
116 |
self.execute(q, (feature,))
|
117 |
return self.fetchall() |
118 |
|
119 |
def feature_set(self, feature, key, value): |
120 |
"""Associate a key, value pair with a feature."""
|
121 |
|
122 |
q = "insert or ignore into xfeaturevals (feature_id, key, value) values (?, ?, ?)"
|
123 |
self.execute(q, (feature, key, value))
|
124 |
|
125 |
def feature_setmany(self, feature, key, values): |
126 |
"""Associate the given key, and values with a feature."""
|
127 |
|
128 |
q = "insert or ignore into xfeaturevals (feature_id, key, value) values (?, ?, ?)"
|
129 |
self.executemany(q, ((feature, key, v) for v in values)) |
130 |
|
131 |
def feature_unset(self, feature, key, value): |
132 |
"""Disassociate a key, value pair from a feature."""
|
133 |
|
134 |
q = ("delete from xfeaturevals where "
|
135 |
"feature_id = ? and key = ? and value = ?")
|
136 |
self.execute(q, (feature, key, value))
|
137 |
|
138 |
def feature_unsetmany(self, feature, key, values): |
139 |
"""Disassociate the key for the values given, from a feature."""
|
140 |
|
141 |
q = ("delete from xfeaturevals where "
|
142 |
"feature_id = ? and key = ? and value = ?")
|
143 |
self.executemany(q, ((feature, key, v) for v in values)) |
144 |
|
145 |
def feature_get(self, feature, key): |
146 |
"""Return the list of values for a key of a feature."""
|
147 |
|
148 |
q = "select value from xfeaturevals where feature_id = ? and key = ?"
|
149 |
self.execute(q, (feature, key))
|
150 |
return [r[0] for r in self.fetchall()] |
151 |
|
152 |
def feature_clear(self, feature, key): |
153 |
"""Delete all key, value pairs for a key of a feature."""
|
154 |
|
155 |
q = "delete from xfeaturevals where feature_id = ? and key = ?"
|
156 |
self.execute(q, (feature, key))
|