home *** CD-ROM | disk | FTP | other *** search
/ Freelog 52 / Freelog052.iso / Dossier / OpenOffice / f_0396 / pythonloader.py
Text File  |  2003-07-18  |  6KB  |  157 lines

  1. #*************************************************************************
  2. #
  3. #   $RCSfile: pythonloader.py,v $
  4. #
  5. #   $Revision: 1.2 $
  6. #
  7. #   last change: $Author: jbu $ $Date: 2003/04/06 17:16:47 $
  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.                       mod = g_loadedComponents.get( url )
  103.                       if not mod:
  104.                             mod = imp.new_module("uno_component")
  105.                             # remove \..\ sequence, which may be useful e.g. in the build env
  106.                             url = unohelper.absolutize( url, url )
  107.  
  108.                             # read the file
  109.                             fileHandle = file( unohelper.fileUrlToSystemPath( url ) )
  110.                             src = fileHandle.read()
  111.  
  112.                             # execute the module
  113.                             exec src in mod.__dict__
  114.                             g_loadedComponents[url] = mod
  115.                       return mod
  116.                 elif "vnd.openoffice.pymodule" == protocol:
  117.                       return  __import__( dependent )
  118.                 else:
  119.                       raise RuntimeException( "PythonLoader: Unknown protocol " +
  120.                                               protocol + " in url " +url, self )
  121.           except ImportError, e:
  122.                 raise RuntimeException( "Couldn't load "+url+ " for reason "+str(e), None)
  123.           return None
  124.        
  125.       def activate( self, implementationName, dummy, locationUrl, regKey ):
  126.       if DEBUG:
  127.          print "pythonloader.Loader.activate"
  128.  
  129.       mod = self.getModuleFromUrl( locationUrl )
  130.           implHelper = mod.__dict__.get( "g_ImplementationHelper" , None )
  131.           if implHelper == None:
  132.         return mod.getComponentFactory( implementationName, self.ctx.ServiceManager, regKey )
  133.           else:
  134.         return implHelper.getComponentFactory( implementationName,regKey,self.ctx.ServiceManager)
  135.          
  136.       def writeRegistryInfo( self, regKey, dummy, locationUrl ):
  137.       if DEBUG:
  138.          print "pythonloader.Loader.writeRegistryInfo"
  139.              
  140.       mod = self.getModuleFromUrl( locationUrl )
  141.           implHelper = mod.__dict__.get( "g_ImplementationHelper" , None )
  142.           if implHelper == None:
  143.             return mod.writeRegistryInfo( self.ctx.ServiceManager, regKey )
  144.           else:
  145.             return implHelper.writeRegistryInfo( regKey, self.ctx.ServiceManager )
  146.  
  147.       def getImplementationName( self ):
  148.       return g_implementationName
  149.  
  150.       def supportsService( self, ServiceName ):
  151.       return ServiceName in self.serviceNames
  152.  
  153.       def getSupportedServiceNames( self ):
  154.       return g_supportedServices
  155.  
  156.  
  157.