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/>.
23 from random import randint
29 def __init__(self, path=None):
31 self._path = os.getcwd()
34 self._admin_dbname = self._get_admin_dbname()
35 self._user_dbname = self._get_userprefs_dbname()
37 def _get_admin_dbname(self):
39 if 'admin.config.db' in cherrypy.config:
40 path = cherrypy.config['admin.config.db']
42 path = os.path.join(self._path, 'adminconfig.sqlite')
45 def _get_userprefs_dbname(self):
47 if 'user.prefs.db' in cherrypy.config:
48 path = cherrypy.config['user.prefs.db']
50 path = os.path.join(self._path, 'userprefs.sqlite')
53 def _load_config(self, dbname):
57 con = sqlite3.connect(dbname)
60 CREATE TABLE IF NOT EXISTS config(name TEXT, value TEXT)
62 cur.execute("SELECT * FROM config")
65 except sqlite3.Error, e:
68 cherrypy.log.error("Failed to load config: [%s]" % e)
77 if conf[row[0]] is list:
78 conf[row[0]].append(row[1])
81 conf[row[0]] = [v, row[1]]
87 def get_admin_config(self):
88 return self._load_config(self._admin_dbname)
90 def _load_user_prefs(self, dbname, user):
94 con = sqlite3.connect(dbname)
97 CREATE TABLE IF NOT EXISTS users(name TEXT,
101 cur.execute("SELECT option, value FROM users "
102 "where name = '%s'" % user)
103 rows = cur.fetchall()
105 except sqlite3.Error, e:
108 cherrypy.log.error("Failed to load %s's prefs from "
109 "%s: [%s]" % (user, dbname, e))
116 conf[row[0]] = row[1]
120 def get_user_preferences(self, user):
121 return self._load_user_prefs(self._user_dbname, user)
123 def save_user_preferences(self, user, options):
124 SELECT = "SELECT option, value FROM users WHERE name=?"
125 UPDATE = "UPDATE users SET value=? WHERE name=? AND option=?"
126 INSERT = "INSERT INTO users VALUES(?,?,?)"
129 con = sqlite3.connect(self._user_dbname)
132 CREATE TABLE IF NOT EXISTS users(name TEXT,
137 for row in cur.execute(SELECT, (user,)):
138 curvals[row[0]] = row[1]
142 cur.execute(UPDATE, (options[name], user, name))
144 cur.execute(INSERT, (user, name, options[name]))
147 except sqlite3.Error, e:
150 cherrypy.log.error("Failed to store config: [%s]" % e)
156 def get_plugins_config(self, facility):
160 con = sqlite3.connect(self._admin_dbname)
162 cur.execute("CREATE TABLE IF NOT EXISTS " +
163 facility + " (name TEXT,option TEXT,value TEXT)")
164 cur.execute("SELECT * FROM " + facility)
165 rows = cur.fetchall()
167 except sqlite3.Error, e:
170 cherrypy.log.error("Failed to load %s config: [%s]" % (facility,
179 if row[0] == 'global':
180 if row[1] == 'order':
181 lpo = row[2].split(',')
183 if row[0] not in plco:
184 # one dict per provider
185 plco[row[0]] = dict()
189 if conf[row[1]] is list:
190 conf[row[1]].append(row[2])
193 conf[row[1]] = [v, row[2]]
195 conf[row[1]] = row[2]
199 def get_plugin_config(self, facility, plugin):
203 con = sqlite3.connect(self._admin_dbname)
205 cur.execute("CREATE TABLE IF NOT EXISTS " +
206 facility + " (name TEXT,option TEXT,value TEXT)")
207 cur.execute("SELECT option, value FROM " +
208 facility + " WHERE name=?", (plugin,))
209 rows = cur.fetchall()
211 except sqlite3.Error, e:
214 fpe = (facility, plugin, e)
215 cherrypy.log.error("Failed to get %s/%s config: [%s]" % fpe)
222 for (option, value) in rows:
224 if res[option] is list:
225 res[option].append(value)
228 res[option] = [v, value]
234 def save_plugin_config(self, facility, plugin, options):
235 SELECT = "SELECT option, value FROM %s WHERE name=?" % facility
236 UPDATE = "UPDATE %s SET value=? WHERE name=? AND option=?" % facility
237 INSERT = "INSERT INTO %s VALUES(?,?,?)" % facility
240 con = sqlite3.connect(self._admin_dbname)
242 cur.execute("CREATE TABLE IF NOT EXISTS " +
243 facility + " (name TEXT,option TEXT,value TEXT)")
245 for row in cur.execute(SELECT, (plugin,)):
246 curvals[row[0]] = row[1]
250 cur.execute(UPDATE, (options[name], plugin, name))
252 cur.execute(INSERT, (plugin, name, options[name]))
255 except sqlite3.Error, e:
258 cherrypy.log.error("Failed to store config: [%s]" % e)
264 def wipe_plugin_config(self, facility, plugin):
265 # Try to backup old data first, just in case ?
267 con = sqlite3.connect(self._admin_dbname)
269 cur.execute("CREATE TABLE IF NOT EXISTS " +
270 facility + " (name TEXT,option TEXT,value TEXT)")
271 cur.execute("DELETE FROM " + facility + " WHERE name=?",
274 except sqlite3.Error, e:
277 cherrypy.log.error("Failed to wipe %s config: [%s]" % (plugin, e))
283 def get_data(self, plugin, idval=None, name=None, value=None):
288 if idval or name or value:
292 values = values + (idval,)
297 values = values + (name,)
302 values = values + (value,)
304 con = sqlite3.connect(self._admin_dbname)
306 cur.execute("CREATE TABLE IF NOT EXISTS " +
307 plugin + "_data (id INTEGER, name TEXT, value TEXT)")
309 cur.execute("SELECT * FROM " + plugin + "_data")
311 cur.execute("SELECT * FROM " + plugin + "_data WHERE" +
313 rows = cur.fetchall()
315 except sqlite3.Error, e:
318 cherrypy.log.error("Failed to load %s data: [%s]" % (plugin, e))
319 cherrypy.log.error(repr([names, values]))
326 if row[0] not in data:
327 data[row[0]] = dict()
331 if item[row[1]] is list:
332 item[row[1]].append(row[2])
335 item[row[1]] = [v, row[2]]
337 item[row[1]] = row[2]
341 def save_data(self, plugin, data):
342 SELECT = "SELECT name, value FROM %s_data WHERE id=?" % plugin
343 UPDATE = "UPDATE %s_data SET value=? WHERE id=? AND name=?" % plugin
344 INSERT = "INSERT INTO %s_data VALUES(?,?,?)" % plugin
347 con = sqlite3.connect(self._admin_dbname)
351 for row in cur.execute(SELECT, (idval,)):
352 curvals[row[0]] = row[1]
357 cur.execute(UPDATE, (datum[name], idval, name))
359 cur.execute(INSERT, (idval, name, datum[name]))
362 except sqlite3.Error, e:
365 cherrypy.log.error("Failed to store %s data: [%s]" % (plugin, e))
371 def new_datum(self, plugin, datum):
372 ID = "(SELECT IFNULL(MAX(id), 0) + 1 FROM %s_data)" % plugin
373 INSERT_NEW = "INSERT INTO %s_data VALUES(%s,?,?)" % (plugin, ID)
374 INSERT = "INSERT INTO %s_data VALUES(?,?,?)" % plugin
375 SELECT = "SELECT id FROM %s_data WHERE name=? AND value=?" % plugin
376 DELETE = "DELETE FROM %s_data WHERE name=? AND value=?" % plugin
379 con = sqlite3.connect(self._admin_dbname)
381 tmpid = ('new', str(randint(0, sys.maxint)))
382 cur.execute(INSERT_NEW, tmpid)
383 cur.execute(SELECT, tmpid)
384 rows = cur.fetchall()
387 cur.execute(INSERT, (idval, name, datum[name]))
388 cur.execute(DELETE, tmpid)
390 except sqlite3.Error, e:
393 cherrypy.log.error("Failed to store %s data: [%s]" % (plugin, e))
399 def wipe_data(self, plugin):
400 # Try to backup old data first, just in case
402 con = sqlite3.connect(self._admin_dbname)
404 cur.execute("DROP TABLE IF EXISTS " + plugin + "_data")
405 cur.execute("CREATE TABLE " + plugin + "_data"
406 "(id INTEGER, name TEXT, value TEXT)")
408 except sqlite3.Error, e:
411 cherrypy.log.error("Failed to wipe %s data: [%s]" % (plugin, e))