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_0369 / unohelper.py
Text File  |  2004-10-09  |  11KB  |  312 lines

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