Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / pyuno / source / officehelper.py
blob0b0bb68af5e582c302de347ccb78da4068163178
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
23 import os
24 import random
25 from sys import platform
26 from time import sleep
28 import uno
29 from com.sun.star.connection import NoConnectException
30 from com.sun.star.uno import Exception as UnoException
33 class BootstrapException(UnoException):
34 pass
36 def bootstrap():
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.
41 """
42 try:
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"]
46 else:
47 sOffice = "" # lets hope for the best
48 sOffice = os.path.join(sOffice, "soffice")
49 if platform.startswith("win"):
50 sOffice += ".exe"
52 # Generate a random pipe name.
53 random.seed()
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)
60 # ---------
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???)
68 nLoop = 20
69 while True:
70 try:
71 xContext = resolver.resolve(sConnect)
72 break
73 except NoConnectException:
74 nLoop -= 1
75 if nLoop <= 0:
76 raise BootstrapException("Cannot connect to soffice server.", None)
77 sleep(0.5) # Sleep 1/2 second.
79 except BootstrapException:
80 raise
81 except Exception as e: # Any other exception
82 raise BootstrapException("Caught exception " + str(e), None)
84 return xContext