python3: upgrade to release 3.8.13
[LibreOffice.git] / pyuno / source / officehelper.py
blob53bd5943e3c0d6b4a9f570a70fdac9c8c24b1c0b
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 # This file incorporates work covered by the following license notice:
11 # Licensed to the Apache Software Foundation (ASF) under one or more
12 # contributor license agreements. See the NOTICE file distributed
13 # with this work for additional information regarding copyright
14 # ownership. The ASF licenses this file to you under the Apache
15 # License, Version 2.0 (the "License"); you may not use this file
16 # except in compliance with the License. You may obtain a copy of
17 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 # Translated to python from "Bootstrap.java" by Kim Kulak
24 import os
25 import random
26 from sys import platform
27 from time import sleep
29 import uno
30 from com.sun.star.connection import NoConnectException
31 from com.sun.star.uno import Exception as UnoException
34 class BootstrapException(UnoException):
35 pass
37 def bootstrap():
38 """Bootstrap OOo and PyUNO Runtime.
39 The soffice process is started opening a named pipe of random name, then the local context is used
40 to access the pipe. This function directly returns the remote component context, from whereon you can
41 get the ServiceManager by calling getServiceManager() on the returned object.
42 """
43 try:
44 # soffice script used on *ix, Mac; soffice.exe used on Win
45 if "UNO_PATH" in os.environ:
46 sOffice = os.environ["UNO_PATH"]
47 else:
48 sOffice = "" # lets hope for the best
49 sOffice = os.path.join(sOffice, "soffice")
50 if platform.startswith("win"):
51 sOffice += ".exe"
53 # Generate a random pipe name.
54 random.seed()
55 sPipeName = "uno" + str(random.random())[2:]
57 # Start the office process, don't check for exit status since an exception is caught anyway if the office terminates unexpectedly.
58 cmdArray = (sOffice, "--nologo", "--nodefault", "".join(["--accept=pipe,name=", sPipeName, ";urp;"]))
59 os.spawnv(os.P_NOWAIT, sOffice, cmdArray)
61 # ---------
63 xLocalContext = uno.getComponentContext()
64 resolver = xLocalContext.ServiceManager.createInstanceWithContext(
65 "com.sun.star.bridge.UnoUrlResolver", xLocalContext)
66 sConnect = "".join(["uno:pipe,name=", sPipeName, ";urp;StarOffice.ComponentContext"])
68 # Wait until an office is started, but loop only nLoop times (can we do this better???)
69 nLoop = 20
70 while True:
71 try:
72 xContext = resolver.resolve(sConnect)
73 break
74 except NoConnectException:
75 nLoop -= 1
76 if nLoop <= 0:
77 raise BootstrapException("Cannot connect to soffice server.", None)
78 sleep(0.5) # Sleep 1/2 second.
80 except BootstrapException:
81 raise
82 except Exception as e: # Any other exception
83 raise BootstrapException("Caught exception " + str(e), None)
85 return xContext
87 # vim: set shiftwidth=4 softtabstop=4 expandtab: