"""Labeled static Word wrappers around existing explicit-space synthetic bodies."""
from pathlib import Path
from zipfile import ZipFile
from lxml import etree
import hashlib,json,shutil
root=Path('benchmarks/fixtures/word-static-editable1');assert not root.exists();root.mkdir()
source_root=Path('benchmarks/research/word-static-editable1/space-explicit-fixtures')
pin=lambda b:dict(bytes=len(b),sha256=hashlib.sha256(b).hexdigest())
docx=b'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml'
types={'docm':b'application/vnd.ms-word.document.macroEnabled.main+xml','dotx':b'application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml'}
rows=[]
for identity in ['letter','landscape','notes']:
    source=source_root/(identity+'.docx')
    for kind,content_type in types.items():
        target=root/(identity+'.'+kind)
        with ZipFile(source) as src,ZipFile(target,'w') as dst:
            for entry in src.infolist():
                data=src.read(entry.filename)
                if entry.filename=='[Content_Types].xml':assert data.count(docx)==1;data=data.replace(docx,content_type)
                dst.writestr(entry,data)
        rows.append(dict(file=target.name,fromFormat=kind,source=str(source),sourcePin=pin(source.read_bytes()),pin=pin(target.read_bytes()),variant='Explicit-space labeled derivative; only main package type changed',newOriginalBody=False))
source=root/'letter.docm';target=root/'letter-utf16.docm'
with ZipFile(source) as src,ZipFile(target,'w') as dst:
    for entry in src.infolist():
        data=src.read(entry.filename)
        if entry.filename=='word/document.xml':
            tree=etree.fromstring(data);data=etree.tostring(tree,encoding='UTF-16',xml_declaration=True,standalone=True)
        dst.writestr(entry,data)
rows.append(dict(file=target.name,fromFormat='docm',source=str(source),sourcePin=pin(source.read_bytes()),pin=pin(target.read_bytes()),variant='Labeled UTF16 document.xml encoding derivative',newOriginalBody=False))
(root/'manifest.json').write_text(json.dumps(dict(rows=rows,newOriginalBodyCorpora=0,scope='Reused original synthetic bodies with explicit significant-space and package/encoding derivatives; originals and earlier failed expectations remain unchanged.'),ensure_ascii=False,indent=2)+'\n')
shutil.copyfile('benchmarks/fixtures/word-package1/LICENSE.txt',root/'LICENSE.txt')
shutil.copyfile('benchmarks/fixtures/word-notes1/python-docx-MIT-LICENSE.txt',root/'python-docx-MIT-LICENSE.txt')
print(dict(files=len(rows),newBodyCorpora=0))
