officehelper.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. # Translated to python from "Bootstrap.java" by Kim Kulak
  21. #
  22. import os
  23. import random
  24. from sys import platform
  25. from time import sleep
  26. import uno
  27. from com.sun.star.connection import NoConnectException
  28. from com.sun.star.uno import Exception as UnoException
  29. class BootstrapException(UnoException):
  30. pass
  31. def bootstrap():
  32. """Bootstrap OOo and PyUNO Runtime.
  33. The soffice process is started opening a named pipe of random name, then the local context is used
  34. to access the pipe. This function directly returns the remote component context, from whereon you can
  35. get the ServiceManager by calling getServiceManager() on the returned object.
  36. """
  37. try:
  38. # soffice script used on *ix, Mac; soffice.exe used on Win
  39. if "UNO_PATH" in os.environ:
  40. sOffice = os.environ["UNO_PATH"]
  41. else:
  42. sOffice = "" # lets hope for the best
  43. sOffice = os.path.join(sOffice, "soffice")
  44. if platform.startswith("win"):
  45. sOffice += ".exe"
  46. # Generate a random pipe name.
  47. random.seed()
  48. sPipeName = "uno" + str(random.random())[2:]
  49. # Start the office process, don't check for exit status since an exception is caught anyway if the office terminates unexpectedly.
  50. cmdArray = (sOffice, "--nologo", "--nodefault", "".join(["--accept=pipe,name=", sPipeName, ";urp;"]))
  51. os.spawnv(os.P_NOWAIT, sOffice, cmdArray)
  52. # ---------
  53. xLocalContext = uno.getComponentContext()
  54. resolver = xLocalContext.ServiceManager.createInstanceWithContext(
  55. "com.sun.star.bridge.UnoUrlResolver", xLocalContext)
  56. sConnect = "".join(["uno:pipe,name=", sPipeName, ";urp;StarOffice.ComponentContext"])
  57. # Wait until an office is started, but loop only nLoop times (can we do this better???)
  58. nLoop = 20
  59. while True:
  60. try:
  61. xContext = resolver.resolve(sConnect)
  62. break
  63. except NoConnectException:
  64. nLoop -= 1
  65. if nLoop <= 0:
  66. raise BootstrapException("Cannot connect to soffice server.", None)
  67. sleep(0.5) # Sleep 1/2 second.
  68. except BootstrapException:
  69. raise
  70. except Exception as e: # Any other exception
  71. raise BootstrapException("Caught exception " + str(e), None)
  72. return xContext
  73. # vim: set shiftwidth=4 softtabstop=4 expandtab: