Question

I'm trying to wrap my xml-rpc server (python 2.7) and i'm using the code from this post

This is my code:

class XMLRPCWrapperProxy(object):
    def __init__(self, wrapped=None):
        if wrapped is None:
            wrapped = xmlrpclib.ServerProxy('http://xxx.xxx.xxx.xxx:xxxx')
        self.tf = wrapped

    def __getattr__(self, name):
        print "before"
        attr = getattr(self.tf, name)
        print "after"
        return type(self)(attr)

    def __call__(self, *args, **kw):
        return self.tf(*args, **kw)

server_w = XMLRPCWrapperProxy()

server_w.tf.do_function1()
server_w.tf.do_function2(param1)
server_w.tf.do_function3()
server_w.tf.do_function4(param1)

I want to wrap do_fuxntion... call but for some reason i can't see the print statements, i think i'm wrapping self.tf and not the call methods, any idea how can i warp any method call?

Was it helpful?

Solution

If you want to wrap tf, why do you call server_w.tf in your tests? That makes the wrapping kind of pointless.

If you try server_w.do_function1() you'll see the print-statements. I don't understand what you're trying to achieve with the type(self)(attr)line though...

I'd try __getattr__ simply like this:

def __getattr__(self, name):
    print "before"
    attr = getattr(self.tf, name)
    print "after"
    return attr

This should properly return the function of the wrapped object.

OTHER TIPS

This might be fairly obvious, but you can't see the print statements, because the __getattr__ method is never called. I suspect your calls should leave out the .tf part, if I understand the intent correctly. Thus it should read:

# ...
server_w.do_function1()
server_w.do_function2(param1)
server_w.do_function3()
server_w.do_function4(param1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top