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 22import time, uno 23from com.sun.star.beans import PropertyValue 24 25FILTERS = {"odt": "writer8", "ods": "calc8", "odp": "impress8", "odg": "draw8"} 26 27 28def pv(n, v): 29 p = PropertyValue(); p.Name = n; p.Value = v; return p 30 31 32def connect(port, tries=60): 33 local = uno.getComponentContext() 34 res = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local) 35 for _ in range(tries): 36 try: 37 return res.resolve("uno:socket,host=localhost,port=%d;urp;StarOffice.ComponentContext" % port) 38 except Exception: 39 time.sleep(1) 40 raise SystemExit("no connection on port %d" % port) 41 42 43def odf_version(ctx): 44 cp = ctx.ServiceManager.createInstanceWithContext("com.sun.star.configuration.ConfigurationProvider", ctx) 45 ca = cp.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", 46 (pv("nodepath", "/org.openoffice.Office.Common/Save/ODF"),)) 47 return ca.getByName("DefaultVersion") 48 49 50def desktop(ctx): 51 return ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) 52 53 54def load(desk, path, readonly=True): 55 doc = desk.loadComponentFromURL(uno.systemPathToFileUrl(path), "_blank", 0, 56 (pv("Hidden", True), pv("MacroExecutionMode", 0), pv("ReadOnly", readonly), pv("UpdateDocMode", 0))) 57 if doc is None: 58 raise RuntimeError("load returned None") 59 return doc 60