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 save_user_preferences(self, user, options):
122 SELECT = "SELECT option, value FROM users WHERE name=?"
123 UPDATE = "UPDATE users SET value=? WHERE name=? AND option=?"
124 INSERT = "INSERT INTO users VALUES(?,?,?)"
127 con = sqlite3.connect(self._user_dbname)
130 CREATE TABLE IF NOT EXISTS users(name TEXT,
135 for row in cur.execute(SELECT, (user,)):
136 curvals[row[0]] = row[1]
140 cur.execute(UPDATE, (options[name], user, name))
142 cur.execute(INSERT, (user, name, options[name]))
145 except sqlite3.Error, e:
148 cherrypy.log.error("Failed to store config: [%s]" % e)
154 def get_plugins_config(self, facility):
158 con = sqlite3.connect(self._admin_dbname)
160 cur.execute("CREATE TABLE IF NOT EXISTS " +
161 facility + " (name TEXT,option TEXT,value TEXT)")
162 cur.execute("SELECT * FROM " + facility)
163 rows = cur.fetchall()
165 except sqlite3.Error, e:
168 cherrypy.log.error("Failed to load %s config: [%s]" % (facility,
177 if row[0] == 'global':
178 if row[1] == 'order':
179 lpo = row[2].split(',')
181 if row[0] not in plco:
182 # one dict per provider
183 plco[row[0]] = dict()
187 if conf[row[1]] is list:
188 conf[row[1]].append(row[2])
191 conf[row[1]] = [v, row[2]]
193 conf[row[1]] = row[2]
197 def get_plugin_config(self, facility, plugin):
201 con = sqlite3.connect(self._admin_dbname)
203 cur.execute("CREATE TABLE IF NOT EXISTS " +
204 facility + " (name TEXT,option TEXT,value TEXT)")
205 cur.execute("SELECT option, value FROM " +
206 facility + " WHERE name=?", (plugin,))
207 rows = cur.fetchall()
209 except sqlite3.Error, e:
212 fpe = (facility, plugin, e)
213 cherrypy.log.error("Failed to get %s/%s config: [%s]" % fpe)
220 for (option, value) in rows:
222 if res[option] is list:
223 res[option].append(value)
226 res[option] = [v, value]
232 def save_plugin_config(self, facility, plugin, options):
233 SELECT = "SELECT option, value FROM %s WHERE name=?" % facility
234 UPDATE = "UPDATE %s SET value=? WHERE name=? AND option=?" % facility
235 INSERT = "INSERT INTO %s VALUES(?,?,?)" % facility
238 con = sqlite3.connect(self._admin_dbname)
240 cur.execute("CREATE TABLE IF NOT EXISTS " +
241 facility + " (name TEXT,option TEXT,value TEXT)")
243 for row in cur.execute(SELECT, (plugin,)):
244 curvals[row[0]] = row[1]
248 cur.execute(UPDATE, (options[name], plugin, name))
250 cur.execute(INSERT, (plugin, name, options[name]))
253 except sqlite3.Error, e:
256 cherrypy.log.error("Failed to store config: [%s]" % e)
262 def wipe_plugin_config(self, facility, plugin):
263 # Try to backup old data first, just in case ?
265 con = sqlite3.connect(self._admin_dbname)
267 cur.execute("CREATE TABLE IF NOT EXISTS " +
268 facility + " (name TEXT,option TEXT,value TEXT)")
269 cur.execute("DELETE FROM " + facility + " WHERE name=?",
272 except sqlite3.Error, e:
275 cherrypy.log.error("Failed to wipe %s config: [%s]" % (plugin, e))
281 def get_data(self, plugin, idval=None, name=None, value=None):
286 if idval or name or value:
290 values = values + (idval,)
295 values = values + (name,)
300 values = values + (value,)
302 con = sqlite3.connect(self._admin_dbname)
304 cur.execute("CREATE TABLE IF NOT EXISTS " +
305 plugin + "_data (id INTEGER, name TEXT, value TEXT)")
307 cur.execute("SELECT * FROM " + plugin + "_data")
309 cur.execute("SELECT * FROM " + plugin + "_data WHERE" +
311 rows = cur.fetchall()
313 except sqlite3.Error, e:
316 cherrypy.log.error("Failed to load %s data: [%s]" % (plugin, e))
317 cherrypy.log.error(repr([names, values]))
324 if row[0] not in data:
325 data[row[0]] = dict()
329 if item[row[1]] is list:
330 item[row[1]].append(row[2])
333 item[row[1]] = [v, row[2]]
335 item[row[1]] = row[2]
339 def save_data(self, plugin, data):
340 SELECT = "SELECT name, value FROM %s_data WHERE id=?" % plugin
341 UPDATE = "UPDATE %s_data SET value=? WHERE id=? AND name=?" % plugin
342 INSERT = "INSERT INTO %s_data VALUES(?,?,?)" % plugin
345 con = sqlite3.connect(self._admin_dbname)
349 for row in cur.execute(SELECT, (idval,)):
350 curvals[row[0]] = row[1]
355 cur.execute(UPDATE, (datum[name], idval, name))
357 cur.execute(INSERT, (idval, name, datum[name]))
360 except sqlite3.Error, e:
363 cherrypy.log.error("Failed to store %s data: [%s]" % (plugin, e))
369 def wipe_data(self, plugin):
370 # Try to backup old data first, just in case
372 con = sqlite3.connect(self._admin_dbname)
374 cur.execute("DROP TABLE IF EXISTS " + plugin + "_data")
375 cur.execute("CREATE TABLE " + plugin + "_data"
376 "(id INTEGER, name TEXT, value TEXT)")
378 except sqlite3.Error, e:
381 cherrypy.log.error("Failed to wipe %s data: [%s]" % (plugin, e))