home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / CHIP_CD_2004-12.iso / bonus / oo / OOo_1.1.3_ru_RU_infra_WinIntel_install.exe / $PLUGINSDIR / f_0371 / pythonloader.py
Text File  |  2004-10-09  |  6KB  |  159 lines

  1. #*************************************************************************
  2. #
  3. #   $RCSfile: pythonloader.py,v $
  4. #
  5. #   $Revision: 1.2.12.1 $
  6. #
  7. #   last change: $Author: vg $ $Date: 2004/01/28 12:16:10 $
  8. #
  9. #   The Contents of this file are made available subject to the terms of
  10. #   either of the following licenses
  11. #
  12. #          - GNU Lesser General Public License Version 2.1
  13. #          - Sun Industry Standards Source License Version 1.1
  14. #
  15. #   Sun Microsystems Inc., October, 2000
  16. #
  17. #   GNU Lesser General Public License Version 2.1
  18. #   =============================================
  19. #   Copyright 2000 by Sun Microsystems, Inc.
  20. #   901 San Antonio Road, Palo Alto, CA 94303, USA
  21. #
  22. #   This library is free software; you can redistribute it and/or
  23. #   modify it under the terms of the GNU Lesser General Public
  24. #   License version 2.1, as published by the Free Software Foundation.
  25. #
  26. #   This library is distributed in the hope that it will be useful,
  27. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  29. #   Lesser General Public License for more details.
  30. #
  31. #   You should have received a copy of the GNU Lesser General Public
  32. #   License along with this library; if not, write to the Free Software
  33. #   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  34. #   MA  02111-1307  USA
  35. #
  36. #
  37. #   Sun Industry Standards Source License Version 1.1
  38. #   =================================================
  39. #   The contents of this file are subject to the Sun Industry Standards
  40. #   Source License Version 1.1 (the "License"); You may not use this file
  41. #   except in compliance with the License. You may obtain a copy of the
  42. #   License at http://www.openoffice.org/license.html.
  43. #
  44. #   Software provided under this License is provided on an "AS IS" basis,
  45. #   WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  46. #   WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
  47. #   MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
  48. #   See the License for the specific provisions governing your rights and
  49. #   obligations concerning the Software.
  50. #
  51. #   The Initial Developer of the Original Code is: Ralph Thomas
  52. #
  53. #   Copyright: 2000 by Sun Microsystems, Inc.
  54. #
  55. #   All Rights Reserved.
  56. #
  57. #   Contributor(s): Ralph Thomas, Joerg Budischewski
  58. #
  59. #*************************************************************************
  60. import uno
  61. import unohelper
  62. import imp
  63. import os
  64. from com.sun.star.uno import Exception,RuntimeException
  65. from com.sun.star.loader import XImplementationLoader
  66. from com.sun.star.lang import XServiceInfo
  67.  
  68. MODULE_PROTOCOL = "vnd.openoffice.pymodule:"
  69. DEBUG = 0
  70.  
  71. g_supportedServices  = "com.sun.star.loader.Python",      # referenced by the native C++ loader !
  72. g_implementationName = "org.openoffice.comp.pyuno.Loader" # referenced by the native C++ loader !
  73.  
  74. def splitUrl( url ):
  75.       nColon = url.find( ":" )
  76.       if -1 == nColon:
  77.             raise RuntimeException( "PythonLoader: No protocol in url " + url )
  78.       return url[0:nColon], url[nColon+1:len(url)]
  79.  
  80. g_loadedComponents = {}
  81.  
  82. class Loader( XImplementationLoader, XServiceInfo, unohelper.Base ):
  83.       def __init__(self, ctx ):
  84.       if DEBUG:
  85.          print "pythonloader.Loader ctor" 
  86.       self.ctx = ctx
  87.  
  88.       def getModuleFromUrl( self, url ):
  89.           if DEBUG:
  90.                 print "pythonloader: interpreting url " +url
  91.           protocol, dependent = splitUrl( url )
  92.           if "vnd.sun.star.expand" == protocol:
  93.                 exp = self.ctx.getValueByName( "/singletons/com.sun.star.util.theMacroExpander" )
  94.                 url = exp.expandMacros(dependent)
  95.                 protocol,dependent = splitUrl( url )
  96.  
  97.           if DEBUG:
  98.                 print "pythonloader: after expansion " +protocol +":" + dependent
  99.                 
  100.           try:
  101.                 if "file" == protocol:
  102.                       # remove \..\ sequence, which may be useful e.g. in the build env
  103.                       url = unohelper.absolutize( url, url )
  104.  
  105.                       # did we load the module already ?
  106.                       mod = g_loadedComponents.get( url )
  107.                       if not mod:
  108.                             mod = imp.new_module("uno_component")
  109.  
  110.                             # read the file
  111.                             fileHandle = file( unohelper.fileUrlToSystemPath( url ) )
  112.                             src = fileHandle.read()
  113.  
  114.                             # execute the module
  115.                             exec src in mod.__dict__
  116.                             g_loadedComponents[url] = mod
  117.                       return mod
  118.                 elif "vnd.openoffice.pymodule" == protocol:
  119.                       return  __import__( dependent )
  120.                 else:
  121.                       raise RuntimeException( "PythonLoader: Unknown protocol " +
  122.                                               protocol + " in url " +url, self )
  123.           except ImportError, e:
  124.                 raise RuntimeException( "Couldn't load "+url+ " for reason "+str(e), None)
  125.           return None
  126.        
  127.       def activate( self, implementationName, dummy, locationUrl, regKey ):
  128.       if DEBUG:
  129.          print "pythonloader.Loader.activate"
  130.  
  131.       mod = self.getModuleFromUrl( locationUrl )
  132.           implHelper = mod.__dict__.get( "g_ImplementationHelper" , None )
  133.           if implHelper == None:
  134.         return mod.getComponentFactory( implementationName, self.ctx.ServiceManager, regKey )
  135.           else:
  136.         return implHelper.getComponentFactory( implementationName,regKey,self.ctx.ServiceManager)
  137.          
  138.       def writeRegistryInfo( self, regKey, dummy, locationUrl ):
  139.       if DEBUG:
  140.          print "pythonloader.Loader.writeRegistryInfo"
  141.              
  142.       mod = self.getModuleFromUrl( locationUrl )
  143.           implHelper = mod.__dict__.get( "g_ImplementationHelper" , None )
  144.           if implHelper == None:
  145.             return mod.writeRegistryInfo( self.ctx.ServiceManager, regKey )
  146.           else:
  147.             return implHelper.writeRegistryInfo( regKey, self.ctx.ServiceManager )
  148.  
  149.       def getImplementationName( self ):
  150.       return g_implementationName
  151.  
  152.       def supportsService( self, ServiceName ):
  153.       return ServiceName in self.serviceNames
  154.  
  155.       def getSupportedServiceNames( self ):
  156.       return g_supportedServices
  157.  
  158.  
  159.