win32com map attributes to methods

jj jj at void.si
Fri Jun 7 18:35:56 EDT 2002


> Have you tried using property() in py2.2?
Yes, and its working, but i have to support older versions (at least py2.1).

My second attempt is done by the Mark's book  using _dynamic_ and
DynamicPolicy,  and its working for attributes. Ofcouse there is a BUT,
but its not working with methods.

Example
o.Flush  method is CALLED

# correct syntax causes proper method call too + exception:
o.Flush()
TypeError: 'NoneType' object is not callable, metod call invokes correct
method and because


# ----------- code -----------
mport sys, string
import pythoncom
import win32com.server.util
import win32com.client
#import win32com.server.policy


class ResponseIStream:
    '''Istream for response'''
    _public_methods_ = ['Write', 'Flush']
    _public_attrs_= ['ContentType', 'Expires']

    _com_interfaces_ = [pythoncom.IID_IStream, pythoncom.IID_IDispatch]

    def __init__(self):
        pass
    def set_Expires(self, value):
        print 'set_expires', value
        self.x = value
    def get_Expires(self):
        print 'get_expires'
        return self.x
    def write(self, x):
        print 'write:', x
    def Write(self, s):
        self.write(s)
        return len(s)
    def Flush(self):
        print 'Iresponse.Flush'


    def _dynamic_(self, name, lcid, wFlags, args):
        if wFlags & pythoncom.DISPATCH_METHOD:
            print 'method call', name
            item= getattr(self, name)
            return apply(item, args)

        elif wFlags & pythoncom.DISPATCH_PROPERTYGET:
            print 'propget'

            mname = 'get_'+name
            if hasattr(self, mname):
                return apply(getattr(self, mname), args)
            item = getattr(self, name)
            if callable(item):
                return
                #return apply(item, args)
            return item
        elif wFlags & pythoncom.DISPATCH_PROPERTYPUT:
            print 'propset'
            mname = 'set_'+name
            if hasattr(self, mname):
                return apply(getattr(self, mname), args)
            item = getattr(self, name)
            if callable(item):
                apply(item, args)
                return
            setattr(self, name, args)
if __name__=="__main__":

    o = win32com.server.util.wrap(ResponseIStream(),  usePolicy =
win32com.server.policy.DynamicPolicy)
    o = win32com.client.Dispatch(o)

    o.Flush
    print 'second flush !!!! is not working'

    o.Flush()
    o.Write('test')

    print 'set'
    o.Expires = 'test value'
    print 'get'
    print o.Expires


    xml = win32com.client.Dispatch("Msxml2.DOMDocument")
    xml.loadXML('<x>streaming</x>')
    xml.save(o)
    r = ResponseIStream()
    r.Expires = 9
    print r.Expires





More information about the Python-list mailing list