def test_creator_metadata_nooverwrite(fpath, lipsum_item):
    with Creator(fpath) as c:
        c.add_item(lipsum_item)
        c.add_metadata("Key", "first")
        # re-setting a value prints a warning and ignore it
        c.add_metadata("Key", "second")
    zim = Archive(fpath)
    assert zim.get_metadata("Key").decode("UTF-8") == "first"
def test_creator_metadata(fpath, lipsum_item):
    metadata = {
        # kiwix-mandatory
        "Name":
        "wikipedia_fr_football",
        "Title":
        "English Wikipedia",
        "Creator":
        "English speaking Wikipedia contributors",
        "Publisher":
        "Wikipedia user Foobar",
        "Date":
        "2009-11-21",
        "Description":
        "All articles (without images) from the english Wikipedia",
        "Language":
        "eng",
        # optional
        "Longdescription":
        ("This ZIM file contains all articles (without images) "
         "from the english Wikipedia by 2009-11-10."
         " The topics are ..."),
        "Licence":
        "CC-BY",
        "Tags":
        "wikipedia;_category:wikipedia;_pictures:no;"
        "_videos:no;_details:yes;_ftindex:yes",
        "Flavour":
        "nopic",
        "Source":
        "https://en.wikipedia.org/",
        "Counter":
        "image/jpeg=5;image/gif=3;image/png=2",
        "Scraper":
        "sotoki 1.2.3",
    }

    # ensure we can't add if not started
    c = Creator(fpath)
    with pytest.raises(RuntimeError, match="not started"):
        key = next(iter(metadata.keys()))
        c.add_metadata(key, metadata.get(key))
    del c

    with Creator(fpath) as c:
        c.add_item(lipsum_item)
        for name, value in metadata.items():
            if name == "Date":
                continue
            c.add_metadata(name, value)

        mdate = datetime.date(
            *[int(x) for x in metadata.get("Date").split("-")])
        c.add_metadata("Date", mdate)

    zim = Archive(fpath)
    for name, value in metadata.items():
        assert zim.get_metadata(name).decode("UTF-8") == value
Exemple #3
0
def test_reader_metadata(
    all_zims, filename, metadata_keys, test_metadata, test_metadata_value
):

    zim = Archive(all_zims / filename)

    # make sure metadata_keys is empty
    assert zim.metadata_keys == metadata_keys
    if test_metadata:
        assert zim.get_metadata(test_metadata).decode("UTF-8") == test_metadata_value