Ejemplo n.º 1
0
    def runTest(self):
        '''Test FileEntry widget'''
        from zim.fs import adapt_from_newfs, Dir
        dir = Dir(self.notebook.folder)  # XXX

        path = Path('Foo:Bar')
        entry = self.entry
        entry.set_use_relative_paths(self.notebook, path)

        home = Dir('~')
        for file, text in (
            (home.file('zim-test.txt'), '~/zim-test.txt'),
            (dir.file('Foo/Bar/test.txt'), './test.txt'),
            (File('/test.txt'), File('/test.txt').path),  # win32 save
        ):
            entry.set_file(file)
            self.assertEqual(entry.get_text(), os_native_path(text))
            self.assertEqual(entry.get_file(), file)

        self.notebook.config['Notebook'][
            'document_root'] = './notebook_document_root'
        doc_root = self.notebook.document_root
        self.assertEqual(doc_root, dir.subdir('notebook_document_root'))

        for file, text in (
            (home.file('zim-test.txt'), os_native_path('~/zim-test.txt')),
            (dir.file('Foo/Bar/test.txt'), os_native_path('./test.txt')),
            (File('/test.txt'), File('/test.txt').uri),  # win32 save
            (doc_root.file('test.txt'), '/test.txt'),
        ):
            entry.set_file(file)
            self.assertEqual(entry.get_text(), text)
            self.assertEqual(entry.get_file(), file)

        entry.set_use_relative_paths(self.notebook, None)

        for file, text in (
            (home.file('zim-test.txt'), os_native_path('~/zim-test.txt')),
            (dir.file('Foo/Bar/test.txt'),
             os_native_path('./Foo/Bar/test.txt')),
            (File('/test.txt'), File('/test.txt').uri),  # win32 save
            (doc_root.file('test.txt'), '/test.txt'),
        ):
            entry.set_file(file)
            self.assertEqual(entry.get_text(), text)
            self.assertEqual(entry.get_file(), file)

        entry.set_use_relative_paths(notebook=None)

        for file, text in (
            (home.file('zim-test.txt'), '~/zim-test.txt'),
                #~ (dir.file('Foo/Bar/test.txt'), './test.txt'),
            (File('/test.txt'), File('/test.txt').path),  # win32 save
        ):
            entry.set_file(file)
            self.assertEqual(entry.get_text(), text)
            self.assertEqual(entry.get_file(), file)
Ejemplo n.º 2
0
    def runTest(self):
        '''Test FileEntry widget'''
        path = Path('Foo:Bar')
        entry = self.entry
        entry.set_use_relative_paths(self.notebook, path)

        home = Dir('~')
        dir = self.notebook.dir
        for file, text in (
            (home.file('zim-test.txt'), '~/zim-test.txt'),
            (dir.file('Foo/Bar/test.txt'), './test.txt'),
            (File('/test.txt'), File('/test.txt').path),  # win32 save
        ):
            entry.set_file(file)
            self.assertEqual(entry.get_text(), os_native_path(text))
            self.assertEqual(entry.get_file(), file)

        self.notebook.config['Notebook'][
            'document_root'] = './notebook_document_root'
        self.notebook.do_properties_changed()  # parse config
        doc_root = self.notebook.document_root
        self.assertEqual(doc_root, dir.subdir('notebook_document_root'))

        for file, text in (
            (home.file('zim-test.txt'), os_native_path('~/zim-test.txt')),
            (dir.file('Foo/Bar/test.txt'), os_native_path('./test.txt')),
            (File('/test.txt'), File('/test.txt').uri),  # win32 save
            (doc_root.file('test.txt'), '/test.txt'),
        ):
            entry.set_file(file)
            self.assertEqual(entry.get_text(), text)
            self.assertEqual(entry.get_file(), file)

        entry.set_use_relative_paths(self.notebook, None)

        for file, text in (
            (home.file('zim-test.txt'), os_native_path('~/zim-test.txt')),
            (dir.file('Foo/Bar/test.txt'),
             os_native_path('./Foo/Bar/test.txt')),
            (File('/test.txt'), File('/test.txt').uri),  # win32 save
            (doc_root.file('test.txt'), '/test.txt'),
        ):
            entry.set_file(file)
            self.assertEqual(entry.get_text(), text)
            self.assertEqual(entry.get_file(), file)

        entry.set_use_relative_paths(notebook=None)

        for file, text in (
            (home.file('zim-test.txt'), '~/zim-test.txt'),
                #~ (dir.file('Foo/Bar/test.txt'), './test.txt'),
            (File('/test.txt'), File('/test.txt').path),  # win32 save
        ):
            entry.set_file(file)
            self.assertEqual(entry.get_text(), text)
            self.assertEqual(entry.get_file(), file)
