Use pep8 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
25 class Store(object):
26
27     def __init__(self, path=None):
28         if path is None:
29             self._path = os.getcwd()
30         else:
31             self._path = path
32
33     def _load_config(self, dbname):
34         con = None
35         rows = []
36         try:
37             con = sqlite3.connect(dbname)
38             cur = con.cursor()
39             cur.executescript("""
40                 CREATE TABLE IF NOT EXISTS config(name TEXT, value TEXT)
41                 """)
42             cur.execute("SELECT * FROM config")
43             rows = cur.fetchall()
44             con.commit()
45         except sqlite3.Error, e:
46             if con:
47                 con.rollback()
48             cherrypy.log.error("Failed to load config: [%s]" % e)
49         finally:
50             if con:
51                 con.close()
52
53         conf = {}
54         for row in rows:
55             if row[0] in conf:
56                 # multivalued
57                 if conf[row[0]] is list:
58                     conf[row[0]].append(row[1])
59                 else:
60                     v = conf[row[0]]
61                     conf[row[0]] = [v, row[1]]
62             else:
63                 conf[row[0]] = row[1]
64
65         return conf
66
67     def get_admin_config(self):
68         path = None
69         if 'admin.config.db' in cherrypy.config:
70             path = cherrypy.config['admin.config.db']
71         if not path:
72             path = os.path.join(self._path, 'adminconfig.sqlite')
73
74         return self._load_config(path)
75
76     def _load_user_prefs(self, dbname, user):
77         con = None
78         rows = []
79         try:
80             con = sqlite3.connect(dbname)
81             cur = con.cursor()
82             cur.executescript("""
83                 CREATE TABLE IF NOT EXISTS users(name TEXT,
84                                                  option TEXT,
85                                                  value TEXT)
86                 """)
87             cur.execute("SELECT option, value FROM users "
88                         "where name = '%s'" % user)
89             rows = cur.fetchall()
90             con.commit()
91         except sqlite3.Error, e:
92             if con:
93                 con.rollback()
94             cherrypy.log.error("Failed to load %s's prefs from "
95                                "%s: [%s]" % (user, dbname, e))
96         finally:
97             if con:
98                 con.close()
99
100         conf = {}
101         for row in rows:
102             conf[row[0]] = row[1]
103
104         return conf
105
106     def get_user_preferences(self, user):
107         path = None
108         if 'user.prefs.db' in cherrypy.config:
109             path = cherrypy.config['user.prefs.db']
110         if not path:
111             path = os.path.join(self._path, 'userprefs.sqlite')
112
113         return self._load_user_prefs(path, user)