xref: /trunk/test/odf-validate/fixtures/doctypes/make.py (revision 0e16a31d78a652c56c5c0fc79c752f48b9048df9)
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# usage: runpy.sh fixtures/doctypes/make.py <port> <outdir>
21# One document of each ODF type the corpus lacks (chart-host.ods feeds derive-chart.py). Saves with the office's configured ODF version: run it against the strict and extended profiles.
22import sys, os, uno
23from unohelp import connect, desktop, pv
24
25port, out = int(sys.argv[1]), sys.argv[2]
26os.makedirs(out, exist_ok=True)
27desk = desktop(connect(port))
28failed = []
29
30
31def new(factory):
32    return desk.loadComponentFromURL(factory, "_blank", 0, (pv("Hidden", True),))
33
34
35def save(doc, name, filt):
36    try:
37        doc.storeToURL(uno.systemPathToFileUrl(os.path.join(out, name)), (pv("FilterName", filt),))
38        print("saved", name, filt)
39    except Exception as e:
40        failed.append(name)
41        print("FAIL", name, filt, repr(e)[:200])
42
43
44def writer_body(doc, text):
45    c = doc.Text.createTextCursor()
46    doc.Text.insertString(c, text, False)
47
48
49def calc_body(doc):
50    sh = doc.Sheets.getByIndex(0)
51    for i in range(4):
52        sh.getCellByPosition(0, i).Value = i + 1
53        sh.getCellByPosition(1, i).Formula = "=A%d*2" % (i + 1)
54    return sh
55
56
57def draw_body(doc):
58    page = doc.DrawPages.getByIndex(0)
59    shape = doc.createInstance("com.sun.star.drawing.RectangleShape")
60    page.add(shape)
61    shape.Size = uno.createUnoStruct("com.sun.star.awt.Size", 4000, 2000)
62    shape.String = "box"
63
64
65doc = new("private:factory/swriter")
66writer_body(doc, "writer template")
67save(doc, "template-writer.ott", "writer8_template")
68doc.close(True)
69
70doc = new("private:factory/scalc")
71calc_body(doc)
72save(doc, "template-calc.ots", "calc8_template")
73doc.close(True)
74
75doc = new("private:factory/simpress")
76draw_body(doc)
77save(doc, "template-impress.otp", "impress8_template")
78save(doc, "impress-as-draw.odg", "impress8_draw")
79doc.close(True)
80
81doc = new("private:factory/sdraw")
82draw_body(doc)
83save(doc, "template-draw.otg", "draw8_template")
84doc.close(True)
85
86doc = new("private:factory/swriter/GlobalDocument")
87writer_body(doc, "master document")
88save(doc, "master.odm", "writerglobal8")
89save(doc, "master-as-text.odt", "writerglobal8_writer")
90doc.close(True)
91
92doc = new("private:factory/swriter/web")
93writer_body(doc, "web document")
94save(doc, "web.odt", "writerweb8_writer")
95save(doc, "web-template.ott", "writerweb8_writer_template")
96doc.close(True)
97
98doc = new("private:factory/smath")
99doc.Formula = "a over b + sqrt {x ^ 2}"
100save(doc, "math.odf", "math8")
101doc.close(True)
102
103doc = new("private:factory/scalc")
104sh = calc_body(doc)
105rng = uno.createUnoStruct("com.sun.star.table.CellRangeAddress", 0, 0, 0, 1, 3)
106sh.Charts.addNewByName("c1", uno.createUnoStruct("com.sun.star.awt.Rectangle", 1000, 1000, 8000, 6000), (rng,), False, False)
107# storeToURL/storeAsURL of a chart model crashes the office, so chart.odc is cut out of this package by derive-chart.py
108save(doc, "chart-host.ods", "calc8")
109doc.close(True)
110
111sys.exit(1 if failed else 0)
112