Add Admin preferences system
[cascardo/ipsilon.git] / src / util / data.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2013  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import sqlite3
22 import cherrypy
23
24 class Store(object):
25
26     def __init__(self, path=None):
27         if path is None:
28             self._path = os.getcwd()
29         else:
30             self._path = path
31
32     def _load_config(self, dbname):
33         con = None
34         rows = []
35         try:
36             con = sqlite3.connect(dbname)
37             cur = con.cursor()
38             cur.executescript("""
39                 CREATE TABLE IF NOT EXISTS config(name TEXT, value TEXT)
40                 """)
41             cur.execute("SELECT * FROM config")
42             rows = cur.fetchall()
43             con.commit()
44         except sqlite3.Error, e:
45             if con:
46                 con.rollback()
47             cherrypy.log.error("Failed to load config: [%s]" % e)
48         finally:
49             if con:
50                 con.close()
51
52         conf = {}
53         for row in rows:
54             conf[row[0]] = row[1]
55
56         return conf
57
58     def get_admin_config(self):
59         path = None
60         if 'admin.config.db' in cherrypy.config:
61             path = cherrypy.config['admin.config.db']
62         if not path:
63             path = os.path.join(self._path, 'adminconfig.sqlite')
64
65         return self._load_config(path)