def testReloadIndexWhileOngoing(self): from zim.notebook.operations import ongoing_operation from zim.notebook.index import IndexCheckAndUpdateOperation op = IndexCheckAndUpdateOperation(self.notebook) op_iter = iter(op) next(op_iter) self.assertEqual(ongoing_operation(self.notebook), op) self.uiactions.reload_index() self.assertIsNone(ongoing_operation(self.notebook))
def reload_index(self, update_only=False): '''Check the notebook for changes and update the index. Shows an progressbar while updateing. @param update_only: if C{True} only updates are done, if C{False} also check is done for all files @returns: C{True} unless the user cancelled the update ''' from zim.notebook.index import IndexCheckAndUpdateOperation, IndexUpdateOperation from zim.notebook.operations import ongoing_operation op = ongoing_operation(self.notebook) if isinstance(op, IndexUpdateOperation): dialog = ProgressDialog(self.widget, op) dialog.run() if update_only or isinstance(op, IndexCheckAndUpdateOperation): return not dialog.cancelled else: # ongoing op was update only but we want check, so try again if not dialog.cancelled: return self.reload_index() # recurs else: return False else: op = IndexCheckAndUpdateOperation(self.notebook) dialog = ProgressDialog(self.widget, op) dialog.run() return not dialog.cancelled
def on_out_of_date_found(notebook, background_check): op = IndexUpdateOperation(notebook) op.connect('finished', lambda *a: background_check.start()) # continue checking # TODO ensure robust in case operation gives error try: op.run_on_idle() except NotebookOperationOngoing: other_op = ongoing_operation(notebook) other_op.connect('finished', lambda *a: background_check.start()) # continue checking
def on_out_of_date_found(notebook, background_check): op = IndexUpdateOperation(notebook) op.connect('finished', lambda *a: background_check.start()) # continue checking other_op = ongoing_operation(notebook) if other_op: other_op.connect( 'finished', lambda *a: background_check.start()) # continue checking else: op.run_on_idle()
def destroy(self): self.pageview.save_changes() if self.page.modified: return # Do not quit if page not saved self._do_close() while Gtk.events_pending(): Gtk.main_iteration_do(False) self.notebook.index.stop_background_check() op = ongoing_operation(self.notebook) if op: op.wait() Window.destroy(self) # gtk destroy & will also emit destroy signal
def runTest(self): plugin = VersionControlPlugin() dir = get_tmp_dir('versioncontrol_TestMainWindowExtension') notebook = self.setUpNotebook(mock=tests.MOCK_ALWAYS_REAL, content=('Test', ), folder=LocalFolder(dir.path)) mainwindow = setUpMainWindow(notebook) plugin.extend(notebook) plugin.extend(mainwindow) notebook_ext = find_extension(notebook, NotebookExtension) window_ext = find_extension(mainwindow, VersionControlMainWindowExtension) op = ongoing_operation(notebook) assert op is None # check no opperation ongoing ## init & save version self.assertIsNone(notebook_ext.vcs) def init(dialog): self.assertIsInstance(dialog, VersionControlInitDialog) choice = dialog.combobox.get_active_text() self.assertTrue(choice and not choice.isspace()) dialog.answer_yes() with tests.DialogContext(init, SaveVersionDialog): window_ext.save_version() self.assertIsNotNone(notebook_ext.vcs) self.assertFalse(notebook_ext.vcs.is_modified()) ## save version again page = notebook.get_page(Path('Foo')) page.parse('wiki', 'foo!') notebook.store_page(page) self.assertTrue(notebook_ext.vcs.is_modified()) with tests.DialogContext(SaveVersionDialog): window_ext.save_version() self.assertFalse(notebook_ext.vcs.is_modified()) ## show versions with tests.DialogContext(VersionsDialog): window_ext.show_versions() ## auto-save plugin.preferences['autosave'] = True page = notebook.get_page(Path('Fooooo')) page.parse('wiki', 'foo!') notebook.store_page(page) self.assertTrue(notebook_ext.vcs.is_modified()) mainwindow.emit('close') self.assertFalse(notebook_ext.vcs.is_modified()) tests.gtk_process_events() assert ongoing_operation(notebook) is None
def runTest(self): dir = Dir(self.create_tmp_dir()) notebook = Notebook.new_from_dir(dir) page = notebook.get_page(Path('SomePage')) orig_store_page_1 = notebook.store_page orig_store_page_2 = notebook.store_page_async store_page_counter = tests.Counter() def wrapper1(page): store_page_counter() orig_store_page_1(page) def wrapper2(page, tree): store_page_counter() return orig_store_page_2(page, tree) notebook.store_page = wrapper1 notebook.store_page_async = wrapper2 pageview = tests.MockObject() pageview.readonly = False handler = SavePageHandler(pageview, notebook, lambda: page) # Normal operation self.assertFalse(page.modified) handler.try_save_page() self.assertEqual(store_page_counter.count, 0) self.assertFalse(page.modified) handler.save_page_now() self.assertEqual(store_page_counter.count, 1) page.modified = True handler.try_save_page() self.assertEqual(store_page_counter.count, 2) ongoing_operation(notebook)() # effectively a join self.assertFalse(page.modified) page.modified = True handler.save_page_now() self.assertEqual(store_page_counter.count, 3) self.assertFalse(page.modified) # With errors def wrapper3(page): raise AssertionError def wrapper4(page, tree): def error_cb(): raise AssertionError return orig_store_page_2(page, error_cb) notebook.store_page = wrapper3 notebook.store_page_async = wrapper4 page.modified = True def catch_dialog(dialog): assert isinstance(dialog, SavePageErrorDialog) with tests.LoggingFilter('zim'): with tests.DialogContext(catch_dialog): handler.save_page_now() self.assertTrue(page.modified) # For autosave first error is ignore, 2nd results in dialog self.assertFalse(handler._error_event and handler._error_event.is_set()) with tests.LoggingFilter('zim'): handler.try_save_page() ongoing_operation(notebook)() # effectively a join self.assertTrue(handler._error_event and handler._error_event.is_set()) with tests.LoggingFilter('zim'): with tests.DialogContext(catch_dialog): handler.try_save_page() self.assertTrue(page.modified)