xref: /trunk/main/pyuno/source/module/unohelper.py (revision a0428e9e)
1*a0428e9eSAndrew Rist#**************************************************************
2*a0428e9eSAndrew Rist#
3*a0428e9eSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*a0428e9eSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*a0428e9eSAndrew Rist#  distributed with this work for additional information
6*a0428e9eSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*a0428e9eSAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*a0428e9eSAndrew Rist#  "License"); you may not use this file except in compliance
9*a0428e9eSAndrew Rist#  with the License.  You may obtain a copy of the License at
10*a0428e9eSAndrew Rist#
11*a0428e9eSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*a0428e9eSAndrew Rist#
13*a0428e9eSAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*a0428e9eSAndrew Rist#  software distributed under the License is distributed on an
15*a0428e9eSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a0428e9eSAndrew Rist#  KIND, either express or implied.  See the License for the
17*a0428e9eSAndrew Rist#  specific language governing permissions and limitations
18*a0428e9eSAndrew Rist#  under the License.
19*a0428e9eSAndrew Rist#
20*a0428e9eSAndrew Rist#**************************************************************
21cdf0e10cSrcweirimport uno
22cdf0e10cSrcweirimport pyuno
23cdf0e10cSrcweirimport os
24cdf0e10cSrcweirimport sys
25cdf0e10cSrcweir
26cdf0e10cSrcweirfrom com.sun.star.lang import XTypeProvider, XSingleComponentFactory, XServiceInfo
27cdf0e10cSrcweirfrom com.sun.star.uno import RuntimeException, XCurrentContext
28cdf0e10cSrcweirfrom com.sun.star.beans.MethodConcept import ALL as METHOD_CONCEPT_ALL
29cdf0e10cSrcweirfrom com.sun.star.beans.PropertyConcept import ALL as PROPERTY_CONCEPT_ALL
30cdf0e10cSrcweir
31cdf0e10cSrcweirfrom com.sun.star.reflection.ParamMode import \
32cdf0e10cSrcweir     IN as PARAM_MODE_IN, \
33cdf0e10cSrcweir     OUT as PARAM_MODE_OUT, \
34cdf0e10cSrcweir     INOUT as PARAM_MODE_INOUT
35cdf0e10cSrcweir
36cdf0e10cSrcweirfrom com.sun.star.beans.PropertyAttribute import \
37cdf0e10cSrcweir     MAYBEVOID as PROP_ATTR_MAYBEVOID, \
38cdf0e10cSrcweir     BOUND as PROP_ATTR_BOUND, \
39cdf0e10cSrcweir     CONSTRAINED as PROP_ATTR_CONSTRAINED, \
40cdf0e10cSrcweir     TRANSIENT as PROP_ATTR_TRANSIENT, \
41cdf0e10cSrcweir     READONLY as PROP_ATTR_READONLY, \
42cdf0e10cSrcweir     MAYBEAMBIGUOUS as PROP_ATTR_MAYBEAMBIGUOUS, \
43cdf0e10cSrcweir     MAYBEDEFAULT as PROP_ATTR_MAYBEDEFAULT, \
44cdf0e10cSrcweir     REMOVEABLE as PROP_ATTR_REMOVEABLE
45cdf0e10cSrcweir
46cdf0e10cSrcweirdef _mode_to_str( mode ):
47cdf0e10cSrcweir    ret = "[]"
48cdf0e10cSrcweir    if mode == PARAM_MODE_INOUT:
49cdf0e10cSrcweir        ret = "[inout]"
50cdf0e10cSrcweir    elif mode == PARAM_MODE_OUT:
51cdf0e10cSrcweir        ret = "[out]"
52cdf0e10cSrcweir    elif mode == PARAM_MODE_IN:
53cdf0e10cSrcweir        ret = "[in]"
54cdf0e10cSrcweir    return ret
55cdf0e10cSrcweir
56cdf0e10cSrcweirdef _propertymode_to_str( mode ):
57cdf0e10cSrcweir    ret = ""
58cdf0e10cSrcweir    if PROP_ATTR_REMOVEABLE & mode:
59cdf0e10cSrcweir        ret = ret + "removeable "
60cdf0e10cSrcweir    if PROP_ATTR_MAYBEDEFAULT & mode:
61cdf0e10cSrcweir        ret = ret + "maybedefault "
62cdf0e10cSrcweir    if PROP_ATTR_MAYBEAMBIGUOUS & mode:
63cdf0e10cSrcweir        ret = ret + "maybeambigous "
64cdf0e10cSrcweir    if PROP_ATTR_READONLY & mode:
65cdf0e10cSrcweir        ret = ret + "readonly "
66cdf0e10cSrcweir    if PROP_ATTR_TRANSIENT & mode:
67cdf0e10cSrcweir        ret = ret + "tranient "
68cdf0e10cSrcweir    if PROP_ATTR_CONSTRAINED & mode:
69cdf0e10cSrcweir        ret = ret + "constrained "
70cdf0e10cSrcweir    if PROP_ATTR_BOUND & mode:
71cdf0e10cSrcweir        ret = ret + "bound "
72cdf0e10cSrcweir    if PROP_ATTR_MAYBEVOID & mode:
73cdf0e10cSrcweir        ret = ret + "maybevoid "
74cdf0e10cSrcweir    return ret.rstrip()
75cdf0e10cSrcweir
76cdf0e10cSrcweirdef inspect( obj , out ):
77cdf0e10cSrcweir    if isinstance( obj, uno.Type ) or \
78cdf0e10cSrcweir       isinstance( obj, uno.Char ) or \
79cdf0e10cSrcweir       isinstance( obj, uno.Bool ) or \
80cdf0e10cSrcweir       isinstance( obj, uno.ByteSequence ) or \
81cdf0e10cSrcweir       isinstance( obj, uno.Enum ) or \
82cdf0e10cSrcweir       isinstance( obj, uno.Any ):
83cdf0e10cSrcweir        out.write( str(obj) + "\n")
84cdf0e10cSrcweir        return
85cdf0e10cSrcweir
86cdf0e10cSrcweir    ctx = uno.getComponentContext()
87cdf0e10cSrcweir    introspection = \
88cdf0e10cSrcweir         ctx.ServiceManager.createInstanceWithContext( "com.sun.star.beans.Introspection", ctx )
89cdf0e10cSrcweir
90cdf0e10cSrcweir    out.write( "Supported services:\n" )
91cdf0e10cSrcweir    if hasattr( obj, "getSupportedServiceNames" ):
92cdf0e10cSrcweir        names = obj.getSupportedServiceNames()
93cdf0e10cSrcweir        for ii in names:
94cdf0e10cSrcweir            out.write( "  " + ii + "\n" )
95cdf0e10cSrcweir    else:
96cdf0e10cSrcweir        out.write( "  unknown\n" )
97cdf0e10cSrcweir
98cdf0e10cSrcweir    out.write( "Interfaces:\n" )
99cdf0e10cSrcweir    if hasattr( obj, "getTypes" ):
100cdf0e10cSrcweir        interfaces = obj.getTypes()
101cdf0e10cSrcweir        for ii in interfaces:
102cdf0e10cSrcweir            out.write( "  " + ii.typeName + "\n" )
103cdf0e10cSrcweir    else:
104cdf0e10cSrcweir        out.write( "  unknown\n" )
105cdf0e10cSrcweir
106cdf0e10cSrcweir    access = introspection.inspect( obj )
107cdf0e10cSrcweir    methods = access.getMethods( METHOD_CONCEPT_ALL )
108cdf0e10cSrcweir    out.write( "Methods:\n" )
109cdf0e10cSrcweir    for ii in methods:
110cdf0e10cSrcweir        out.write( "  " + ii.ReturnType.Name + " " + ii.Name )
111cdf0e10cSrcweir        args = ii.ParameterTypes
112cdf0e10cSrcweir        infos = ii.ParameterInfos
113cdf0e10cSrcweir        out.write( "( " )
114cdf0e10cSrcweir        for i in range( 0, len( args ) ):
115cdf0e10cSrcweir            if i > 0:
116cdf0e10cSrcweir                out.write( ", " )
117cdf0e10cSrcweir            out.write( _mode_to_str( infos[i].aMode ) + " " + args[i].Name + " " + infos[i].aName )
118cdf0e10cSrcweir        out.write( " )\n" )
119cdf0e10cSrcweir
120cdf0e10cSrcweir    props = access.getProperties( PROPERTY_CONCEPT_ALL )
121cdf0e10cSrcweir    out.write ("Properties:\n" )
122cdf0e10cSrcweir    for ii in props:
123cdf0e10cSrcweir        out.write( "  ("+_propertymode_to_str( ii.Attributes ) + ") "+ii.Type.typeName+" "+ii.Name+ "\n" )
124cdf0e10cSrcweir
125cdf0e10cSrcweirdef createSingleServiceFactory( clazz, implementationName, serviceNames ):
126cdf0e10cSrcweir    return _FactoryHelper_( clazz, implementationName, serviceNames )
127cdf0e10cSrcweir
128cdf0e10cSrcweirclass _ImplementationHelperEntry:
129cdf0e10cSrcweir      def __init__(self, ctor,serviceNames):
130cdf0e10cSrcweir	  self.ctor = ctor
131cdf0e10cSrcweir	  self.serviceNames = serviceNames
132cdf0e10cSrcweir
133cdf0e10cSrcweirclass ImplementationHelper:
134cdf0e10cSrcweir      def __init__(self):
135cdf0e10cSrcweir	  self.impls = {}
136cdf0e10cSrcweir
137cdf0e10cSrcweir      def addImplementation( self, ctor, implementationName, serviceNames ):
138cdf0e10cSrcweir          self.impls[implementationName] =  _ImplementationHelperEntry(ctor,serviceNames)
139cdf0e10cSrcweir
140cdf0e10cSrcweir      def writeRegistryInfo( self, regKey, smgr ):
141cdf0e10cSrcweir          for i in self.impls.items():
142cdf0e10cSrcweir	      keyName = "/"+ i[0] + "/UNO/SERVICES"
143cdf0e10cSrcweir	      key = regKey.createKey( keyName )
144cdf0e10cSrcweir	      for serviceName in i[1].serviceNames:
145cdf0e10cSrcweir		  key.createKey( serviceName )
146cdf0e10cSrcweir          return 1
147cdf0e10cSrcweir
148cdf0e10cSrcweir      def getComponentFactory( self, implementationName , regKey, smgr ):
149cdf0e10cSrcweir	  entry = self.impls.get( implementationName, None )
150cdf0e10cSrcweir	  if entry == None:
151cdf0e10cSrcweir	     raise RuntimeException( implementationName + " is unknown" , None )
152cdf0e10cSrcweir	  return createSingleServiceFactory( entry.ctor, implementationName, entry.serviceNames )
153cdf0e10cSrcweir
154cdf0e10cSrcweir      def getSupportedServiceNames( self, implementationName ):
155cdf0e10cSrcweir	  entry = self.impls.get( implementationName, None )
156cdf0e10cSrcweir	  if entry == None:
157cdf0e10cSrcweir	     raise RuntimeException( implementationName + " is unknown" , None )
158cdf0e10cSrcweir	  return entry.serviceNames
159cdf0e10cSrcweir
160cdf0e10cSrcweir      def supportsService( self, implementationName, serviceName ):
161cdf0e10cSrcweir	  entry = self.impls.get( implementationName,None )
162cdf0e10cSrcweir	  if entry == None:
163cdf0e10cSrcweir	     raise RuntimeException( implementationName + " is unknown", None )
164cdf0e10cSrcweir          return serviceName in entry.serviceNames
165cdf0e10cSrcweir
166cdf0e10cSrcweir
167cdf0e10cSrcweirclass ImplementationEntry:
168cdf0e10cSrcweir      def __init__(self, implName, supportedServices, clazz ):
169cdf0e10cSrcweir	  self.implName = implName
170cdf0e10cSrcweir	  self.supportedServices = supportedServices
171cdf0e10cSrcweir	  self.clazz = clazz
172cdf0e10cSrcweir
173cdf0e10cSrcweirdef writeRegistryInfoHelper( smgr, regKey, seqEntries ):
174cdf0e10cSrcweir    for entry in seqEntries:
175cdf0e10cSrcweir        keyName = "/"+ entry.implName + "/UNO/SERVICES"
176cdf0e10cSrcweir	key = regKey.createKey( keyName )
177cdf0e10cSrcweir	for serviceName in entry.supportedServices:
178cdf0e10cSrcweir	    key.createKey( serviceName )
179cdf0e10cSrcweir
180cdf0e10cSrcweirdef systemPathToFileUrl( systemPath ):
181cdf0e10cSrcweir    "returns a file-url for the given system path"
182cdf0e10cSrcweir    return pyuno.systemPathToFileUrl( systemPath )
183cdf0e10cSrcweir
184cdf0e10cSrcweirdef fileUrlToSystemPath( url ):
185cdf0e10cSrcweir    "returns a system path (determined by the system, the python interpreter is running on)"
186cdf0e10cSrcweir    return pyuno.fileUrlToSystemPath( url )
187cdf0e10cSrcweir
188cdf0e10cSrcweirdef absolutize( path, relativeUrl ):
189cdf0e10cSrcweir    "returns an absolute file url from the given urls"
190cdf0e10cSrcweir    return pyuno.absolutize( path, relativeUrl )
191cdf0e10cSrcweir
192cdf0e10cSrcweirdef getComponentFactoryHelper( implementationName, smgr, regKey, seqEntries ):
193cdf0e10cSrcweir    for x in seqEntries:
194cdf0e10cSrcweir	if x.implName == implementationName:
195cdf0e10cSrcweir	   return createSingleServiceFactory( x.clazz, implementationName, x.supportedServices )
196cdf0e10cSrcweir
197cdf0e10cSrcweirdef addComponentsToContext( toBeExtendedContext, contextRuntime, componentUrls, loaderName ):
198cdf0e10cSrcweir    smgr = contextRuntime.ServiceManager
199cdf0e10cSrcweir    loader = smgr.createInstanceWithContext( loaderName, contextRuntime )
200cdf0e10cSrcweir    implReg = smgr.createInstanceWithContext( "com.sun.star.registry.ImplementationRegistration",contextRuntime)
201cdf0e10cSrcweir
202cdf0e10cSrcweir    isWin = os.name == 'nt' or os.name == 'dos'
203cdf0e10cSrcweir    isMac = sys.platform == 'darwin'
204cdf0e10cSrcweir    #   create a temporary registry
205cdf0e10cSrcweir    for componentUrl in componentUrls:
206cdf0e10cSrcweir        reg = smgr.createInstanceWithContext( "com.sun.star.registry.SimpleRegistry", contextRuntime )
207cdf0e10cSrcweir	reg.open( "", 0, 1 )
208cdf0e10cSrcweir        if not isWin and componentUrl.endswith( ".uno" ):  # still allow platform independent naming
209cdf0e10cSrcweir            if isMac:
210cdf0e10cSrcweir               componentUrl = componentUrl + ".dylib"
211cdf0e10cSrcweir            else:
212cdf0e10cSrcweir               componentUrl = componentUrl + ".so"
213cdf0e10cSrcweir
214cdf0e10cSrcweir	implReg.registerImplementation( loaderName,componentUrl, reg )
215cdf0e10cSrcweir	rootKey = reg.getRootKey()
216cdf0e10cSrcweir	implementationKey = rootKey.openKey( "IMPLEMENTATIONS" )
217cdf0e10cSrcweir	implNames = implementationKey.getKeyNames()
218cdf0e10cSrcweir	extSMGR = toBeExtendedContext.ServiceManager
219cdf0e10cSrcweir	for x in implNames:
220cdf0e10cSrcweir	    fac = loader.activate( max(x.split("/")),"",componentUrl,rootKey)
221cdf0e10cSrcweir	    extSMGR.insert( fac )
222cdf0e10cSrcweir	reg.close()
223cdf0e10cSrcweir
224cdf0e10cSrcweir# never shrinks !
225cdf0e10cSrcweir_g_typeTable = {}
226cdf0e10cSrcweirdef _unohelper_getHandle( self):
227cdf0e10cSrcweir   ret = None
228cdf0e10cSrcweir   if _g_typeTable.has_key( self.__class__ ):
229cdf0e10cSrcweir     ret = _g_typeTable[self.__class__]
230cdf0e10cSrcweir   else:
231cdf0e10cSrcweir     names = {}
232cdf0e10cSrcweir     traverse = list(self.__class__.__bases__)
233cdf0e10cSrcweir     while len( traverse ) > 0:
234cdf0e10cSrcweir         item = traverse.pop()
235cdf0e10cSrcweir         bases = item.__bases__
236cdf0e10cSrcweir         if uno.isInterface( item ):
237cdf0e10cSrcweir             names[item.__pyunointerface__] = None
238cdf0e10cSrcweir         elif len(bases) > 0:
239cdf0e10cSrcweir             # the "else if", because we only need the most derived interface
240cdf0e10cSrcweir             traverse = traverse + list(bases)#
241cdf0e10cSrcweir
242cdf0e10cSrcweir     lst = names.keys()
243cdf0e10cSrcweir     types = []
244cdf0e10cSrcweir     for x in lst:
245cdf0e10cSrcweir         t = uno.getTypeByName( x )
246cdf0e10cSrcweir         types.append( t )
247cdf0e10cSrcweir
248cdf0e10cSrcweir     ret = tuple(types) , uno.generateUuid()
249cdf0e10cSrcweir     _g_typeTable[self.__class__] = ret
250cdf0e10cSrcweir   return ret
251cdf0e10cSrcweir
252cdf0e10cSrcweirclass Base(XTypeProvider):
253cdf0e10cSrcweir      def getTypes( self ):
254cdf0e10cSrcweir	  return _unohelper_getHandle( self )[0]
255cdf0e10cSrcweir      def getImplementationId(self):
256cdf0e10cSrcweir	  return _unohelper_getHandle( self )[1]
257cdf0e10cSrcweir
258cdf0e10cSrcweirclass CurrentContext(XCurrentContext, Base ):
259cdf0e10cSrcweir    """a current context implementation, which first does a lookup in the given
260cdf0e10cSrcweir       hashmap and if the key cannot be found, it delegates to the predecessor
261cdf0e10cSrcweir       if available
262cdf0e10cSrcweir    """
263cdf0e10cSrcweir    def __init__( self, oldContext, hashMap ):
264cdf0e10cSrcweir        self.hashMap = hashMap
265cdf0e10cSrcweir        self.oldContext = oldContext
266cdf0e10cSrcweir
267cdf0e10cSrcweir    def getValueByName( self, name ):
268cdf0e10cSrcweir        if name in self.hashMap:
269cdf0e10cSrcweir            return self.hashMap[name]
270cdf0e10cSrcweir        elif self.oldContext != None:
271cdf0e10cSrcweir            return self.oldContext.getValueByName( name )
272cdf0e10cSrcweir        else:
273cdf0e10cSrcweir            return None
274cdf0e10cSrcweir
275cdf0e10cSrcweir# -------------------------------------------------
276cdf0e10cSrcweir# implementation details
277cdf0e10cSrcweir# -------------------------------------------------
278cdf0e10cSrcweirclass _FactoryHelper_( XSingleComponentFactory, XServiceInfo, Base ):
279cdf0e10cSrcweir      def __init__( self, clazz, implementationName, serviceNames ):
280cdf0e10cSrcweir	  self.clazz = clazz
281cdf0e10cSrcweir	  self.implementationName = implementationName
282cdf0e10cSrcweir	  self.serviceNames = serviceNames
283cdf0e10cSrcweir
284cdf0e10cSrcweir      def getImplementationName( self ):
285cdf0e10cSrcweir	  return self.implementationName
286cdf0e10cSrcweir
287cdf0e10cSrcweir      def supportsService( self, ServiceName ):
288cdf0e10cSrcweir	  return ServiceName in self.serviceNames
289cdf0e10cSrcweir
290cdf0e10cSrcweir      def getSupportedServiceNames( self ):
291cdf0e10cSrcweir	  return self.serviceNames
292cdf0e10cSrcweir
293cdf0e10cSrcweir      def createInstanceWithContext( self, context ):
294cdf0e10cSrcweir	  return self.clazz( context )
295cdf0e10cSrcweir
296cdf0e10cSrcweir      def createInstanceWithArgumentsAndContext( self, args, context ):
297cdf0e10cSrcweir	  return self.clazz( context, *args )
298cdf0e10cSrcweir
299