def __init__(self, dbm, dir, teacher=u""): self.dbm = dbm self.dbname = self.dbm.getName() self.teacher = teacher self.ctime = self.dbm.getTime() if teacher: # The filename is of the form 'dbname'_'user'.zga filename = u"%s_%s.zga" % (self.dbname, teacher) force = False else: # The filename is of the form 'dbname'_'time'.zgb filename = u"%s_%s.zgb" % (self.dbname, self.ctime) force = True self.filepath = os.path.join(dir, filename) if not checkOverwrite(self.filepath, force): self.filepath = None return # Create/open database file self.dbs = DBn(self.filepath, new=True) if not self.dbs.isOpen(): self.filepath = None return
def __init__(self, path): # path is the full path to the sqlite database. DBs.__init__(self, path) if not self.db: return try: if self.readValue(u"config", u"dbversion") == DBVERSION: # Parse 'base' data self.baseDict = self.getData(u"base") return except: pass self.db = None warning(_("File '%1' is not a 'Zeugs' database, version '%2'"), (path, DBVERSION))
def __init__(self, path): # path is the full path to the sqlite database. DBs.__init__(self, path) if not self.db: return try: if (self.readValue(u"config", u"dbversion") == DBVERSION): # Parse 'base' data self.baseDict = self.getData(u"base") return except: pass self.db = None warning(_("File '%1' is not a 'Zeugs' database, version '%2'"), (path, DBVERSION))
class Dump: """Create a slave (sqlite) database file in the given directory. It can be a backup/print file (no teacher), or a teacher's database file. teacher's file: <dbname>_<teacher>.zga an existing one cannot be overwritten. backup/print file: <dbname>_<time>.zgb The constructor opens the sqlite file, the method 'run' does the backup. Note that in dump files all the reports are collected in a single table, 'reports', as user authentication is not relevant here. """ def __init__(self, dbm, dir, teacher=u""): self.dbm = dbm self.dbname = self.dbm.getName() self.teacher = teacher self.ctime = self.dbm.getTime() if teacher: # The filename is of the form 'dbname'_'user'.zga filename = u"%s_%s.zga" % (self.dbname, teacher) force = False else: # The filename is of the form 'dbname'_'time'.zgb filename = u"%s_%s.zgb" % (self.dbname, self.ctime) force = True self.filepath = os.path.join(dir, filename) if not checkOverwrite(self.filepath, force): self.filepath = None return # Create/open database file self.dbs = DBn(self.filepath, new=True) if not self.dbs.isOpen(): self.filepath = None return def run(self, gui): """Copy the master database to the slave. """ try: # Create tables table = u"config" gui.report(_("Creating table '%s'") % table) self.makeTable(table) self.dumpTable(table) gui.report(_("Creating table 'data'")) self.dbs.createDataTable() if self.teacher: # Don't dump the image files, just that for the current teacher self.dumpDataTable( r"(?!imagefiles/)|imagefiles/teachers/%s\." % self.teacher) else: self.dumpDataTable() # Copy reports gui.report(_("Copying reports ...")) table = u"reports" self.makeTable(table) if self.teacher: # Database user for self.teacher (also report table name) user = teacher2user(self.teacher) # Only copy the owner's reports self.dumpTable(user, table) else: for tm in self.dbm.getTeacherTables(): self.dumpTable(tm, table) #********* This was just an idea, but it may never be used. # # Copy comments # gui.report(_("Copying comments ...")) # table = u"comments" # self.makeTable(table) # if self.teacher: # # Only copy comments pertaining to the owner's reports # okIds = self.dbm.listIds(user) # for c in self.dbm.listIds(table): # if c in okIds: # for v in self.dbm.read(u"""SELECT value FROM comments # WHERE id = ?""", (c,)): # self.dbs.send(u"""INSERT INTO comments # VALUES(?, ?)""", (c, v[0])) # # else: # self.dumpTable(table) if self.teacher: # Set creation time self.dbs.send( u"""INSERT INTO config VALUES('createtime', ?)""", (self.ctime, )) # Set db-file 'owner' self.dbs.send( u"""UPDATE config SET value = ? WHERE id = 'me'""", (self.teacher, )) else: # Set the last backup time self.dbs.send( u"""UPDATE config SET value = ? WHERE id = 'backuptime'""", (self.ctime, )) self.dbm.send( u"""UPDATE config SET value = ? WHERE id = 'backuptime'""", (self.ctime, )) self.dbs.close() self.dbs = None gui.report(_("DONE!")) except: print_exc() message( _("Couldn't create database file." " Removing incomplete file (%1)"), (self.filepath, )) if self.dbs.isOpen(): self.dbs.close() os.remove(self.filepath) self.dbs = None self.filepath = None #raise def makeTable(self, name): """Create a new database table with the given name and standard text fields 'id' and 'value'. """ if not self.dbs.createIVTable(name): message(_("Couldn't create table '%1'"), (name, )) raise def dumpTable(self, name, name2=None, filter=".+"): """Copy the contents of table name from the master to table name2 in the slave. If name2 is not given use name. Both tables are assumed to have the 'standard' fields 'id' and 'value'. Only copy if the 'id' field matches the filter expression. """ rx = re.compile(filter) if not name2: name2 = name sqlsel = u"SELECT * FROM %s" % name sqlins = u"INSERT INTO %s VALUES(?, ?)" % name2 for row in self.dbm.read(sqlsel): if rx.match(row[0]): self.dbs.send(sqlins, row) def dumpDataTable(self, filter=".+"): """Copy the files from the table 'data' from master to slave. Only copy if the 'id' field matches the filter expression. """ rx = re.compile(filter) for id in self.dbm.listIds(u"data"): if rx.match(id): self.dbs.putFile(id, self.dbm.getBFile(id))
class Dump: """Create a slave (sqlite) database file in the given directory. It can be a backup/print file (no teacher), or a teacher's database file. teacher's file: <dbname>_<teacher>.zga an existing one cannot be overwritten. backup/print file: <dbname>_<time>.zgb The constructor opens the sqlite file, the method 'run' does the backup. Note that in dump files all the reports are collected in a single table, 'reports', as user authentication is not relevant here. """ def __init__(self, dbm, dir, teacher=u""): self.dbm = dbm self.dbname = self.dbm.getName() self.teacher = teacher self.ctime = self.dbm.getTime() if teacher: # The filename is of the form 'dbname'_'user'.zga filename = u"%s_%s.zga" % (self.dbname, teacher) force = False else: # The filename is of the form 'dbname'_'time'.zgb filename = u"%s_%s.zgb" % (self.dbname, self.ctime) force = True self.filepath = os.path.join(dir, filename) if not checkOverwrite(self.filepath, force): self.filepath = None return # Create/open database file self.dbs = DBn(self.filepath, new=True) if not self.dbs.isOpen(): self.filepath = None return def run(self, gui): """Copy the master database to the slave. """ try: # Create tables table = u"config" gui.report(_("Creating table '%s'") % table) self.makeTable(table) self.dumpTable(table) gui.report(_("Creating table 'data'")) self.dbs.createDataTable() if self.teacher: # Don't dump the image files, just that for the current teacher self.dumpDataTable(r"(?!imagefiles/)|imagefiles/teachers/%s\." % self.teacher) else: self.dumpDataTable() # Copy reports gui.report(_("Copying reports ...")) table = u"reports" self.makeTable(table) if self.teacher: # Database user for self.teacher (also report table name) user = teacher2user(self.teacher) # Only copy the owner's reports self.dumpTable(user, table) else: for tm in self.dbm.getTeacherTables(): self.dumpTable(tm, table) #********* This was just an idea, but it may never be used. # # Copy comments # gui.report(_("Copying comments ...")) # table = u"comments" # self.makeTable(table) # if self.teacher: # # Only copy comments pertaining to the owner's reports # okIds = self.dbm.listIds(user) # for c in self.dbm.listIds(table): # if c in okIds: # for v in self.dbm.read(u"""SELECT value FROM comments # WHERE id = ?""", (c,)): # self.dbs.send(u"""INSERT INTO comments # VALUES(?, ?)""", (c, v[0])) # # else: # self.dumpTable(table) if self.teacher: # Set creation time self.dbs.send(u"""INSERT INTO config VALUES('createtime', ?)""", (self.ctime,)) # Set db-file 'owner' self.dbs.send(u"""UPDATE config SET value = ? WHERE id = 'me'""", (self.teacher,)) else: # Set the last backup time self.dbs.send(u"""UPDATE config SET value = ? WHERE id = 'backuptime'""", (self.ctime,)) self.dbm.send(u"""UPDATE config SET value = ? WHERE id = 'backuptime'""", (self.ctime,)) self.dbs.close() self.dbs = None gui.report(_("DONE!")) except: print_exc() message(_("Couldn't create database file." " Removing incomplete file (%1)"), (self.filepath,)) if self.dbs.isOpen(): self.dbs.close() os.remove(self.filepath) self.dbs = None self.filepath = None #raise def makeTable(self, name): """Create a new database table with the given name and standard text fields 'id' and 'value'. """ if not self.dbs.createIVTable(name): message(_("Couldn't create table '%1'"), (name,)) raise def dumpTable(self, name, name2=None, filter=".+"): """Copy the contents of table name from the master to table name2 in the slave. If name2 is not given use name. Both tables are assumed to have the 'standard' fields 'id' and 'value'. Only copy if the 'id' field matches the filter expression. """ rx = re.compile(filter) if not name2: name2 = name sqlsel = u"SELECT * FROM %s" % name sqlins = u"INSERT INTO %s VALUES(?, ?)" % name2 for row in self.dbm.read(sqlsel): if rx.match(row[0]): self.dbs.send(sqlins, row) def dumpDataTable(self, filter=".+"): """Copy the files from the table 'data' from master to slave. Only copy if the 'id' field matches the filter expression. """ rx = re.compile(filter) for id in self.dbm.listIds(u"data"): if rx.match(id): self.dbs.putFile(id, self.dbm.getBFile(id))