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)
192 def wipe_plugin_config(self, facility, plugin):
193 # Try to backup old data first, just in case ?
195 con = sqlite3.connect(self._admin_dbname)
197 cur.execute("CREATE TABLE IF NOT EXISTS " +
198 facility + " (name TEXT,option TEXT,value TEXT)")
199 cur.execute("DELETE FROM " + facility + " WHERE name=?",
202 except sqlite3.Error, e:
205 cherrypy.log.error("Failed to wipe %s config: [%s]" % (plugin, e))
211 def get_data(self, plugin, idval=None, name=None, value=None):
216 if idval or name or value:
220 values = values + (idval,)
225 values = values + (name,)
230 values = values + (value,)
232 con = sqlite3.connect(self._admin_dbname)
234 cur.execute("CREATE TABLE IF NOT EXISTS " +
235 plugin + "_data (id INTEGER, name TEXT, value TEXT)")
237 cur.execute("SELECT * FROM " + plugin + "_data")
239 cur.execute("SELECT * FROM " + plugin + "_data WHERE" +
241 rows = cur.fetchall()
243 except sqlite3.Error, e:
246 cherrypy.log.error("Failed to load %s data: [%s]" % (plugin, e))
247 cherrypy.log.error(repr([names, values]))
254 if row[0] not in data:
255 data[row[0]] = dict()
259 if item[row[1]] is list:
260 item[row[1]].append(row[2])
263 item[row[1]] = [v, row[2]]
265 item[row[1]] = row[2]
269 def save_data(self, plugin, data):
270 SELECT = "SELECT name, value FROM %s_data WHERE id=?" % plugin
271 UPDATE = "UPDATE %s_data SET value=? WHERE id=? AND name=?" % plugin
272 INSERT = "INSERT INTO %s_data VALUES(?,?,?)" % plugin
275 con = sqlite3.connect(self._admin_dbname)
279 for row in cur.execute(SELECT, (idval,)):
280 curvals[row[0]] = row[1]
285 cur.execute(UPDATE, (datum[name], idval, name))
287 cur.execute(INSERT, (idval, name, datum[name]))
290 except sqlite3.Error, e:
293 cherrypy.log.error("Failed to store %s data: [%s]" % (plugin, e))
299 def wipe_data(self, plugin):
300 # Try to backup old data first, just in case
302 con = sqlite3.connect(self._admin_dbname)
304 cur.execute("DROP TABLE IF EXISTS " + plugin + "_data")
305 cur.execute("CREATE TABLE " + plugin + "_data"
306 "(id INTEGER, name TEXT, value TEXT)")
308 except sqlite3.Error, e:
311 cherrypy.log.error("Failed to wipe %s data: [%s]" % (plugin, e))