home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / OOo_3.0.1_Win32Intel_install_wJRE_en-US.exe / openofficeorg1.cab / officehelper.py < prev    next >
Encoding:
Python Source  |  2009-01-09  |  3.3 KB  |  94 lines

  1. ### *************************************************************************
  2. ### *
  3. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. # Copyright 2008 by Sun Microsystems, Inc.
  5. #
  6. # OpenOffice.org - a multi-platform office productivity suite
  7. #
  8. # $RCSfile: officehelper.py,v $
  9. #
  10. # $Revision: 1.3 $
  11. #
  12. # This file is part of OpenOffice.org.
  13. #
  14. # OpenOffice.org is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU Lesser General Public License version 3
  16. # only, as published by the Free Software Foundation.
  17. #
  18. # OpenOffice.org is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. # GNU Lesser General Public License version 3 for more details
  22. # (a copy is included in the LICENSE file that accompanied this code).
  23. #
  24. # You should have received a copy of the GNU Lesser General Public License
  25. # version 3 along with OpenOffice.org.  If not, see
  26. # <http://www.openoffice.org/license.html>
  27. # for a copy of the LGPLv3 License.
  28. #
  29. ### ************************************************************************/
  30.  
  31. #
  32. # Translated to python from "Bootstrap.java" by Kim Kulak
  33. #
  34.  
  35. import os
  36. import random
  37. from sys import platform
  38. from time import sleep
  39.  
  40. import uno
  41. from com.sun.star.connection import NoConnectException
  42. from com.sun.star.uno import Exception as UnoException
  43.  
  44.  
  45. class BootstrapException(UnoException):
  46.     pass
  47.  
  48. def bootstrap():
  49.     """Bootstrap OOo and PyUNO Runtime.
  50.     The soffice process is started opening a named pipe of random name, then the local context is used
  51.     to access the pipe. This function directly returns the remote component context, from whereon you can
  52.     get the ServiceManager by calling getServiceManager() on the returned object.
  53.     """
  54.     try:
  55.     # soffice script used on *ix, Mac; soffice.exe used on Windoof
  56.         sOffice = os.path.join(os.path.dirname(__file__), "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.