2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 # Translated to python from "Bootstrap.java" by Kim Kulak
25 from sys
import platform
26 from time
import sleep
29 from com
.sun
.star
.connection
import NoConnectException
30 from com
.sun
.star
.uno
import Exception as UnoException
33 class BootstrapException(UnoException
):
37 """Bootstrap OOo and PyUNO Runtime.
38 The soffice process is started opening a named pipe of random name, then the local context is used
39 to access the pipe. This function directly returns the remote component context, from whereon you can
40 get the ServiceManager by calling getServiceManager() on the returned object.
43 # soffice script used on *ix, Mac; soffice.exe used on Win
44 if "UNO_PATH" in os
.environ
:
45 sOffice
= os
.environ
["UNO_PATH"]
47 sOffice
= "" # lets hope for the best
48 sOffice
= os
.path
.join(sOffice
, "soffice")
49 if platform
.startswith("win"):
52 # Generate a random pipe name.
54 sPipeName
= "uno" + str(random
.random())[2:]
56 # Start the office process, don't check for exit status since an exception is caught anyway if the office terminates unexpectedly.
57 cmdArray
= (sOffice
, "--nologo", "--nodefault", "".join(["--accept=pipe,name=", sPipeName
, ";urp;"]))
58 os
.spawnv(os
.P_NOWAIT
, sOffice
, cmdArray
)
62 xLocalContext
= uno
.getComponentContext()
63 resolver
= xLocalContext
.ServiceManager
.createInstanceWithContext(
64 "com.sun.star.bridge.UnoUrlResolver", xLocalContext
)
65 sConnect
= "".join(["uno:pipe,name=", sPipeName
, ";urp;StarOffice.ComponentContext"])
67 # Wait until an office is started, but loop only nLoop times (can we do this better???)
71 xContext
= resolver
.resolve(sConnect
)
73 except NoConnectException
:
76 raise BootstrapException("Cannot connect to soffice server.", None)
77 sleep(0.5) # Sleep 1/2 second.
79 except BootstrapException
:
81 except Exception as e
: # Any other exception
82 raise BootstrapException("Caught exception " + str(e
), None)