2 # -*- coding: utf-8 -*-
5 This file is part of Multilist.
7 Multilist 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 Multilist 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 Multilist. If not, see <http://www.gnu.org/licenses/>.
20 Copyright (C) 2008 Christoph Würstle
38 _moduleLogger = logging.getLogger(__name__)
41 class Speichern(object):
44 home_dir = os.path.expanduser('~')
45 filename = os.path.join(home_dir, ".multilist.dat")
46 self.d = shelve.open(filename)
59 db = self.ladeDirekt("datenbank")
61 home_dir = os.path.expanduser('~')
62 db = os.path.join(home_dir, "multilist.s3db")
64 datum = time.strftime("%Y-%m-%d--", (time.localtime(time.time())))+str(int(time.time()))+"--"
65 if os.path.exists(db) and os.path.exists(os.path.dirname(db)+os.sep+"backup"):
67 shutil.copyfile(db, str(os.path.dirname(db))+os.sep+"backup"+os.sep+datum+os.path.basename(db))
69 _moduleLogger.info("Achtung Backup-Datei NICHT (!!!) angelegt!")
71 self.conn = sqlite3.connect(db)
72 self.cur = self.conn.cursor()
74 sql = "CREATE TABLE logtable (id INTEGER PRIMARY KEY AUTOINCREMENT, pcdatum INTEGER , sql TEXT, param TEXT, host TEXT, rowid TEXT)"
80 #Add rowid line (not in old versions included)
82 sql = "ALTER TABLE logtable ADD rowid TEXT"
101 _moduleLogger.info("Alle Daten gespeichert")
106 def speichereDirekt(self, schluessel, daten):
107 self.d[schluessel] = daten
108 _moduleLogger.info("speichereDirekt "+str(schluessel)+" "+str(daten)+" lesen: "+str(self.d[schluessel]))
110 def ladeDirekt(self, schluessel, default = ""):
111 if self.d.has_key(schluessel):
112 data = self.d[schluessel]
117 def speichereSQL(self, sql, tupel = None, commit = True, host = "self", log = True, pcdatum = None, rowid = ""):
119 programSQLError = True
121 self.cur.execute(sql)
123 self.cur.execute(sql, tupel)
124 programSQLError = False
128 if tupel is not None:
130 strtupel.append(str(t))
133 pcdatum = int(time.time())
134 self.cur.execute("INSERT INTO logtable ( pcdatum, sql, param, host, rowid ) VALUES (?, ?, ?, ?, ?)", (pcdatum, sql, string.join(strtupel, " <<Tren-ner>> "), host, str(rowid) ))
139 s = str(sys.exc_info())
140 if s.find(" already exists") == -1:
142 _moduleLogger.error("speichereSQL-Exception "+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
144 _moduleLogger.error("speichereSQL-Exception in Logging!!!! :"+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
150 def ladeSQL(self, sql, tupel = None):
153 self.cur.execute(sql)
155 self.cur.execute(sql, tupel)
156 return self.cur.fetchall()
158 _moduleLogger.error("ladeSQL-Exception "+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
161 def ladeHistory(self, sql_condition, param_condition):
162 sql = "SELECT * FROM logtable WHERE sql LIKE '%"+str(sql_condition)+"%' AND param LIKE '%"+str(param_condition)+"%'"
163 rows = self.ladeSQL(sql)
167 datum = time.strftime("%d.%m.%y %H:%M:%S", (time.localtime(row[1])))
168 erg.append([row[1], datum, row[2], row[3], row[3].split(" <<Tren-ner>> ")])
172 def delHistory(self, sql_condition, param_condition, exceptTheLastXSeconds = 0):
173 pcdatum = int(time.time())-exceptTheLastXSeconds
174 sql = "DELETE FROM logtable WHERE sql LIKE '%"+str(sql_condition)+"%' AND param LIKE '%"+str(param_condition)+"%' AND pcdatum<?"
175 self.speichereSQL(sql, (pcdatum, ))
177 def delHistoryWithRowID(self, rowid, sql_condition = " ", exceptTheLastXSeconds = 0):
178 pcdatum = int(time.time())-exceptTheLastXSeconds
179 sql = "DELETE FROM logtable WHERE rowid = ? AND pcdatum<? AND sql LIKE '%"+str(sql_condition)+"%'"
180 self.speichereSQL(sql, (rowid, pcdatum, ))