Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlite / config.py @ 4a7b190f

History | View | Annotate | Download (2.4 kB)

1 6540c590 Sofia Papagiannaki
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 6540c590 Sofia Papagiannaki
#
3 6540c590 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 6540c590 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 6540c590 Sofia Papagiannaki
# conditions are met:
6 6540c590 Sofia Papagiannaki
#
7 6540c590 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 6540c590 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 6540c590 Sofia Papagiannaki
#      disclaimer.
10 6540c590 Sofia Papagiannaki
#
11 6540c590 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 6540c590 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 6540c590 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 6540c590 Sofia Papagiannaki
#      provided with the distribution.
15 6540c590 Sofia Papagiannaki
#
16 6540c590 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 6540c590 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 6540c590 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 6540c590 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 6540c590 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 6540c590 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 6540c590 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 6540c590 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 6540c590 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 6540c590 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 6540c590 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 6540c590 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 6540c590 Sofia Papagiannaki
#
29 6540c590 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 6540c590 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 6540c590 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 6540c590 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 6540c590 Sofia Papagiannaki
34 6540c590 Sofia Papagiannaki
# from collections import defaultdict
35 29148653 Sofia Papagiannaki
#
36 6540c590 Sofia Papagiannaki
from dbworker import DBWorker
37 6540c590 Sofia Papagiannaki
38 6540c590 Sofia Papagiannaki
39 6540c590 Sofia Papagiannaki
class Config(DBWorker):
40 6540c590 Sofia Papagiannaki
    """Config are properties holding persistent information about system state.
41 6540c590 Sofia Papagiannaki
    """
42 6540c590 Sofia Papagiannaki
43 6540c590 Sofia Papagiannaki
    def __init__(self, **params):
44 6540c590 Sofia Papagiannaki
        DBWorker.__init__(self, **params)
45 6540c590 Sofia Papagiannaki
        execute = self.execute
46 6540c590 Sofia Papagiannaki
47 6540c590 Sofia Papagiannaki
        execute(""" create table if not exists config
48 6540c590 Sofia Papagiannaki
                          ( key text primary key,
49 6540c590 Sofia Papagiannaki
                            value text ) """)
50 6540c590 Sofia Papagiannaki
51 6540c590 Sofia Papagiannaki
    def get_value(self, key):
52 6540c590 Sofia Papagiannaki
        """Return configuration value for key."""
53 6540c590 Sofia Papagiannaki
54 6540c590 Sofia Papagiannaki
        q = "select value from config where key = ?"
55 6540c590 Sofia Papagiannaki
        self.execute(q, (key,))
56 6540c590 Sofia Papagiannaki
        r = self.fetchone()
57 6540c590 Sofia Papagiannaki
        if r is not None:
58 6540c590 Sofia Papagiannaki
            return r[0]
59 6540c590 Sofia Papagiannaki
        return None
60 29148653 Sofia Papagiannaki
61 6540c590 Sofia Papagiannaki
    def set_value(self, key, value):
62 6540c590 Sofia Papagiannaki
        """Set configuration entry.
63 6540c590 Sofia Papagiannaki
        """
64 6540c590 Sofia Papagiannaki
65 e647d184 Sofia Papagiannaki
        q = "insert into config (key, value) values (?, ?)"
66 6540c590 Sofia Papagiannaki
        id = self.execute(q, (key, value)).lastrowid
67 29148653 Sofia Papagiannaki
        return id