1 ### *************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 # Copyright 2008 by Sun Microsystems, Inc.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # $RCSfile: officehelper.py,v $
13 # This file is part of OpenOffice.org.
15 # OpenOffice.org is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU Lesser General Public License version 3
17 # only, as published by the Free Software Foundation.
19 # OpenOffice.org is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU Lesser General Public License version 3 for more details
23 # (a copy is included in the LICENSE file that accompanied this code).
25 # You should have received a copy of the GNU Lesser General Public License
26 # version 3 along with OpenOffice.org. If not, see
27 # <http://www.openoffice.org/license.html>
28 # for a copy of the LGPLv3 License.
30 ### ************************************************************************/
33 # Translated to python from "Bootstrap.java" by Kim Kulak
38 from sys
import platform
39 from time
import sleep
42 from com
.sun
.star
.connection
import NoConnectException
43 from com
.sun
.star
.uno
import Exception as UnoException
46 class BootstrapException(UnoException
):
50 """Bootstrap OOo and PyUNO Runtime.
51 The soffice process is started opening a named pipe of random name, then the local context is used
52 to access the pipe. This function directly returns the remote component context, from whereon you can
53 get the ServiceManager by calling getServiceManager() on the returned object.
56 # soffice script used on *ix, Mac; soffice.exe used on Windoof
57 if "UNO_PATH" in os
.environ
:
58 sOffice
= os
.environ
["UNO_PATH"]
60 sOffice
= "${exec_prefix}/lib/ooo-3.2/program"
61 sOffice
= os
.path
.join(sOffice
, "soffice")
62 if platform
.startswith("win"):
65 # Generate a random pipe name.
67 sPipeName
= "uno" + str(random
.random())[2:]
69 # Start the office proces, don't check for exit status since an exception is caught anyway if the office terminates unexpectedly.
70 cmdArray
= (sOffice
, "-nologo", "-nodefault", "".join(["-accept=pipe,name=", sPipeName
, ";urp;"]))
71 os
.spawnv(os
.P_NOWAIT
, sOffice
, cmdArray
)
75 xLocalContext
= uno
.getComponentContext()
76 resolver
= xLocalContext
.ServiceManager
.createInstanceWithContext(
77 "com.sun.star.bridge.UnoUrlResolver", xLocalContext
)
78 sConnect
= "".join(["uno:pipe,name=", sPipeName
, ";urp;StarOffice.ComponentContext"])
80 # Wait until an office is started, but loop only nLoop times (can we do this better???)
84 xContext
= resolver
.resolve(sConnect
)
86 except NoConnectException
:
89 raise BootstrapException("Cannot connect to soffice server.", None)
90 sleep(0.5) # Sleep 1/2 second.
92 except BootstrapException
:
94 except Exception, e
: # Any other exception
95 raise BootstrapException("Caught exception " + str(e
), None)