def resetfromlog(self): """ drop the db and reinsert (and update) all the entries from log to the empty db """ if not os.path.exists(self.logfile): print("The logfile " + self.logfile + " doesn't exist!") return # backup the old one if os.path.exists(self.dbFilePath): bak = self.dbFilePath + "~" if os.path.exists(bak): if input("A backup already exists. Remove it (only Yes is accepted)? ") != "Yes": return os.remove(bak) os.rename(self.dbFilePath, bak) # read new one print("This might take a while depending on the log size. Please wait ...") self.dbConnection = DBConnection(self.dbFilePath) with self.dbConnection.getSessionContext() as session: with open(self.logfile, "r") as self.logf: for ll in self.logf: if ll.startswith(Register.LOGADD): dbf = self._dbFileFromLog(ll[len(Register.LOGADD):]) DBFileRegister.insert(session, dbf, commit=False) elif ll.startswith(Register.LOGUPDATED): dbf = self._dbFileFromLog(ll[len(Register.LOGUPDATED):]) session.commit() DBFileRegister.update(session,dbf, setall=True) # TODO display progress (line, items, date) session.commit() print("Done")
def setdata(self): """ Change some details of the entries given by IDs. IDs are required. Only filename, comment and group can be changed. """ ff = None if (isinstance(self.files, list)): if len(self.files) > 1: print("Please provide just one name or none at all!") return elif len(self.files) == 1: ff = self.files[0] if (self.fileId is None): print("Please provide file ID (-i option).") return self.fileId = int(self.fileId) dbf = DBFile(fileId=self.fileId, fileName=ff, group=self.group, comment=self.comment) self.log(Register.LOGUPDATE + self._formatDBFileForLog(dbf)) with self.dbConnection.getSessionContext() as session: dbf = DBFileRegister.update(session, dbf) if not dbf: print("Error updating the entry!") else: self.log(Register.LOGUPDATED + self._formatDBFileForLog(dbf))