win32com map attributes to methods
jj
jj at void.si
Thu Jun 6 18:25:42 EDT 2002
Hi, I am trying to map attributes in COM object to methods, so when
attribute (for example: Expires) is set a "set_Expires" method will be
called, and when the attribute is read a method prefixed with "get_"
(get_Expires) method should be called.
I think I am on a good path, because my example is working 50%.
get_Expires is called when reading attr., but set_Expires is never called.
their implementation is so similar and simple, that there must be a simple
solution.
Thanks for your help
Janez
import sys, string
import pythoncom
import win32com.server.util
import win32com.client
from win32com.server.policy import DesignatedWrapPolicy
class PropWrapPolicy(DesignatedWrapPolicy):
'policy that will handle properties correctly'
def __init__(self, object):
DesignatedWrapPolicy.__init__(self, object)
def _wrap_(self, ob):
DesignatedWrapPolicy._wrap_(self, ob)
for name in ob._public_attrs_:
lname = string.lower(name)
id = self._name_to_dispid_[lname]
if hasattr(ob, 'set_' + name):
print 'found set_'+ name, id
self._dispid_to_put_[id] = 'set_' + name
if hasattr(ob, 'get_' + name):
print 'found get_' + name, id
self._dispid_to_get_[id] = 'get_' + name
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
def get_Expires(self):
print 'get_expires'
return 10
def write(self, x):
print 'write:', x
def Write(self, s):
self.write(s)
return len(s)
def Flush(self):
print 'Iresponse.Flush'
if __name__=="__main__":
o = win32com.server.util.wrap(ResponseIStream(),
pythoncom.IID_IDispatch, usePolicy = PropWrapPolicy)
o = win32com.client.Dispatch(o)
o.Flush()
o.Write('test')
print 'set'
# !!!!! set_Expires should be called !!!!
o.Expires = -1
print 'get'
print o.Expires
xml = win32com.client.Dispatch("Msxml2.DOMDocument")
xml.loadXML('<x>streaming</x>')
xml.save(o)
More information about the Python-list
mailing list