コード例 #1
0
def test_compression(tmp_path):
    fpath = tmp_path / "test.zim"
    with Creator(tmp_path / "test.zim", "welcome", "",
                 compression="lzma") as creator:
        creator.add_item(StaticItem(path="welcome", content="hello"))

    with Creator(fpath, "welcome", "",
                 compression=Compression.lzma) as creator:
        creator.add_item(StaticItem(path="welcome", content="hello"))
コード例 #2
0
def test_double_finish(tmp_path):
    fpath = tmp_path / "test.zim"
    with Creator(fpath, "welcome", "fra") as creator:
        creator.add_item(StaticItem(path="welcome", content="hello"))

    # ensure we can finish an already finished creator
    creator.finish()
コード例 #3
0
def test_sourcefile_noremoval(tmp_path, html_file):
    # copy html to folder
    src_path = tmp_path / "source.html"
    shutil.copyfile(html_file, src_path)

    fpath = tmp_path / "test.zim"
    with Creator(fpath) as creator:
        creator.add_item(StaticItem(path=src_path.name, filepath=src_path))

    assert src_path.exists()
コード例 #4
0
def test_item_callback(tmp_path, html_file):
    fpath = tmp_path / "test.zim"

    class Store:
        called = False

    def cb():
        Store.called = True

    with Creator(fpath) as creator:
        creator.add_item(StaticItem(path=html_file.name, filepath=html_file),
                         callback=cb)

    assert Store.called is True
コード例 #5
0
def test_sourcefile_removal(tmp_path, html_file):

    fpath = tmp_path / "test.zim"
    with Creator(fpath) as creator:
        # using a temp dir so file still have a meaningful name
        tmpdir = tempfile.TemporaryDirectory(
            dir=tmp_path)  # can't use contextmgr
        # copy html to folder
        src_path = pathlib.Path(tmpdir.name, "source.html")
        shutil.copyfile(html_file, src_path)
        creator.add_item(
            StaticItem(filepath=src_path, path=src_path.name, ref=tmpdir))
        del tmpdir

    assert not src_path.exists()
コード例 #6
0
def test_noindexlanguage(tmp_path):
    fpath = tmp_path / "test.zim"
    with Creator(fpath, "welcome", "") as creator:
        creator.add_item(StaticItem(path="welcome", content="hello"))
        creator.update_metadata(language="bam")
        creator.add_item_for("index",
                             "Index",
                             content="-",
                             mimetype="text/html")

    reader = Archive(fpath)
    assert reader.get_metadata("Language").decode(UTF8) == "bam"
    # html content triggers both title and content xapian indexes
    # but since indexing is disabled, we should only have title one
    assert reader.has_title_index
    assert not reader.has_fulltext_index
コード例 #7
0
def test_sourcefile_removal_std(tmp_path, html_file):

    fpath = tmp_path / "test.zim"
    paths = []
    with Creator(fpath) as creator:
        for idx in range(0, 4):
            # copy html to folder
            paths.append(pathlib.Path(tmp_path / f"source{idx}.html"))
            shutil.copyfile(html_file, paths[-1])
            creator.add_item(
                StaticItem(
                    filepath=paths[-1],
                    path=paths[-1].name,
                    mimetype="text/html",
                ),
                callback=(delete_callback, paths[-1]),
            )
    for path in paths:
        assert not path.exists()
コード例 #8
0
def test_add_item_for_delete_fail(tmp_path, png_image):
    fpath = tmp_path / "test.zim"
    local_path = pathlib.Path(tmp_path / "somefile.png")

    # copy file to local path
    shutil.copyfile(png_image, local_path)

    def remove_source(item):
        print("##########", "remove_source")
        os.remove(item.filepath)

    with Creator(fpath, "welcome", "") as creator:
        creator.add_item(
            StaticItem(filepath=local_path,
                       path="index",
                       callback=remove_source),
            callback=(delete_callback, local_path),
        )
    assert not local_path.exists()

    reader = Archive(fpath)
    assert reader.get_item("index")