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