Ejemplo n.º 3
0
    def testResolveFile(self):
        '''Test notebook.resolve_file()'''
        from zim.fs import adapt_from_newfs, Dir
        dir = Dir(self.notebook.folder.path)  # XXX

        path = Path('Foo:Bar')
        self.notebook.config['Notebook'][
            'document_root'] = './notebook_document_root'
        doc_root = self.notebook.document_root
        self.assertEqual(doc_root, dir.subdir('notebook_document_root'))
        for link, wanted, cleaned in (
            ('~/test.txt', File('~/test.txt'), '~/test.txt'),
            (r'~\test.txt', File('~/test.txt'), '~/test.txt'),
            ('file:///test.txt', File('file:///test.txt'), None),
            ('file:/test.txt', File('file:///test.txt'), None),
            ('file://localhost/test.txt', File('file:///test.txt'), None),
            ('/test.txt', doc_root.file('test.txt'), '/test.txt'),
            ('../../notebook_document_root/test.txt',
             doc_root.file('test.txt'), '/test.txt'),
            ('./test.txt', dir.file('Foo/Bar/test.txt'), './test.txt'),
            (r'.\test.txt', dir.file('Foo/Bar/test.txt'), './test.txt'),
            ('../test.txt', dir.file('Foo/test.txt'), '../test.txt'),
            (r'..\test.txt', dir.file('Foo/test.txt'), '../test.txt'),
            ('../Bar/Baz/test.txt', dir.file('Foo/Bar/Baz/test.txt'),
             './Baz/test.txt'),
            (r'C:\foo\bar', File('file:///C:/foo/bar'), None),
            (r'Z:\foo\bar', File('file:///Z:/foo/bar'), None),
        ):
            #~ print link, '>>', self.notebook.resolve_file(link, path)
            if cleaned is not None and not cleaned.startswith('/'):
                cleaned = os_native_path(cleaned)
            self.assertEqual(self.notebook.resolve_file(link, path), wanted)
            self.assertEqual(self.notebook.relative_filepath(wanted, path),
                             cleaned)

        # check relative path without Path
        self.assertEqual(
            self.notebook.relative_filepath(doc_root.file('foo.txt')),
            '/foo.txt')
        self.assertEqual(self.notebook.relative_filepath(dir.file('foo.txt')),
                         os_native_path('./foo.txt'))
Ejemplo n.º 4
0
	def assertMatchPath(self, file, path):
		if not file.path.startswith('<virtual>'):
			path = os_native_path(path)
		assert file.path.endswith(path), '"%s" does not match "%s"' % (file.path, path)
Ejemplo n.º 5
0
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 which allows you to resolve file
	paths etc. It will not automatically touch the dir
	(hence it being 'fake').
	'''
    import sqlite3

    from zim.fs import Dir
    from zim.config import VirtualConfigBackend
    from zim.notebook import Notebook, Path
    from zim.notebook.notebook import NotebookConfig
    from zim.notebook.index import Index

    from zim.notebook.layout import FilesLayout
    from zim.newfs.mock import MockFolder, clone_mock_object, os_native_path

    global _notebook_data
    if not _notebook_data:  # run this one time only
        templfolder = MockFolder('/mock/notebook')
        layout = FilesLayout(templfolder, endofline='unix')

        manifest = []
        for name, text in WikiTestData:
            manifest.append(name)
            file, x = layout.map_page(Path(name))
            file.write(text)

        manifest = frozenset(_expand_manifest(manifest))

        index = Index(':memory:', layout)
        index.check_and_update()
        lines = list(index._db.iterdump())
        sql = '\n'.join(lines)

        _notebook_data = (templfolder, sql, manifest)

    if fakedir:
        fakedir = fakedir if isinstance(fakedir, basestring) else fakedir.path
        fakedir = os_native_path(fakedir)
    templfolder, sql, manifest = _notebook_data
    folder = MockFolder(fakedir or templfolder.path)
    clone_mock_object(templfolder, folder)

    #~ print ">>>>>>>>>>>>>"
    #~ for path in folder._fs.tree():
    #~ print path

    layout = FilesLayout(folder, endofline='unix')
    index = Index(':memory:', layout)
    tables = [
        r[0] for r in index._db.execute(
            'SELECT name FROM sqlite_master '
            'WHERE type="table" and name NOT LIKE "sqlite%"')
    ]
    for table in tables:
        index._db.execute('DROP TABLE %s' % table)
    index._db.executescript(sql)
    index._db.commit()

    ### XXX - Big HACK here - Get better classes for this - XXX ###
    dir = VirtualConfigBackend()
    file = dir.file('notebook.zim')
    file.dir = dir
    file.dir.basename = 'Unnamed Notebook'
    ###
    config = NotebookConfig(file)

    notebook = Notebook(None, None, config, folder, layout, index)
    if fakedir:
        notebook.dir = Dir(fakedir)

    notebook.testdata_manifest = manifest
    return notebook