1################################################################ 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20################################################################ 21 22"""Which ODF version a saved package declares.""" 23import re 24import zipfile 25 26# Save/ODF/DefaultVersion -> the office:version written (3 and 4 are ODF 1.2, 5 and 6 are 1.3, 7 and 8 are 1.4) 27MODES = {3: "1.2", 4: "1.2", 5: "1.3", 6: "1.3", 7: "1.4", 8: "1.4"} 28 29 30# mode name -> Save/ODF/DefaultVersion (None leaves the product default) 31SAVE_MODES = {"strict": 4, "ext": 3, "v13": 5, "x13": 6, "v14": 7, "x14": 8, "default": None} 32 33 34def odf_versions(path): 35 """(office:version of the root element of content.xml, manifest:version of META-INF/manifest.xml).""" 36 with zipfile.ZipFile(path) as z: 37 content = z.read("content.xml").decode("utf-8") 38 manifest = z.read("META-INF/manifest.xml").decode("utf-8") 39 c = re.search(r'<office:document-content[^>]*\boffice:version="([^"]*)"', content) 40 m = re.search(r'<manifest:manifest[^>]*\bmanifest:version="([^"]*)"', manifest) 41 return (c.group(1) if c else None), (m.group(1) if m else None) 42 43 44def is_formula(path): 45 """True for a formula (Math) package: its content.xml is a MathML root that carries no office:version.""" 46 with zipfile.ZipFile(path) as z: 47 return z.read("mimetype").decode("ascii").strip() == "application/vnd.oasis.opendocument.formula" 48