[python-win32] wrapping objects from a COM server

Jonathan LaCour jonathan-lists at cleverdevil.org
Wed Jan 24 01:33:31 CET 2007


Mark Hammond wrote:

>> Is there a way that I can automatically have this happen for me, so
>> that `win32com.server.util.wrap` will automatically be called on the
>> way out of a method?
>
> It should be possible to have your _dynamic_ method do this for you?
> Instead of returning the result item, introspect what is returned  
> and if it
> is an instance with _public_methods_ etc, just explicitly do the wrap
> yourself.
>
> Or I'm missing something... :)

Well, thats basically what I am doing now, but these object structures
that are returned can be arbitrarily complex, with lists of objects
that contain other kinds of objects, which have lists of even more
objects.  Having to walk through arbitrary object structures and look
for lists/tuples of other objects, etc. is a bit of a pain, and I am
not sure if I can really do this reliably.

Here is a simple example:

     class Person:
         _public_methods_ = []
         _public_attrs_ = [
             'firstName',
             'lastName',
             'titles'
         ]

     class Title:
         _public_methods_ = []
         _public_attrs_ = [
             'name',
             'description'
         ]

     p1 = Person()
     p1.firstName = 'Jonathan'
     p1.lastName = 'LaCour'

     t1 = Title()
     t1.name = 'Some Title'
     t1.description = 'Some Description'

     p1.titles.append(t1)

In order to return arbitrary objects from my _dynamic_ method, I have
to check and see if any of the public attributes are lists or have
their own public attributes, and then replace them with "wrapped"
versions.  Then I have to see if the things contained within the lists
have _their_ own attributes, and so on.  It would be nice if I could
just do this:

     from win32com.server.util import wrap, register_wrapper

     register_wrapper(Title, wrap)
     register_wrapper(Person, wrap)

     class MyServer:
         ...

         def _dynamic_(self, ...):
             ...
             return wrap(p1)

Right now, if I just wrap the "outer" Person instance, the bridge
complains about not being able to convert a Title object into a COM
VARIANT.  If I could just tell it to use the existing `wrap` function,
then it could do all of this bridging for me automatically.

Does this make more sense?

--
Jonathan LaCour
http://cleverdevil.org





More information about the Python-win32 mailing list