class DBRestore(QDialog): update_signal = pyqtSignal(object, object) def __init__(self, parent, library_path): QDialog.__init__(self, parent) self.l = QVBoxLayout() self.setLayout(self.l) self.l1 = QLabel('<b>'+_('Restoring database from backups, do not' ' interrupt, this will happen in three stages')+'...') self.setWindowTitle(_('Restoring database')) self.l.addWidget(self.l1) self.pb = QProgressBar(self) self.l.addWidget(self.pb) self.pb.setMaximum(0) self.pb.setMinimum(0) self.msg = QLabel('') self.l.addWidget(self.msg) self.msg.setWordWrap(True) self.bb = QDialogButtonBox(QDialogButtonBox.Cancel) self.l.addWidget(self.bb) self.bb.rejected.connect(self.reject) self.resize(self.sizeHint() + QSize(100, 50)) self.error = None self.rejected = False self.library_path = library_path self.update_signal.connect(self.do_update, type=Qt.QueuedConnection) self.restorer = Restore(library_path, self) self.restorer.daemon = True # Give the metadata backup thread time to stop QTimer.singleShot(2000, self.start) def start(self): self.restorer.start() QTimer.singleShot(10, self.update) def reject(self): self.rejected = True self.restorer.progress_callback = lambda x, y: x QDialog.reject(self) def update(self): if self.restorer.is_alive(): QTimer.singleShot(10, self.update) else: self.restorer.progress_callback = lambda x, y: x self.accept() def __call__(self, msg, step): self.update_signal.emit(msg, step) def do_update(self, msg, step): if msg is None: self.pb.setMaximum(step) else: self.msg.setText(msg) self.pb.setValue(step)
def __init__(self, parent, library_path): QDialog.__init__(self, parent) self.l = QVBoxLayout() self.setLayout(self.l) self.l1 = QLabel('<b>'+_('Restoring database from backups, do not' ' interrupt, this will happen in three stages')+'...') self.setWindowTitle(_('Restoring database')) self.l.addWidget(self.l1) self.pb = QProgressBar(self) self.l.addWidget(self.pb) self.pb.setMaximum(0) self.pb.setMinimum(0) self.msg = QLabel('') self.l.addWidget(self.msg) self.msg.setWordWrap(True) self.bb = QDialogButtonBox(QDialogButtonBox.Cancel) self.l.addWidget(self.bb) self.bb.rejected.connect(self.reject) self.resize(self.sizeHint() + QSize(100, 50)) self.error = None self.rejected = False self.library_path = library_path self.update_signal.connect(self.do_update, type=Qt.QueuedConnection) self.restorer = Restore(library_path, self) self.restorer.daemon = True # Give the metadata backup thread time to stop QTimer.singleShot(2000, self.start)
def command_restore_database(args, dbpath): from calibre.library.restore import Restore parser = restore_database_option_parser() opts, args = parser.parse_args(args) if len(args) != 0: parser.print_help() return 1 if not opts.really_do_it: prints(_('You must provide the %s option to do a' ' recovery')%'--really-do-it', end='\n\n') parser.print_help() return 1 if opts.library_path is not None: dbpath = opts.library_path if isbytestring(dbpath): dbpath = dbpath.decode(preferred_encoding) class Progress(object): def __init__(self): self.total = 1 def __call__(self, msg, step): if msg is None: self.total = float(step) else: prints(msg, '...', '%d%%'%int(100*(step/self.total))) r = Restore(dbpath, progress_callback=Progress()) r.start() r.join() if r.tb is not None: prints('Restoring database failed with error:') prints(r.tb) else: prints('Restoring database succeeded') prints('old database saved as', r.olddb) if r.errors_occurred: name = 'calibre_db_restore_report.txt' open('calibre_db_restore_report.txt', 'wb').write(r.report.encode('utf-8')) prints('Some errors occurred. A detailed report was ' 'saved to', name)