home *** CD-ROM | disk | FTP | other *** search
/ Freelog 52 / Freelog052.iso / Dossier / OpenOffice / f_0394 / unohelper.py
Text File  |  2003-07-18  |  11KB  |  306 lines

  1. #*************************************************************************
  2. #
  3. #   $RCSfile: unohelper.py,v $
  4. #
  5. #   $Revision: 1.2 $
  6. #
  7. #   last change: $Author: jbu $ $Date: 2003/05/24 23:23:38 $
  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 pyuno
  62. import os
  63.  
  64. from com.sun.star.lang import XTypeProvider, XSingleComponentFactory, XServiceInfo
  65. from com.sun.star.uno import RuntimeException
  66. from com.sun.star.beans.MethodConcept import ALL as METHOD_CONCEPT_ALL
  67. from com.sun.star.beans.PropertyConcept import ALL as PROPERTY_CONCEPT_ALL
  68.  
  69. from com.sun.star.reflection.ParamMode import \
  70.      IN as PARAM_MODE_IN, \
  71.      OUT as PARAM_MODE_OUT, \
  72.      INOUT as PARAM_MODE_INOUT
  73.  
  74. from com.sun.star.beans.PropertyAttribute import \
  75.      MAYBEVOID as PROP_ATTR_MAYBEVOID, \
  76.      BOUND as PROP_ATTR_BOUND, \
  77.      CONSTRAINED as PROP_ATTR_CONSTRAINED, \
  78.      TRANSIENT as PROP_ATTR_TRANSIENT, \
  79.      READONLY as PROP_ATTR_READONLY, \
  80.      MAYBEAMBIGUOUS as PROP_ATTR_MAYBEAMBIGUOUS, \
  81.      MAYBEDEFAULT as PROP_ATTR_MAYBEDEFAULT, \
  82.      REMOVEABLE as PROP_ATTR_REMOVEABLE
  83.  
  84. def _mode_to_str( mode ):
  85.     ret = "[]"
  86.     if mode == PARAM_MODE_INOUT:
  87.         ret = "[inout]"
  88.     elif mode == PARAM_MODE_OUT:
  89.         ret = "[out]"
  90.     elif mode == PARAM_MODE_IN:
  91.         ret = "[in]"
  92.     return ret
  93.  
  94. def _propertymode_to_str( mode ):
  95.     ret = ""
  96.     if PROP_ATTR_REMOVEABLE & mode:
  97.         ret = ret + "removeable "
  98.     if PROP_ATTR_MAYBEDEFAULT & mode:
  99.         ret = ret + "maybedefault "
  100.     if PROP_ATTR_MAYBEAMBIGUOUS & mode:
  101.         ret = ret + "maybeambigous "
  102.     if PROP_ATTR_READONLY & mode:
  103.         ret = ret + "readonly "
  104.     if PROP_ATTR_TRANSIENT & mode:
  105.         ret = ret + "tranient "
  106.     if PROP_ATTR_CONSTRAINED & mode:
  107.         ret = ret + "constrained "
  108.     if PROP_ATTR_BOUND & mode:
  109.         ret = ret + "bound "
  110.     if PROP_ATTR_MAYBEVOID & mode:
  111.         ret = ret + "maybevoid "
  112.     return ret.rstrip()
  113.     
  114. def inspect( obj , out ):
  115.     ctx = uno.getComponentContext()
  116.     introspection = \
  117.          ctx.ServiceManager.createInstanceWithContext( "com.sun.star.beans.Introspection", ctx )
  118.  
  119.     out.write( "Supported services:\n" )
  120.     if hasattr( obj, "getSupportedServiceNames" ):
  121.         names = obj.getSupportedServiceNames()
  122.         for ii in names:
  123.             out.write( "  " + ii + "\n" )
  124.     else:
  125.         out.write( "  unknown\n" )
  126.  
  127.     out.write( "Interfaces:\n" )
  128.     if hasattr( obj, "getTypes" ):
  129.         interfaces = obj.getTypes()
  130.         for ii in interfaces:
  131.             out.write( "  " + ii.typeName + "\n" )
  132.     else:
  133.         out.write( "  unknown\n" )
  134.         
  135.     access = introspection.inspect( obj )
  136.     methods = access.getMethods( METHOD_CONCEPT_ALL )
  137.     out.write( "Methods:\n" )
  138.     for ii in methods:
  139.         out.write( "  " + ii.ReturnType.Name + " " + ii.Name )
  140.         args = ii.ParameterTypes
  141.         infos = ii.ParameterInfos
  142.         out.write( "( " )
  143.         for i in range( 0, len( args ) ):
  144.             if i > 0:
  145.                 out.write( ", " )
  146.             out.write( _mode_to_str( infos[i].aMode ) + " " + args[i].Name + " " + infos[i].aName )
  147.         out.write( " )\n" )
  148.  
  149.     props = access.getProperties( PROPERTY_CONCEPT_ALL )
  150.     out.write ("Properties:\n" )
  151.     for ii in props:
  152.         out.write( "  ("+_propertymode_to_str( ii.Attributes ) + ") "+ii.Type.typeName+" "+ii.Name+ "\n" )
  153.  
  154. def createSingleServiceFactory( clazz, implementationName, serviceNames ):
  155.     return _FactoryHelper_( clazz, implementationName, serviceNames )
  156.  
  157. class _ImplementationHelperEntry:
  158.       def __init__(self, ctor,serviceNames):
  159.       self.ctor = ctor
  160.       self.serviceNames = serviceNames
  161.       
  162. class ImplementationHelper:
  163.       def __init__(self):
  164.       self.impls = {}
  165.       
  166.       def addImplementation( self, ctor, implementationName, serviceNames ):
  167.           self.impls[implementationName] =  _ImplementationHelperEntry(ctor,serviceNames)
  168.       
  169.       def writeRegistryInfo( self, regKey, smgr ):
  170.           for i in self.impls.items():
  171.           keyName = "/"+ i[0] + "/UNO/SERVICES"
  172.           key = regKey.createKey( keyName )
  173.           for serviceName in i[1].serviceNames:
  174.           key.createKey( serviceName )
  175.           return 1
  176.  
  177.       def getComponentFactory( self, implementationName , regKey, smgr ):
  178.       entry = self.impls.get( implementationName, None )
  179.       if entry == None:
  180.          raise RuntimeException( implementationName + " is unknown" , None )
  181.       return createSingleServiceFactory( entry.ctor, implementationName, entry.serviceNames )
  182.  
  183.       def getSupportedServiceNames( self, implementationName ):
  184.       entry = self.impls.get( implementationName, None )
  185.       if entry == None:
  186.          raise RuntimeException( implementationName + " is unknown" , None )
  187.       return entry.serviceNames         
  188.       
  189.       def supportsService( self, implementationName, serviceName ):
  190.       entry = self.impls.get( implementationName,None )
  191.       if entry == None:
  192.          raise RuntimeException( implementationName + " is unknown", None )
  193.           return serviceName in entry.serviceNames         
  194.  
  195.       
  196. class ImplementationEntry:
  197.       def __init__(self, implName, supportedServices, clazz ):
  198.       self.implName = implName
  199.       self.supportedServices = supportedServices
  200.       self.clazz = clazz
  201.  
  202. def writeRegistryInfoHelper( smgr, regKey, seqEntries ):
  203.     for entry in seqEntries:
  204.         keyName = "/"+ entry.implName + "/UNO/SERVICES"
  205.     key = regKey.createKey( keyName )
  206.     for serviceName in entry.supportedServices:
  207.         key.createKey( serviceName )
  208.  
  209. def systemPathToFileUrl( systemPath ):
  210.     "returns a file-url for the given system path"
  211.     return pyuno.systemPathToFileUrl( systemPath )
  212.  
  213. def fileUrlToSystemPath( url ):
  214.     "returns a system path (determined by the system, the python interpreter is running on)"
  215.     return pyuno.fileUrlToSystemPath( url )
  216.  
  217. def absolutize( path, relativeUrl ):
  218.     "returns an absolute file url from the given urls"
  219.     return pyuno.absolutize( path, relativeUrl )
  220.         
  221. def getComponentFactoryHelper( implementationName, smgr, regKey, seqEntries ):
  222.     for x in seqEntries:
  223.     if x.implName == implementationName:
  224.        return createSingleServiceFactory( x.clazz, implementationName, x.supportedServices )
  225.  
  226. def addComponentsToContext( toBeExtendedContext, contextRuntime, componentUrls, loaderName ):
  227.     smgr = contextRuntime.ServiceManager
  228.     loader = smgr.createInstanceWithContext( loaderName, contextRuntime )
  229.     implReg = smgr.createInstanceWithContext( "com.sun.star.registry.ImplementationRegistration",contextRuntime)
  230.  
  231.     isWin = os.name == 'nt' or os.name == 'dos'
  232.     #   create a temporary registry
  233.     for componentUrl in componentUrls:
  234.         reg = smgr.createInstanceWithContext( "com.sun.star.registry.SimpleRegistry", contextRuntime )
  235.     reg.open( "", 0, 1 )
  236.         if not isWin and componentUrl.endswith( ".uno" ):  # still allow platform independent naming
  237.             componentUrl = componentUrl + ".so"
  238.     implReg.registerImplementation( loaderName,componentUrl, reg )
  239.     rootKey = reg.getRootKey()
  240.     implementationKey = rootKey.openKey( "IMPLEMENTATIONS" )
  241.     implNames = implementationKey.getKeyNames()
  242.     extSMGR = toBeExtendedContext.ServiceManager
  243.     for x in implNames:
  244.         fac = loader.activate( max(x.split("/")),"",componentUrl,rootKey)
  245.         extSMGR.insert( fac )
  246.     reg.close()
  247.                 
  248. # never shrinks !
  249. _g_typeTable = {}
  250. def _unohelper_getHandle( self):
  251.    ret = None
  252.    if _g_typeTable.has_key( self.__class__ ):
  253.      ret = _g_typeTable[self.__class__]
  254.    else:
  255.      names = {}
  256.      traverse = list(self.__class__.__bases__)
  257.      while len( traverse ) > 0:
  258.          item = traverse.pop()
  259.          bases = item.__bases__
  260.          if uno.isInterface( item ):
  261.              names[item.__pyunointerface__] = None
  262.          elif len(bases) > 0:
  263.              # the "else if", because we only need the most derived interface
  264.              traverse = traverse + list(bases)#
  265.  
  266.      lst = names.keys()
  267.      types = []
  268.      for x in lst:
  269.          t = uno.getTypeByName( x )
  270.          types.append( t )
  271.          
  272.      ret = tuple(types) , uno.generateUuid()
  273.      _g_typeTable[self.__class__] = ret
  274.    return ret
  275.   
  276. class Base(XTypeProvider):
  277.       def getTypes( self ):
  278.       return _unohelper_getHandle( self )[0]
  279.       def getImplementationId(self):
  280.       return _unohelper_getHandle( self )[1]
  281.       
  282. # -------------------------------------------------
  283. # implementation details
  284. # -------------------------------------------------
  285. class _FactoryHelper_( XSingleComponentFactory, XServiceInfo, Base ):
  286.       def __init__( self, clazz, implementationName, serviceNames ):
  287.       self.clazz = clazz
  288.       self.implementationName = implementationName
  289.       self.serviceNames = serviceNames
  290.       
  291.       def getImplementationName( self ):
  292.       return self.implementationName
  293.  
  294.       def supportsService( self, ServiceName ):
  295.       return ServiceName in serviceNames
  296.  
  297.       def getSupportedServiceNames( self ):
  298.       return self.serviceNames
  299.  
  300.       def createInstanceWithContext( self, context ):
  301.       return self.clazz( context )
  302.           
  303.       def createInstanceWithArgumentsAndContext( self, args, context ):
  304.       return self.clazz( context, *args )
  305.       
  306.