LibreOffice Module pyuno (master) 1
officehelper.py
Go to the documentation of this file.
1# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
2#
3# This file is part of the LibreOffice project.
4#
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/.
8#
9# This file incorporates work covered by the following license notice:
10#
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 .
18#
19
20#
21# Translated to python from "Bootstrap.java" by Kim Kulak
22#
23
24import os
25import random
26from sys import platform
27from time import sleep
28
29import uno
30from com.sun.star.connection import NoConnectException
31from com.sun.star.uno import Exception as UnoException
32
33
34class BootstrapException(UnoException):
35 pass
36
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"
52
53 # Generate a random pipe name.
54 random.seed()
55 sPipeName = "uno" + str(random.random())[2:]
56
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)
60
61 # ---------
62
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"])
67
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.
79
80 except BootstrapException:
81 raise
82 except Exception as e: # Any other exception
83 raise BootstrapException("Caught exception " + str(e), None)
84
85 return xContext
86
87# vim: set shiftwidth=4 softtabstop=4 expandtab:
def bootstrap()
Definition: officehelper.py:37
def getComponentContext()
Definition: uno.py:32