def dump(self, dbpath, user, isList=False): if isList and user: backup = DumpUsers(user, self.master, dbpath) guimessage = _("Creating multiple user database files ...\n") else: backup = Dump(self.master, dbpath, user) if not backup.filepath: return None guimessage = argSub(_("Database file '%1' created, now" " read in the data"), (backup.filepath,)) if user: title = _("Create User Database File") else: title = _("Create Full Database Dump File") guiReport(title, backup, guimessage) return backup.filepath
def dump(self, dbpath, user, isList=False): if isList and user: backup = DumpUsers(user, self.master, dbpath) guimessage = _("Creating multiple user database files ...\n") else: backup = Dump(self.master, dbpath, user) if not backup.filepath: return None guimessage = argSub( _("Database file '%1' created, now" " read in the data"), (backup.filepath, )) if user: title = _("Create User Database File") else: title = _("Create Full Database Dump File") guiReport(title, backup, guimessage) return backup.filepath
def slot_dump(self, arg): """Generate a backup file (sqlite database). """ # Get destination directory: dir0 = self.settings.getSetting("destDir") dbpath = getDirectory(_("Destination Folder"), dir0) if not dbpath: return self.settings.setSetting("destDir", dbpath) # Do the dump backup = Dump(self.master, dbpath) if not backup.filepath: return guimessage = argSub(_("New backup file '%1' opened, now write in the data"), (backup.filepath,)) guiReport(_("Create Backup File"), backup, guimessage)
def slot_dump(self, arg): """Generate a backup file (sqlite database). """ # Get destination directory: dir0 = self.settings.getSetting("destDir") dbpath = getDirectory(_("Destination Folder"), dir0) if not dbpath: return self.settings.setSetting("destDir", dbpath) # Do the dump backup = Dump(self.master, dbpath) if not backup.filepath: return guimessage = argSub( _("New backup file '%1' opened, now write in the data"), (backup.filepath, )) guiReport(_("Create Backup File"), backup, guimessage)
def createNewDb(self, source): """Create a new database using the configuration file supplied as a CfgZip object in source. """ dbname = source.cfgName state = 0 try: self.db.send(u"""CREATE DATABASE %s OWNER %s ENCODING 'UTF8'""" % (dbname, ADMIN)) state = 1 # Add to 'databases' table self.db.send(u"INSERT INTO databases VALUES (?, ?, ?, ?)", (self.db.getTime(), dbname, u'', u'')) state = 2 newmaster = self.connect(dbname) state = 3 guimessage = argSub( _("New database '%1' created, now read in the data"), (dbname, )) mm = MakeMaster(source, newmaster) guiReport(_("Create New Database"), mm, guimessage) #message(_("New database now set up")) self.usersPrivileges(newmaster) # Ensure connection is closed mm = None newmaster.close() newmaster = None except: # print_exc() message(_("Couldn't create new database (%1)"), (dbname, )) if (state >= 3): newmaster.close() if (state >= 2): self.db.send(u"DELETE FROM databases WHERE name = ?", (dbname, )) if (state >= 1): self.db.send(u"DROP DATABASE %s" % dbname) # adjust display, select new db self.initDBlist()
def createNewDb(self, source): """Create a new database using the configuration file supplied as a CfgZip object in source. """ dbname = source.cfgName state = 0 try: self.db.send(u"""CREATE DATABASE %s OWNER %s ENCODING 'UTF8'""" % (dbname, ADMIN)) state = 1 # Add to 'databases' table self.db.send(u"INSERT INTO databases VALUES (?, ?, ?, ?)", (self.db.getTime(), dbname, u'', u'')) state = 2 newmaster = self.connect(dbname) state = 3 guimessage = argSub(_("New database '%1' created, now read in the data"), (dbname,)) mm = MakeMaster(source, newmaster) guiReport(_("Create New Database"), mm, guimessage) #message(_("New database now set up")) self.usersPrivileges(newmaster) # Ensure connection is closed mm = None newmaster.close() newmaster = None except: # print_exc() message(_("Couldn't create new database (%1)"), (dbname,)) if (state >= 3): newmaster.close() if (state >= 2): self.db.send(u"DELETE FROM databases WHERE name = ?", (dbname,)) if (state >= 1): self.db.send(u"DROP DATABASE %s" % dbname) # adjust display, select new db self.initDBlist()
def slot_getPupils(self, arg): """Import pupil data from csv files, one file per class. """ columns = [f[0] for f in getSpec("classes/*/pupils/*")[3:]] csv = CsvData(self.settings.getSetting("csvData")) if not getCsvConfig(["id"] + columns, csv): return self.settings.setSetting("csvData", csv.setting) csvHandler = Csv2Sini(self, csv.separator, csv.columns) lastDir = self.settings.getSetting("csvDir") csvdir = csvHandler.init(lastDir) if not csvdir: return self.settings.setSetting("csvDir", csvdir) guiReport(_("Importing pupil data"), csvHandler, '') self.checkAll() self.gui.setCurrentItem(self.nodeDict["classes/"])
def slot_restore(self, arg): """Restore a dumped database. It can be either an existing one, or one which has been deleted. """ # Get source file: dbpath = self.getBDbPath() if not dbpath: return None restore = Restore(dbpath) dbname = restore.getDbName() if not dbname: message(_("Couldn't open database file '%1'"), (dbpath, )) return state = 0 try: if dbname in self.dbList: if not confirmationDialog( _("Replace Database?"), argSub( _("Are you sure you want to replace database '%1'?" ), (dbname, )), False): restore.close() return self.deletedb(dbname) self.db.send(u"""CREATE DATABASE %s OWNER %s ENCODING 'UTF8'""" % (dbname, ADMIN)) state = 1 # Add to 'databases' table self.db.send(u"INSERT INTO databases VALUES (?, ?, ?, ?)", (self.db.getTime(), dbname, u'', u'')) state = 2 newmaster = self.connect(dbname) state = 3 guimessage = argSub( _("New database '%1' created, now read in the data"), (dbname, )) restore.setMaster(newmaster) guiReport(_("Restore Database"), restore, guimessage) #message(_("New database now set up")) self.usersPrivileges(newmaster) # Ensure connection is closed restore = None newmaster.close() newmaster = None except: print_exc() message(_("Couldn't create new database (%1)"), (dbname, )) if (state >= 3): newmaster.close() if (state >= 2): self.db.send(u"DELETE FROM databases WHERE name = ?", (dbname, )) if (state >= 1): self.db.send(u"DROP DATABASE %s" % dbname) # adjust display, select new db self.initDBlist()
def updateDbConfig(self, source): """Update the current database using the configuration file supplied as a CfgZip object in source. The selected config file must match the name of the current database. Before updating from this file, dump the current database state to a folder 'dumps' in the same folder as the config file. That is in case something goes wrong and the old state must be recovered. """ if (self.dbname != source.cfgName): message(_("Database name does not match data folder")) return # Backup existing database state. sPath = self.configEd.getSourcePath() budir = os.path.join(os.path.dirname(sPath), 'dumps') if not os.path.isdir(budir): os.mkdir(budir) backup = Dump(self.master, budir) filepath = backup.filepath if not filepath: return guimessage = argSub( _("New backup file '%1' created, now read in the data"), (filepath, )) guiReport(_("Create Backup File"), backup, guimessage) backup = None if not filepath: return try: guimessage = argSub(_("Updating database '%1' from %2"), (self.dbname, sPath)) mm = MakeMaster(source, self.master) guiReport(_("Updating Master Database"), mm, guimessage) mm = None except: print_exc() message(_("Update failed, trying to restore from '%1'"), (filepath, )) restore = Restore(filepath) dbname = restore.getDbName() if not dbname: message(_("Couldn't open database file '%1'"), (filepath, )) return # Delete all tables for t in self.master.getTables(): self.master.send(u"DROP TABLE %s" % t) # Restore old state guimessage = argSub( _("Database '%1' cleared, now restore the data"), (dbname, )) restore.setMaster(self.master) guiReport(_("Restore Database"), restore, guimessage) self.usersPrivileges(self.master) # adjust display, select new db self.initDBlist()
def slot_restore(self, arg): """Restore a dumped database. It can be either an existing one, or one which has been deleted. """ # Get source file: dbpath = self.getBDbPath() if not dbpath: return None restore = Restore(dbpath) dbname = restore.getDbName() if not dbname: message(_("Couldn't open database file '%1'"), (dbpath,)) return state = 0 try: if dbname in self.dbList: if not confirmationDialog(_("Replace Database?"), argSub(_("Are you sure you want to replace database '%1'?"), (dbname,)), False): restore.close() return self.deletedb(dbname) self.db.send(u"""CREATE DATABASE %s OWNER %s ENCODING 'UTF8'""" % (dbname, ADMIN)) state = 1 # Add to 'databases' table self.db.send(u"INSERT INTO databases VALUES (?, ?, ?, ?)", (self.db.getTime(), dbname, u'', u'')) state = 2 newmaster = self.connect(dbname) state = 3 guimessage = argSub(_("New database '%1' created, now read in the data"), (dbname,)) restore.setMaster(newmaster) guiReport(_("Restore Database"), restore, guimessage) #message(_("New database now set up")) self.usersPrivileges(newmaster) # Ensure connection is closed restore = None newmaster.close() newmaster = None except: print_exc() message(_("Couldn't create new database (%1)"), (dbname,)) if (state >= 3): newmaster.close() if (state >= 2): self.db.send(u"DELETE FROM databases WHERE name = ?", (dbname,)) if (state >= 1): self.db.send(u"DROP DATABASE %s" % dbname) # adjust display, select new db self.initDBlist()
def updateDbConfig(self, source): """Update the current database using the configuration file supplied as a CfgZip object in source. The selected config file must match the name of the current database. Before updating from this file, dump the current database state to a folder 'dumps' in the same folder as the config file. That is in case something goes wrong and the old state must be recovered. """ if (self.dbname != source.cfgName): message(_("Database name does not match data folder")) return # Backup existing database state. sPath = self.configEd.getSourcePath() budir = os.path.join(os.path.dirname(sPath), 'dumps') if not os.path.isdir(budir): os.mkdir(budir) backup = Dump(self.master, budir) filepath = backup.filepath if not filepath: return guimessage = argSub(_("New backup file '%1' created, now read in the data"), (filepath,)) guiReport(_("Create Backup File"), backup, guimessage) backup = None if not filepath: return try: guimessage = argSub(_("Updating database '%1' from %2"), (self.dbname, sPath)) mm = MakeMaster(source, self.master) guiReport(_("Updating Master Database"), mm, guimessage) mm = None except: print_exc() message(_("Update failed, trying to restore from '%1'"), (filepath,)) restore = Restore(filepath) dbname = restore.getDbName() if not dbname: message(_("Couldn't open database file '%1'"), (filepath,)) return # Delete all tables for t in self.master.getTables(): self.master.send(u"DROP TABLE %s" % t) # Restore old state guimessage = argSub(_("Database '%1' cleared, now restore the data"), (dbname,)) restore.setMaster(self.master) guiReport(_("Restore Database"), restore, guimessage) self.usersPrivileges(self.master) # adjust display, select new db self.initDBlist()