Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlite / public.py @ 73673127

History | View | Annotate | Download (3.1 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 a9b3f29d Antony Chazapis
# 
3 a9b3f29d Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 a9b3f29d Antony Chazapis
# without modification, are permitted provided that the following
5 a9b3f29d Antony Chazapis
# conditions are met:
6 a9b3f29d Antony Chazapis
# 
7 a9b3f29d Antony Chazapis
#   1. Redistributions of source code must retain the above
8 a9b3f29d Antony Chazapis
#      copyright notice, this list of conditions and the following
9 a9b3f29d Antony Chazapis
#      disclaimer.
10 a9b3f29d Antony Chazapis
# 
11 a9b3f29d Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 a9b3f29d Antony Chazapis
#      copyright notice, this list of conditions and the following
13 a9b3f29d Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 a9b3f29d Antony Chazapis
#      provided with the distribution.
15 a9b3f29d Antony Chazapis
# 
16 a9b3f29d Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 a9b3f29d Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 a9b3f29d Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 a9b3f29d Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 a9b3f29d Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 a9b3f29d Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 a9b3f29d Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 a9b3f29d Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 a9b3f29d Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 a9b3f29d Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 a9b3f29d Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 a9b3f29d Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 a9b3f29d Antony Chazapis
# 
29 a9b3f29d Antony Chazapis
# The views and conclusions contained in the software and
30 a9b3f29d Antony Chazapis
# documentation are those of the authors and should not be
31 a9b3f29d Antony Chazapis
# interpreted as representing official policies, either expressed
32 a9b3f29d Antony Chazapis
# or implied, of GRNET S.A.
33 a9b3f29d Antony Chazapis
34 a9b3f29d Antony Chazapis
from dbworker import DBWorker
35 a9b3f29d Antony Chazapis
36 a9b3f29d Antony Chazapis
37 a9b3f29d Antony Chazapis
class Public(DBWorker):
38 a9b3f29d Antony Chazapis
    """Paths can be marked as public."""
39 a9b3f29d Antony Chazapis
    
40 a9b3f29d Antony Chazapis
    def __init__(self, **params):
41 a9b3f29d Antony Chazapis
        DBWorker.__init__(self, **params)
42 a9b3f29d Antony Chazapis
        execute = self.execute
43 a9b3f29d Antony Chazapis
        
44 a9b3f29d Antony Chazapis
        execute(""" create table if not exists public
45 bb4eafc6 Antony Chazapis
                          ( public_id integer primary key autoincrement,
46 af75e8a5 Antony Chazapis
                            path      text not null,
47 af75e8a5 Antony Chazapis
                            active    boolean not null default 1 ) """)
48 bb4eafc6 Antony Chazapis
        execute(""" create unique index if not exists idx_public_path
49 bb4eafc6 Antony Chazapis
                    on public(path) """)
50 a9b3f29d Antony Chazapis
    
51 a9b3f29d Antony Chazapis
    def public_set(self, path):
52 a9b3f29d Antony Chazapis
        q = "insert or ignore into public (path) values (?)"
53 a9b3f29d Antony Chazapis
        self.execute(q, (path,))
54 af75e8a5 Antony Chazapis
        q = "update public set active = 1 where path = ?"
55 af75e8a5 Antony Chazapis
        self.execute(q, (path,))
56 a9b3f29d Antony Chazapis
    
57 a9b3f29d Antony Chazapis
    def public_unset(self, path):
58 af75e8a5 Antony Chazapis
        q = "update public set active = 0 where path = ?"
59 a9b3f29d Antony Chazapis
        self.execute(q, (path,))
60 a9b3f29d Antony Chazapis
    
61 bb4eafc6 Antony Chazapis
    def public_get(self, path):
62 af75e8a5 Antony Chazapis
        q = "select public_id from public where path = ? and active = 1"
63 a9b3f29d Antony Chazapis
        self.execute(q, (path,))
64 bb4eafc6 Antony Chazapis
        row = self.fetchone()
65 bb4eafc6 Antony Chazapis
        if row:
66 bb4eafc6 Antony Chazapis
            return row[0]
67 bb4eafc6 Antony Chazapis
        return None
68 bb4eafc6 Antony Chazapis
    
69 15a96c3e Antony Chazapis
    def public_list(self, prefix):
70 15a96c3e Antony Chazapis
        q = "select path, public_id from public where path like ? escape '\\' and active = 1"
71 15a96c3e Antony Chazapis
        self.execute(q, (self.escape_like(prefix) + '%',))
72 15a96c3e Antony Chazapis
        return self.fetchall()
73 15a96c3e Antony Chazapis
    
74 bb4eafc6 Antony Chazapis
    def public_path(self, public):
75 af75e8a5 Antony Chazapis
        q = "select path from public where public_id = ? and active = 1"
76 bb4eafc6 Antony Chazapis
        self.execute(q, (public,))
77 bb4eafc6 Antony Chazapis
        row = self.fetchone()
78 bb4eafc6 Antony Chazapis
        if row:
79 bb4eafc6 Antony Chazapis
            return row[0]
80 bb4eafc6 Antony Chazapis
        return None