コード例 #1
0
ファイル: notebook.py プロジェクト: timckuhn/zim-desktop-wiki
    def setUp(self):
        folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_MOCK)
        layout = FilesLayout(folder, endofline='unix')
        index = Index(':memory:', layout)

        ### 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)

        dir = None
        cache_dir = None
        self.notebook = Notebook(dir, cache_dir, config, folder, layout, index)
        index.check_and_update()
コード例 #2
0
ファイル: __init__.py プロジェクト: xbynet/zim-desktop-wiki
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