3 # Copyright (C) 2013 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
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.
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.
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/>.
27 def __init__(self, path=None):
29 self._path = os.getcwd()
32 self._admin_dbname = self._get_admin_dbname()
33 self._user_dbname = self._get_userprefs_dbname()
35 def _get_admin_dbname(self):
37 if 'admin.config.db' in cherrypy.config:
38 path = cherrypy.config['admin.config.db']
40 path = os.path.join(self._path, 'adminconfig.sqlite')
43 def _get_userprefs_dbname(self):
45 if 'user.prefs.db' in cherrypy.config:
46 path = cherrypy.config['user.prefs.db']
48 path = os.path.join(self._path, 'userprefs.sqlite')
51 def _load_config(self, dbname):
55 con = sqlite3.connect(dbname)
58 CREATE TABLE IF NOT EXISTS config(name TEXT, value TEXT)
60 cur.execute("SELECT * FROM config")
63 except sqlite3.Error, e:
66 cherrypy.log.error("Failed to load config: [%s]" % e)
75 if conf[row[0]] is list:
76 conf[row[0]].append(row[1])
79 conf[row[0]] = [v, row[1]]
85 def get_admin_config(self):
86 return self._load_config(self._admin_dbname)
88 def _load_user_prefs(self, dbname, user):
92 con = sqlite3.connect(dbname)
95 CREATE TABLE IF NOT EXISTS users(name TEXT,
99 cur.execute("SELECT option, value FROM users "
100 "where name = '%s'" % user)
101 rows = cur.fetchall()
103 except sqlite3.Error, e:
106 cherrypy.log.error("Failed to load %s's prefs from "
107 "%s: [%s]" % (user, dbname, e))
114 conf[row[0]] = row[1]
118 def get_user_preferences(self, user):
119 return self._load_user_prefs(self._user_dbname, user)
121 def get_plugins_config(self, facility):
125 con = sqlite3.connect(self._admin_dbname)
127 cur.execute("CREATE TABLE IF NOT EXISTS " +
128 facility + " (name TEXT,option TEXT,value TEXT)")
129 cur.execute("SELECT * FROM " + facility)
130 rows = cur.fetchall()
132 except sqlite3.Error, e:
135 cherrypy.log.error("Failed to load %s config: [%s]" % (facility,
144 if row[0] == 'global':
145 if row[1] == 'order':
146 lpo = row[2].split(',')
148 if row[0] not in plco:
149 # one dict per provider
150 plco[row[0]] = dict()
154 if conf[row[1]] is list:
155 conf[row[1]].append(row[2])
158 conf[row[1]] = [v, row[2]]
160 conf[row[1]] = row[2]
164 def save_plugin_config(self, facility, plugin, options):
165 SELECT = "SELECT option, value FROM %s WHERE name=?" % facility
166 UPDATE = "UPDATE %s SET value=? WHERE name=? AND option=?" % facility
167 INSERT = "INSERT INTO %s VALUES(?,?,?)" % facility
170 con = sqlite3.connect(self._admin_dbname)
173 for row in cur.execute(SELECT, (plugin,)):
174 curvals[row[0]] = row[1]
178 cur.execute(UPDATE, (options[name], plugin, name))
180 cur.execute(INSERT, (plugin, name, options[name]))
183 except sqlite3.Error, e:
186 cherrypy.log.error("Failed to store config: [%s]" % e)