示例#1
0
    def setUpNotebook(self,
                      name='notebook',
                      mock=MOCK_ALWAYS_MOCK,
                      content={},
                      folder=None):
        '''
		@param name: name postfix for the folder, see L{setUpFolder}
		@param mock: see L{setUpFolder}, default is C{MOCK_ALWAYS_MOCK}
		@param content: dictionary where the keys are page names and the
		values the page content. If a tuple or list is given, pages are created
		with default text. L{Path} objects are allowed instead of page names
		@param folder: determine the folder to be used, only needed in special
		cases where the folder must be outside of the project folder, like
		when testing version control logic
		'''
        import datetime
        from zim.newfs.mock import MockFolder
        from zim.notebook.notebook import NotebookConfig, Notebook
        from zim.notebook.page import Path
        from zim.notebook.layout import FilesLayout
        from zim.notebook.index import Index
        from zim.formats.wiki import WIKI_FORMAT_VERSION

        if folder is None:
            folder = self.setUpFolder(name, mock)
        folder.touch()  # Must exist for sane notebook
        cache_dir = folder.folder('.zim')
        layout = FilesLayout(folder, endofline='unix')

        if isinstance(folder, MockFolder):
            conffile = folder.file('notebook.zim')
            config = NotebookConfig(conffile)
            index = Index(':memory:', layout)
        else:
            conffile = folder.file('notebook.zim')
            config = NotebookConfig(conffile)
            cache_dir.touch()
            index = Index(cache_dir.file('index.db').path, layout)

        if isinstance(content, (list, tuple)):
            content = dict((p, 'test 123') for p in content)

        notebook = Notebook(cache_dir, config, folder, layout, index)
        for name, text in list(content.items()):
            path = Path(name) if isinstance(name, str) else name
            file, folder = layout.map_page(path)
            file.write(
                ('Content-Type: text/x-zim-wiki\n'
                 'Wiki-Format: %s\n'
                 'Creation-Date: %s\n\n') %
                (WIKI_FORMAT_VERSION, datetime.datetime.now().isoformat()) +
                text)

        notebook.index.check_and_update()
        assert notebook.index.is_uptodate
        return notebook
示例#2
0
    def setUpNotebook(cls, name=None, mock=MOCK_ALWAYS_MOCK, content={}):
        '''
		@param name: basename for the folder, use class name if C{None}
		@param mock: see L{setUpFolder}, default is C{MOCK_ALWAYS_MOCK}
		@param content: dictionary where the keys are page names and the
		values the page content.
		'''
        import datetime
        from zim.newfs.mock import MockFolder
        from zim.config import VirtualConfigBackend
        from zim.notebook.notebook import NotebookConfig, Notebook
        from zim.notebook.page import Path
        from zim.notebook.layout import FilesLayout
        from zim.notebook.index import Index
        from zim.formats.wiki import WIKI_FORMAT_VERSION

        folder = cls.setUpFolder(name, mock)
        cache_dir = folder.folder('.zim')
        layout = FilesLayout(folder, endofline='unix')

        if isinstance(folder, MockFolder):
            ### XXX - Big HACK here - Get better classes for this - XXX ###
            dir = VirtualConfigBackend()
            file = dir.file('notebook.zim')
            file.dir = dir
            file.dir.basename = folder.basename
            ###
            config = NotebookConfig(file)
            index = Index(':memory:', layout)
        else:
            from zim.fs import Dir
            conffile = Dir(folder.path).file('notebook.zim')
            config = NotebookConfig(conffile)
            cache_dir.touch()
            index = Index(cache_dir.file('index.db').path, layout)

        notebook = Notebook(None, cache_dir, config, folder, layout, index)
        for name, text in content.items():
            file, folder = layout.map_page(Path(name))
            file.write(
                ('Content-Type: text/x-zim-wiki\n'
                 'Wiki-Format: %s\n'
                 'Creation-Date: %s\n\n') %
                (WIKI_FORMAT_VERSION, datetime.datetime.now().isoformat()) +
                text + '\n')

        notebook.index.check_and_update()
        return notebook
示例#3
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