home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 July / maximum-cd-2011-07.iso / DiscContents / LibO_3.3.2_Win_x86_install_multi.exe / libreoffice1.cab / officehelper.py < prev    next >
Encoding:
Python Source  |  2011-03-15  |  3.4 KB  |  94 lines

  1. ### *************************************************************************
  2. ### *
  3. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. # Copyright 2000, 2010 Oracle and/or its affiliates.
  5. #
  6. # OpenOffice.org - a multi-platform office productivity suite
  7. #
  8. # This file is part of OpenOffice.org.
  9. #
  10. # OpenOffice.org is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Lesser General Public License version 3
  12. # only, as published by the Free Software Foundation.
  13. #
  14. # OpenOffice.org is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU Lesser General Public License version 3 for more details
  18. # (a copy is included in the LICENSE file that accompanied this code).
  19. #
  20. # You should have received a copy of the GNU Lesser General Public License
  21. # version 3 along with OpenOffice.org.  If not, see
  22. # <http://www.openoffice.org/license.html>
  23. # for a copy of the LGPLv3 License.
  24. #
  25. ### ************************************************************************/
  26.  
  27. #
  28. # Translated to python from "Bootstrap.java" by Kim Kulak
  29. #
  30.  
  31. import os
  32. import random
  33. from sys import platform
  34. from time import sleep
  35.  
  36. import uno
  37. from com.sun.star.connection import NoConnectException
  38. from com.sun.star.uno import Exception as UnoException
  39.  
  40.  
  41. class BootstrapException(UnoException):
  42.     pass
  43.  
  44. def bootstrap():
  45.     """Bootstrap OOo and PyUNO Runtime.
  46.     The soffice process is started opening a named pipe of random name, then the local context is used
  47.     to access the pipe. This function directly returns the remote component context, from whereon you can
  48.     get the ServiceManager by calling getServiceManager() on the returned object.
  49.     """
  50.     try:
  51.     # soffice script used on *ix, Mac; soffice.exe used on Windoof
  52.         if "UNO_PATH" in os.environ:
  53.             sOffice = os.environ["UNO_PATH"]
  54.         else:
  55.             sOffice = "" # lets hope for the best
  56.         sOffice = os.path.join(sOffice, "soffice")
  57.         if platform.startswith("win"): 
  58.             sOffice += ".exe"
  59.         
  60.         # Generate a random pipe name.
  61.         random.seed()
  62.         sPipeName = "uno" + str(random.random())[2:]
  63.         
  64.         # Start the office proces, don't check for exit status since an exception is caught anyway if the office terminates unexpectedly.
  65.         cmdArray = (sOffice, "-nologo", "-nodefault", "".join(["-accept=pipe,name=", sPipeName, ";urp;"]))        
  66.         os.spawnv(os.P_NOWAIT, sOffice, cmdArray)
  67.             
  68.         # ---------
  69.  
  70.         xLocalContext = uno.getComponentContext()
  71.         resolver = xLocalContext.ServiceManager.createInstanceWithContext(
  72.             "com.sun.star.bridge.UnoUrlResolver", xLocalContext)
  73.         sConnect = "".join(["uno:pipe,name=", sPipeName, ";urp;StarOffice.ComponentContext"])
  74.  
  75.         # Wait until an office is started, but loop only nLoop times (can we do this better???)
  76.         nLoop = 20
  77.         while True:
  78.             try:
  79.                 xContext = resolver.resolve(sConnect)
  80.                 break
  81.             except NoConnectException:
  82.                 nLoop -= 1
  83.                 if nLoop <= 0:
  84.                     raise BootstrapException("Cannot connect to soffice server.", None)
  85.                 sleep(0.5)  # Sleep 1/2 second.
  86.  
  87.     except BootstrapException:
  88.         raise     
  89.     except Exception, e:  # Any other exception
  90.         raise BootstrapException("Caught exception " + str(e), None)
  91.  
  92.     return xContext
  93.