Python 3
I have a COM object via win32com.client.Dispatch with many methods used to automate the application. I'd like it so that every time any method of the COM object is called, python does something based on the returned value (logging actually).
My automation pseudocode is like this:
obj = win32com.client.DispatchEx("3rdParty.Application")
obj.methodA(Parameter)
obj.methodB()
obj.non_method_call
obj.methodN()
...
When any method call is made to obj, I would like what's intended by this pseudocode to happen:
x = obj.any_method(*args)
if x:
logger.debug(any_method.__name__ + ' was called at ' + datetime.now().strftime("%H:%M") + ' with parameters ' + str(*args)
else:
logger.error(obj.cerrmsg)
obj.logout
Note that the any_method.__name__
part would be nice, but is not crucial.
Can python do this, especially with a COM object, and without writing the logic for a finite set of methods?
Decorators sounded right because they modify functions/classes, but posts I've reviewed wouldn't work in this case (like going through the object's method dict), and then I heard they only work on methods defined in my own code. @Property was suggested, but I couldn't work out how to apply it in this case.
Advanced tricks welcome (getattr, metaclasses, functools, wraps, etc.) but please demonstrate.