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 / pythonloader.py < prev    next >
Encoding:
Python Source  |  2009-01-09  |  6.2 KB  |  156 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: pythonloader.py,v $
  9. #
  10. # $Revision: 1.7 $
  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. import uno
  31. import unohelper
  32. import sys
  33. import imp
  34. import os
  35. from com.sun.star.uno import Exception,RuntimeException
  36. from com.sun.star.loader import XImplementationLoader
  37. from com.sun.star.lang import XServiceInfo
  38.  
  39. MODULE_PROTOCOL = "vnd.openoffice.pymodule:"
  40. DEBUG = 1
  41.  
  42. g_supportedServices  = "com.sun.star.loader.Python",      # referenced by the native C++ loader !
  43. g_implementationName = "org.openoffice.comp.pyuno.Loader" # referenced by the native C++ loader !
  44.  
  45. def splitUrl( url ):
  46.       nColon = url.find( ":" )
  47.       if -1 == nColon:
  48.             raise RuntimeException( "PythonLoader: No protocol in url " + url, None )
  49.       return url[0:nColon], url[nColon+1:len(url)]
  50.  
  51. g_loadedComponents = {}
  52. def checkForPythonPathBesideComponent( url ):
  53.       path = unohelper.fileUrlToSystemPath( url+"/pythonpath.zip" );
  54.       if DEBUG == 1:
  55.             print "checking for existence of " + encfile( path )
  56.       if 1 == os.access( encfile( path ), os.F_OK) and not path in sys.path:
  57.             if DEBUG == 1:
  58.                   print "adding " + encfile( path ) + " to sys.path"
  59.             sys.path.append( path )
  60.  
  61.       path = unohelper.fileUrlToSystemPath( url+"/pythonpath" );
  62.       if 1 == os.access( encfile( path ), os.F_OK) and not path in sys.path:
  63.             if DEBUG == 1:
  64.                   print "adding " + encfile( path ) + " to sys.path"
  65.             sys.path.append( path )
  66.  
  67. def encfile(uni):
  68.     return uni.encode( sys.getfilesystemencoding())
  69.  
  70. class Loader( XImplementationLoader, XServiceInfo, unohelper.Base ):
  71.       def __init__(self, ctx ):
  72.       if DEBUG:
  73.          print "pythonloader.Loader ctor" 
  74.       self.ctx = ctx
  75.  
  76.       def getModuleFromUrl( self, url ):
  77.           if DEBUG:
  78.                 print "pythonloader: interpreting url " +url
  79.           protocol, dependent = splitUrl( url )
  80.           if "vnd.sun.star.expand" == protocol:
  81.                 exp = self.ctx.getValueByName( "/singletons/com.sun.star.util.theMacroExpander" )
  82.                 url = exp.expandMacros(dependent)
  83.                 protocol,dependent = splitUrl( url )
  84.  
  85.           if DEBUG:
  86.                 print "pythonloader: after expansion " +protocol +":" + dependent
  87.                 
  88.           try:
  89.                 if "file" == protocol:
  90.                       # remove \..\ sequence, which may be useful e.g. in the build env
  91.                       url = unohelper.absolutize( url, url )
  92.  
  93.                       # did we load the module already ?
  94.                       mod = g_loadedComponents.get( url )
  95.                       if not mod:
  96.                             mod = imp.new_module("uno_component")
  97.  
  98.                             # check for pythonpath.zip beside .py files
  99.                             checkForPythonPathBesideComponent( url[0:url.rfind('/')] )
  100.                             
  101.                             # read the file
  102.                             filename = unohelper.fileUrlToSystemPath( url )
  103.                             fileHandle = file( filename )
  104.                             src = fileHandle.read().replace("\r","")
  105.                             if not src.endswith( "\n" ):
  106.                                   src = src + "\n"
  107.  
  108.                             # compile and execute the module
  109.                             codeobject = compile( src, encfile(filename), "exec" )
  110.                             exec codeobject in mod.__dict__
  111.                             mod.__file__ = encfile(filename)
  112.                             g_loadedComponents[url] = mod
  113.                       return mod
  114.                 elif "vnd.openoffice.pymodule" == protocol:
  115.                       return  __import__( dependent )
  116.                 else:
  117.                       raise RuntimeException( "PythonLoader: Unknown protocol " +
  118.                                               protocol + " in url " +url, self )
  119.           except ImportError, e:
  120.                 raise RuntimeException( "Couldn't load "+url+ " for reason "+str(e), None)
  121.           return None
  122.        
  123.       def activate( self, implementationName, dummy, locationUrl, regKey ):
  124.       if DEBUG:
  125.          print "pythonloader.Loader.activate"
  126.  
  127.       mod = self.getModuleFromUrl( locationUrl )
  128.           implHelper = mod.__dict__.get( "g_ImplementationHelper" , None )
  129.           if implHelper == None:
  130.         return mod.getComponentFactory( implementationName, self.ctx.ServiceManager, regKey )
  131.           else:
  132.         return implHelper.getComponentFactory( implementationName,regKey,self.ctx.ServiceManager)
  133.          
  134.       def writeRegistryInfo( self, regKey, dummy, locationUrl ):
  135.       if DEBUG:
  136.          print "pythonloader.Loader.writeRegistryInfo"
  137.              
  138.       mod = self.getModuleFromUrl( locationUrl )
  139.           implHelper = mod.__dict__.get( "g_ImplementationHelper" , None )
  140.           if implHelper == None:
  141.             return mod.writeRegistryInfo( self.ctx.ServiceManager, regKey )
  142.           else:
  143.             return implHelper.writeRegistryInfo( regKey, self.ctx.ServiceManager )
  144.  
  145.       def getImplementationName( self ):
  146.       return g_implementationName
  147.  
  148.       def supportsService( self, ServiceName ):
  149.       return ServiceName in self.serviceNames
  150.  
  151.       def getSupportedServiceNames( self ):
  152.       return g_supportedServices
  153.  
  154.  
  155.