views:

51

answers:

1

I'm writing a class that interfaces to a MoinMoin wiki via xmlrpc (simplified code follows):

class MoinMoin(object):
    token = None

    def __init__(self, url, username=None, password=None):
        self.wiki  = xmlrpclib.ServerProxy(url + '/?action=xmlrpc2')
        if username and password:
            self.token = self.wiki.getAuthToken(username, password)
    # some sample methods:
    def searchPages(self, regexp):
    def getPage(self, page):
    def putPage(self, page):

now each of my methods needs to call the relevant xmlrpc method alone if there isn't authentication involved, or to wrap it in a multicall if there's auth. Example:

def getPage(self, page):
    if not self.token:
        result = self.wiki.getPage(page)
    else:
        mc = xmlrpclib.MultiCall(self.wiki) # build an XML-RPC multicall
        mc.applyAuthToken(self.token)       # call 1
        mc.getPage(page)                    # call 2
        result = mc()[-1]                  # run both, keep result of the latter
    return result

is there any nicer way to do it other than repeating that stuff for each and every method?

Since I have to call arbitrary methods, wrap them with stuff, then call the identically named method on another class, select relevant results and give them back, I suspect the solution would involve meta-classes or similar esoteric (for me) stuff. I should probably look at xmlrpclib sources and see how it's done, then maybe subclass their MultiCall to add my stuff...

But maybe I'm missing something easier. The best I've come out with is something like:

def _getMultiCall(self):
    mc = xmlrpclib.MultiCall(self.wiki)
    if self.token:
        mc.applyAuthToken(self.token)
    return mc
def fooMethod(self, x):
    mc = self._getMultiCall()
    mc.fooMethod(x)
    return mc()[-1]

but it still repeats the same three lines of code for each and every method I need to implement, just changing the called method name. Any better?

A: 

Python function are objects so they can be passed quite easily to other function.

def HandleAuthAndReturnResult(self, method, arg):
    mc = xmlrpclib.MultiCall(self.wiki)
    if self.token:
        mc.applyAuthToken(self.token)
    method(mc, arg)
    return mc()[-1]
def fooMethod(self, x):
    HandleAuthAndReturnResult(xmlrpclib.MultiCall.fooMethod, x)

There may be other way but I think it should work. Of course, the arg part needs to be aligned with what is needed for the method but all your methods take one argument.

Edit: I didn't understand that MultiCall was a proxy object. Even if the real method call ultimately is the one in your ServerProxy, you should not pass this method object in case MultiCall ever overrides(define) it. In this case, you could use the getattribute method with the method name you want to call and then call the returned function object. Take care to handle the AttributeError exception.

Methods would now look like:

def HandleAuthAndReturnResult(self, methodName, arg):
    mc = xmlrpclib.MultiCall(self.wiki)
    if self.token:
        mc.applyAuthToken(self.token)

    try:
        methodToCall = getattr(mc, methodName)
    except AttributeError:
        return None

    methodToCall(arg)
    return mc()[-1]

def fooMethod(self, x):
    HandleAuthAndReturnResult('fooMethod', x)
Eric Fortin
Doesn't work. fooMethod() is a remote method, not present in the xmlrpclib.MultiCall class (you get an AttributeError). Instances of that class will handle arbitrary method names because they just get passed to the remote end, but that still leaves me in a solution like the _getMultiCall() I described in the question.
Luke404
Update the answer to make it work with the MultiCall object that queues function calls.
Eric Fortin