Use pylint check
[cascardo/ipsilon.git] / ipsilon / 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             if row[0] in conf:
55                 # multivalued
56                 if conf[row[0]] is list:
57                     conf[row[0]].append(row[1])
58                 else:
59                     v = conf[row[0]]
60                     conf[row[0]] = [v, row[1]]
61             else:
62                 conf[row[0]] = row[1]
63
64         return conf
65
66     def get_admin_config(self):
67         path = None
68         if 'admin.config.db' in cherrypy.config:
69             path = cherrypy.config['admin.config.db']
70         if not path:
71             path = os.path.join(self._path, 'adminconfig.sqlite')
72
73         return self._load_config(path)
74
75     def _load_user_prefs(self, dbname, user):
76         con = None
77         rows = []
78         try:
79             con = sqlite3.connect(dbname)
80             cur = con.cursor()
81             cur.executescript("""
82                 CREATE TABLE IF NOT EXISTS users(name TEXT,
83                                                  option TEXT,
84                                                  value TEXT)
85                 """)
86             cur.execute("SELECT option, value FROM users "
87                         "where name = '%s'" % user)
88             rows = cur.fetchall()
89             con.commit()
90         except sqlite3.Error, e:
91             if con:
92                 con.rollback()
93             cherrypy.log.error("Failed to load %s's prefs from "
94                                "%s: [%s]" % ( user, dbname, e))
95         finally:
96             if con:
97                 con.close()
98
99         conf = {}
100         for row in rows:
101             conf[row[0]] = row[1]
102
103         return conf
104
105     def get_user_preferences(self, user):
106         path = None
107         if 'user.prefs.db' in cherrypy.config:
108             path = cherrypy.config['user.prefs.db']
109         if not path:
110             path = os.path.join(self._path, 'userprefs.sqlite')
111
112         return self._load_user_prefs(path, user)