Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / config.py @ 3759eddb

History | View | Annotate | Download (3 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 sqlalchemy import Table, Column, String, MetaData
35 6540c590 Sofia Papagiannaki
from sqlalchemy.sql import select
36 6540c590 Sofia Papagiannaki
from sqlalchemy.exc import NoSuchTableError
37 6540c590 Sofia Papagiannaki
38 6540c590 Sofia Papagiannaki
from dbworker import DBWorker
39 6540c590 Sofia Papagiannaki
40 29148653 Sofia Papagiannaki
41 6540c590 Sofia Papagiannaki
def create_tables(engine):
42 6540c590 Sofia Papagiannaki
    metadata = MetaData()
43 6540c590 Sofia Papagiannaki
    columns = []
44 6540c590 Sofia Papagiannaki
    columns.append(Column('key', String(256), primary_key=True))
45 6540c590 Sofia Papagiannaki
    columns.append(Column('value', String(256)))
46 29148653 Sofia Papagiannaki
    Table('config', metadata, *columns, mysql_engine='InnoDB')
47 29148653 Sofia Papagiannaki
48 6540c590 Sofia Papagiannaki
    metadata.create_all(engine)
49 6540c590 Sofia Papagiannaki
    return metadata.sorted_tables
50 6540c590 Sofia Papagiannaki
51 29148653 Sofia Papagiannaki
52 6540c590 Sofia Papagiannaki
class Config(DBWorker):
53 6540c590 Sofia Papagiannaki
    """Config are properties holding persistent information about system state.
54 6540c590 Sofia Papagiannaki
    """
55 6540c590 Sofia Papagiannaki
56 6540c590 Sofia Papagiannaki
    def __init__(self, **params):
57 6540c590 Sofia Papagiannaki
        DBWorker.__init__(self, **params)
58 6540c590 Sofia Papagiannaki
        try:
59 6540c590 Sofia Papagiannaki
            metadata = MetaData(self.engine)
60 6540c590 Sofia Papagiannaki
            self.config = Table('config', metadata, autoload=True)
61 6540c590 Sofia Papagiannaki
        except NoSuchTableError:
62 6540c590 Sofia Papagiannaki
            tables = create_tables(self.engine)
63 6540c590 Sofia Papagiannaki
            map(lambda t: self.__setattr__(t.name, t), tables)
64 6540c590 Sofia Papagiannaki
65 6540c590 Sofia Papagiannaki
    def get_value(self, key):
66 6540c590 Sofia Papagiannaki
        """Return configuration value for key."""
67 6540c590 Sofia Papagiannaki
68 6540c590 Sofia Papagiannaki
        s = select([self.config.c.value])
69 6540c590 Sofia Papagiannaki
        s = s.where(self.config.c.key == key)
70 6540c590 Sofia Papagiannaki
        r = self.conn.execute(s)
71 6540c590 Sofia Papagiannaki
        row = r.fetchone()
72 6540c590 Sofia Papagiannaki
        r.close()
73 6540c590 Sofia Papagiannaki
        if row:
74 6540c590 Sofia Papagiannaki
            return row[0]
75 6540c590 Sofia Papagiannaki
        return None
76 29148653 Sofia Papagiannaki
77 6540c590 Sofia Papagiannaki
    def set_value(self, key, value):
78 6540c590 Sofia Papagiannaki
        """Set a configuration entry.
79 6540c590 Sofia Papagiannaki
        """
80 6540c590 Sofia Papagiannaki
81 6540c590 Sofia Papagiannaki
        s = self.config.insert()
82 6540c590 Sofia Papagiannaki
        r = self.conn.execute(s, key=key, value=value)
83 6540c590 Sofia Papagiannaki
        inserted_primary_key = r.inserted_primary_key[0]
84 6540c590 Sofia Papagiannaki
        r.close()
85 29148653 Sofia Papagiannaki
        return inserted_primary_key