mrodent33 Power User

Joined: 06 Nov 2010 Posts: 74
|
Posted: Fri Aug 19, 2011 5:39 am Post subject: (Jython) using MRI with Hanya-Wrapped objects |
|
|
There's a brilliant chap called Hanya who has written a way of Jython users of OOo (of whom there may well only be 2, him and me) being able to avoid endlessly fetching XInterfaces from the Uno runtime, as in Java, instead of the automatic reflection of Python. Details of his Wrapper can be found here:
http://www.oooforum.org/forum/viewtopic.phtml?t=126598
But this then leads to a problem when you want to use MRI (written incidentally by Hanya!) which, because Jython is written in Java, expects to receive XInterfaces to introspect, and also an XComponentContext.
So my little contribution overcomes this by allowing you to submit either an object exporting XInterface(s) for the object for introspection, or a Wrapper wrapping one, and also either an XComponentContext, or a Wrapper wrapping one, as param 2. It's child's play really but for those discovering the raw power of Jython-OOo-Hanya Wrapper will hopefully be useful.
NB getUnoInterface() and isUnoInterfaceInstanceOf() might be used by production code, but inspect() will only be used during development - this consideration has determined my use of assert and __debug__
| Code: | def getUnoInterface( unoInterface, object ):
if __debug__:
checkNotNull( ( unoInterface, object ) )
_checkClassAndPackage( unoInterface )
returnedObject = star.uno.UnoRuntime.queryInterface( unoInterface, object )
assert returnedObject != None, \
"could not get interface " + unoInterface.getSimpleName() + " for object of class " \
+ object.getClass().getSimpleName()
return returnedObject
def _checkClassAndPackage( unoInterface ):
interfaceSimpleName = unoInterface.getSimpleName()
packageName = unoInterface.getPackage().getName()
assert packageName.startswith("com.sun.star"), "not a UNO module: " + packageName
assert interfaceSimpleName.startswith("X"), "not a UNO interface: " + interfaceSimpleName + " (must begin with \"X\")"
def isUnoInterfaceInstanceOf( unoInterface, object ):
if __debug__:
checkNotNull( (unoInterface, object ))
_checkClassAndPackage( unoInterface )
return star.uno.UnoRuntime.queryInterface( unoInterface, object) != None
# NB this method will only be used during development: hence the lack of "__debug__" check
# and the liberal use of assert
def inspect( object, compContext ):
checkNotNull( ( object, compContext ))
import UnoWrapper
if isinstance ( compContext, UnoWrapper.Wrapper ):
unoObject = UnoWrapper.Wrapper.getUnoObject( compContext )
assert isUnoInterfaceInstanceOf( star.uno.XComponentContext, unoObject ), \
"Wrapper compContext's Uno object does not export XComponentContext"
compContext = getUnoInterface( star.uno.XComponentContext, unoObject )
else:
assert isUnoInterfaceInstanceOf( star.uno.XComponentContext, compContext ), \
"compContext param does not export XComponentContext and is not Wrapper"
if isinstance ( object, UnoWrapper.Wrapper ):
object = UnoWrapper.Wrapper.getUnoObject( object )
# NB at this point the compContext is a real XComponentContext object, not wrapped and
# the object is an unwrapped object which is hopefully a UNO object
oMri = compContext.getServiceManager().createInstanceWithContext( "mytools.Mri", compContext )
getUnoInterface( star.beans.XIntrospection, oMri ).inspect(object)
|
|
|