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 get_plugin_config(self, facility, plugin):
168 con = sqlite3.connect(self._admin_dbname)
170 cur.execute("CREATE TABLE IF NOT EXISTS " +
171 facility + " (name TEXT,option TEXT,value TEXT)")
172 cur.execute("SELECT option, value FROM " +
173 facility + " WHERE name=?", (plugin,))
174 rows = cur.fetchall()
176 except sqlite3.Error, e:
179 fpe = (facility, plugin, e)
180 cherrypy.log.error("Failed to get %s/%s config: [%s]" % fpe)
187 for (option, value) in rows:
189 if res[option] is list:
190 res[option].append(value)
193 res[option] = [v, value]
199 def save_plugin_config(self, facility, plugin, options):
200 SELECT = "SELECT option, value FROM %s WHERE name=?" % facility
201 UPDATE = "UPDATE %s SET value=? WHERE name=? AND option=?" % facility
202 INSERT = "INSERT INTO %s VALUES(?,?,?)" % facility
205 con = sqlite3.connect(self._admin_dbname)
207 cur.execute("CREATE TABLE IF NOT EXISTS " +
208 facility + " (name TEXT,option TEXT,value TEXT)")
210 for row in cur.execute(SELECT, (plugin,)):
211 curvals[row[0]] = row[1]
215 cur.execute(UPDATE, (options[name], plugin, name))
217 cur.execute(INSERT, (plugin, name, options[name]))
220 except sqlite3.Error, e:
223 cherrypy.log.error("Failed to store config: [%s]" % e)
229 def wipe_plugin_config(self, facility, plugin):
230 # Try to backup old data first, just in case ?
232 con = sqlite3.connect(self._admin_dbname)
234 cur.execute("CREATE TABLE IF NOT EXISTS " +
235 facility + " (name TEXT,option TEXT,value TEXT)")
236 cur.execute("DELETE FROM " + facility + " WHERE name=?",
239 except sqlite3.Error, e:
242 cherrypy.log.error("Failed to wipe %s config: [%s]" % (plugin, e))
248 def get_data(self, plugin, idval=None, name=None, value=None):
253 if idval or name or value:
257 values = values + (idval,)
262 values = values + (name,)
267 values = values + (value,)
269 con = sqlite3.connect(self._admin_dbname)
271 cur.execute("CREATE TABLE IF NOT EXISTS " +
272 plugin + "_data (id INTEGER, name TEXT, value TEXT)")
274 cur.execute("SELECT * FROM " + plugin + "_data")
276 cur.execute("SELECT * FROM " + plugin + "_data WHERE" +
278 rows = cur.fetchall()
280 except sqlite3.Error, e:
283 cherrypy.log.error("Failed to load %s data: [%s]" % (plugin, e))
284 cherrypy.log.error(repr([names, values]))
291 if row[0] not in data:
292 data[row[0]] = dict()
296 if item[row[1]] is list:
297 item[row[1]].append(row[2])
300 item[row[1]] = [v, row[2]]
302 item[row[1]] = row[2]
306 def save_data(self, plugin, data):
307 SELECT = "SELECT name, value FROM %s_data WHERE id=?" % plugin
308 UPDATE = "UPDATE %s_data SET value=? WHERE id=? AND name=?" % plugin
309 INSERT = "INSERT INTO %s_data VALUES(?,?,?)" % plugin
312 con = sqlite3.connect(self._admin_dbname)
316 for row in cur.execute(SELECT, (idval,)):
317 curvals[row[0]] = row[1]
322 cur.execute(UPDATE, (datum[name], idval, name))
324 cur.execute(INSERT, (idval, name, datum[name]))
327 except sqlite3.Error, e:
330 cherrypy.log.error("Failed to store %s data: [%s]" % (plugin, e))
336 def wipe_data(self, plugin):
337 # Try to backup old data first, just in case
339 con = sqlite3.connect(self._admin_dbname)
341 cur.execute("DROP TABLE IF EXISTS " + plugin + "_data")
342 cur.execute("CREATE TABLE " + plugin + "_data"
343 "(id INTEGER, name TEXT, value TEXT)")
345 except sqlite3.Error, e:
348 cherrypy.log.error("Failed to wipe %s data: [%s]" % (plugin, e))