def new_notebook(fakedir=None): '''Returns a new Notebook object with all data in memory Uses data from L{WikiTestData} @param fakedir: optional parameter to set the 'dir' attribute for the notebook and the main store which allows you to resolve file paths etc. It will not automatically touch the dir (hence it being 'fake'). ''' from zim.fs import Dir from zim.notebook import Notebook, Path from zim.index import Index notebook = Notebook(index=Index(dbfile=':memory:')) store = notebook.add_store(Path(':'), 'memory') manifest = [] for name, text in WikiTestData: manifest.append(name) store.set_node(Path(name), text) notebook.testdata_manifest = _expand_manifest(manifest) notebook.index.update() if fakedir: dir = Dir(fakedir) notebook.dir = dir store.dir = dir return notebook
def new_files_notebook(dir): '''Returns a new Notebook object with a file store Uses data from L{WikiTestData} @param path: a folder path, e.g. created by L{TestCase.create_tmp_dir()} ''' from zim.fs import Dir from zim.notebook import init_notebook, Notebook, Path from zim.index import Index dir = Dir(dir) init_notebook(dir) notebook = Notebook(dir=dir) store = notebook.get_store(':') manifest = [] for name, text in WikiTestData: manifest.append(name) page = store.get_page(Path(name)) page.parse('wiki', text) store.store_page(page) notebook.testdata_manifest = _expand_manifest(manifest) notebook.index.update() return notebook
def export(self): dir = Dir(create_tmp_dir('export_SourceFiles')) notebook = Notebook(dir=dir) for name, text in get_test_data('wiki'): page = notebook.get_page(Path(name)) page.parse('wiki', text) notebook.store_page(page) file = dir.file('Test/foo.txt') self.assertTrue(file.exists()) check_call(['python', './zim.py', '--export', '--template=Default', dir.path, '--output', self.dir.path])
def get_test_notebook(format='wiki'): '''Returns a notebook with a memory store and some test data''' from zim.notebook import Notebook, Path from zim.index import Index notebook = Notebook(index=Index(dbfile=':memory:')) store = notebook.add_store(Path(':'), 'memory') manifest = [] for name, text in get_test_data(format): manifest.append(name) store.set_node(Path(name), text) notebook.testdata_manifest = expand_manifest(manifest) return notebook
def export(self): dir = Dir(self.create_tmp_dir('source_files')) init_notebook(dir) notebook = Notebook(dir=dir) for name, text in tests.WikiTestData: page = notebook.get_page(Path(name)) page.parse('wiki', text) notebook.store_page(page) file = dir.file('Test/foo.txt') self.assertTrue(file.exists()) zim = Application(('./zim.py', '--export', '--template=Default', dir.path, '--output', self.dir.path, '--index-page', 'index')) zim.run()
def get_files_notebook(key): # We fill the notebook using the store interface, as this test comes before # the notebook test, but after the store test. dir = Dir(tests.create_tmp_dir('index_'+key)) notebook = Notebook(dir=dir) store = notebook.get_store(':') manifest = [] for name, text in tests.get_test_data('wiki'): manifest.append(name) page = store.get_page(Path(name)) page.parse('wiki', text) store.store_page(page) notebook.testdata_manifest = tests.expand_manifest(manifest) return notebook
def runTest(self): '''Test if included notebooks are up to date''' from zim.fs import Dir from zim.notebook import Notebook for path in ('data/manual', 'HACKING'): notebook = Notebook(dir=Dir(path)) self.assertTrue(not notebook.needs_upgrade)
def get_notebook_argument(self): '''Get the notebook and page arguments for this command @returns: a 2-tuple of an L{NotebookInfo} object and an optional L{Path} or C{(None, None)} if the notebook argument is optional and not given @raises NotebookLookupError: if the notebook is mandatory and not given, or if it is given but could not be resolved ''' assert self.arguments[0] in ('NOTEBOOK', '[NOTEBOOK]') args = self.get_arguments() notebook = args[0] if notebook is None: notebook = self.get_default_or_only_notebook() if notebook: logger.info('Using default notebook: %s', notebook) elif self.arguments[0] == 'NOTEBOOK': # not optional raise NotebookLookupError, _('Please specify a notebook') # T: Error when looking up a notebook else: return None, None notebookinfo = resolve_notebook(notebook) if not notebookinfo: raise NotebookLookupError, _('Could not find notebook: %s') % notebook if len(self.arguments) > 1 \ and self.arguments[1] in ('PAGE', '[PAGE]') \ and args[1] is not None: pagename = Notebook.cleanup_pathname(args[1], purge=True) return notebookinfo, Path(pagename) else: return notebookinfo, None
def get_notebook_argument(self): '''Get the notebook and page arguments for this command @returns: a 2-tuple of an L{NotebookInfo} object and an optional L{Path} or C{(None, None)} if the notebook argument is optional and not given @raises NotebookLookupError: if the notebook is mandatory and not given, or if it is given but could not be resolved ''' assert self.arguments[0] in ('NOTEBOOK', '[NOTEBOOK]') args = self.get_arguments() notebook = args[0] if notebook is None: notebook = self.get_default_or_only_notebook() if notebook: logger.info('Using default notebook: %s', notebook) elif self.arguments[0] == 'NOTEBOOK': # not optional raise NotebookLookupError, _('Please specify a notebook') # T: Error when looking up a notebook else: return None, None notebookinfo = resolve_notebook(notebook) if not notebookinfo: raise NotebookLookupError, _( 'Could not find notebook: %s') % notebook # T: error message if len(self.arguments) > 1 \ and self.arguments[1] in ('PAGE', '[PAGE]') \ and args[1] is not None: pagename = Notebook.cleanup_pathname(args[1], purge=True) return notebookinfo, Path(pagename) else: return notebookinfo, None
def setUp(self): klass = zim.stores.get_store('memory') self.store = klass(path=Path(':'), notebook=Notebook()) self.index = set() for name, text in tests.WikiTestData: self.store.set_node(Path(name), text) self.index.add(name) self.normalize_index()
def export(self): dir = Dir(self.create_tmp_dir('source_files')) init_notebook(dir) notebook = Notebook(dir=dir) for name, text in tests.WikiTestData: page = notebook.get_page(Path(name)) page.parse('wiki', text) notebook.store_page(page) file = dir.file('Test/foo.txt') self.assertTrue(file.exists()) argv = ('./zim.py', '--export', '--template=Default', dir.path, '--output', self.dir.path, '--index-page', 'index') #~ zim = Application(argv) #~ zim.run() cmd = zim.main.build_command(argv[1:]) cmd.run()
def write_notebook(dir, pages): init_notebook(dir) notebook = Notebook.new_from_dir(dir) for path, content in pages: p = notebook.get_page(path) p.parse('wiki', content) notebook.store_page(p) print "Wrote", p.source.path
def setUp(self): TestStoresMemory.setUp(self) tmpdir = self.create_tmp_dir(u'_some_utf8_here_\u0421\u0430\u0439') self.dir = Dir([tmpdir, 'store-files']) self.mem = self.store klass = zim.stores.get_store('files') self.store = klass(path=Path(':'), notebook=Notebook(), dir=self.dir) for parent, page in walk(self.mem): if page.hascontent: mypage = self.store.get_page(page) mypage.set_parsetree(page.get_parsetree()) self.store.store_page(mypage)
def runTest(self): notebook = tests.new_notebook() page = notebook.get_page(Path('FooBar')) page.parse('wiki', '''\ ====== Page Heading ====== **foo bar !** ''') self.assertTrue(len(page.dump('html', linker=StubLinker())) > 0) proxy = PageProxy(Notebook(), page, zim.formats.get_format('html'), StubLinker(), {}) self.assertEqual(proxy.name, page.name) self.assertEqual(proxy.namespace, page.namespace) self.assertEqual(proxy.basename, page.basename) self.assertTrue(isinstance(proxy.properties, dict)) self.assertTrue(len(proxy.body) > 0)
def on_text_changed(self, buffer): if not self._title_set_manually: # Automatically generate a (valid) page name self._updating_title = True bounds = buffer.get_bounds() title = buffer.get_text(*bounds).strip()[:50] # Cut off at 50 characters to prevent using a whole paragraph title = title.replace(':', '') if '\n' in title: title, _ = title.split('\n', 1) try: title = Notebook.cleanup_pathname(title, purge=True) self.form['basename'] = title except PageNameError: pass self._updating_title = False
def runTest(self): '''Test synchronization''' # Test if zim detects pages, that where created with another # zim instance and transfered to this instance with # dropbox or another file synchronization tool. # # The scenario is as follow: # 1) Page.txt is created in this instance # 2) Page/Subpage.txt is created in another instance # and copied into the notebook by the synchronization tool # 3) Zim runs a standard index update # Outcome should be that Page:Subpage shows up in the index # create notebook dir = Dir(self.create_tmp_dir()) init_notebook(dir, name='foo') notebook = Notebook(dir=dir) index = notebook.index index.update() # add page in this instance path = Path('Page') page = notebook.get_page(path) page.parse('wiki', 'nothing important') notebook.store_page(page) # check file exists self.assertTrue(dir.file('Page.txt').exists()) # check file is indexed self.assertTrue(page in list(index.list_all_pages())) # check attachment dir does not exist subdir = dir.subdir('Page') self.assertEqual(notebook.get_attachments_dir(page), subdir) self.assertFalse(subdir.exists()) for newfile, newpath in ( (subdir.file('NewSubpage.txt').path, Path('Page:NewSubpage')), (dir.file('Newtoplevel.txt').path, Path('Newtoplevel')), (dir.file('SomeDir/Dir/Newpage.txt').path, Path('SomeDir:Dir:Newpage')), ): # make sure ctime changed since last index import time time.sleep(2) # create new page without using zim classes self.assertFalse(os.path.isfile(newfile)) mydir = os.path.dirname(newfile) if not os.path.isdir(mydir): os.makedirs(mydir) fh = open(newfile, 'w') fh.write('Test 123\n') fh.close() self.assertTrue(os.path.isfile(newfile)) # simple index reload index.update() # check if the new page is found in the index self.assertTrue(newpath in list(index.list_all_pages()))
def setUp(self): buffer = StubFile(self.xml) klass = zim.stores.get_store('xml') self.store = klass(path=Path(':'), notebook=Notebook(), file=buffer) self.index = set(['Foo', 'Foo:Bar', 'Baz', u'utf8:\u03b1\u03b2\u03b3']) self.normalize_index()
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)
def get_test_page(name='Foo'): '''FIXME''' from zim.notebook import Notebook, Path notebook = Notebook() notebook.add_store(Path(':'), 'memory') return notebook, notebook.get_page(Path(name))