Tofu/scripts/import-recipes/tests/buildSampleOdt.py
2026-09-18 01:41:02 +02:00

73 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Rebuilds sample-recipe.odt, the fixture the ODT tests run against.
The fixture is committed, so running this is only needed when the sample
document changes. The archive is laid out the way LibreOffice lays it out: an
uncompressed `mimetype` entry first, then the deflated parts.
"""
import zipfile
from pathlib import Path
HERE = Path(__file__).resolve().parent
CONTENT_XML = """<?xml version="1.0" encoding="UTF-8"?>
<office:document-content
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
office:version="1.3">
<office:automatic-styles>
<style:style style:name="T1" style:family="text"/>
</office:automatic-styles>
<office:body>
<office:text>
<text:h text:style-name="Heading_20_1" text:outline-level="1">Gratin de courgettes</text:h>
<text:p text:style-name="Standard">Pour 4 personnes</text:p>
<text:h text:style-name="Heading_20_2" text:outline-level="2">Ingr&#233;dients</text:h>
<text:p text:style-name="Standard">600 g de courgettes</text:p>
<text:p text:style-name="Standard">une poign&#233;e de pois chiches</text:p>
<text:p text:style-name="Standard">2 c. &#224; soupe d&apos;huile d&apos;olive<text:line-break/>sel &amp; poivre au go&#251;t</text:p>
<text:p text:style-name="Standard"/>
<text:h text:style-name="Heading_20_2" text:outline-level="2">Pr&#233;paration</text:h>
<text:p text:style-name="Standard">1.<text:tab/>Couper les courgettes en rondelles<text:span text:style-name="T1"> fines</text:span>.</text:p>
<text:p text:style-name="Standard">2.<text:tab/>Enfourner 30 minutes &#224;<text:s text:c="3"/>180 &#176;C.</text:p>
<text:p text:style-name="Standard"><text:bookmark text:name="fin"/>Servir chaud.</text:p>
</office:text>
</office:body>
</office:document-content>
"""
MANIFEST_XML = """<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.3">
<manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.text"/>
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/>
</manifest:manifest>
"""
STYLES_XML = """<?xml version="1.0" encoding="UTF-8"?>
<office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" office:version="1.3"/>
"""
META_XML = """<?xml version="1.0" encoding="UTF-8"?>
<office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" office:version="1.3"/>
"""
def build(target):
with zipfile.ZipFile(target, "w") as archive:
archive.writestr(
zipfile.ZipInfo("mimetype"),
"application/vnd.oasis.opendocument.text",
compress_type=zipfile.ZIP_STORED,
)
archive.writestr("META-INF/manifest.xml", MANIFEST_XML, zipfile.ZIP_DEFLATED)
archive.writestr("styles.xml", STYLES_XML, zipfile.ZIP_DEFLATED)
archive.writestr("meta.xml", META_XML, zipfile.ZIP_DEFLATED)
archive.writestr("content.xml", CONTENT_XML, zipfile.ZIP_DEFLATED)
if __name__ == "__main__":
build(HERE / "sample-recipe.odt")
print("wrote", HERE / "sample-recipe.odt")