Note that there are some explanatory texts on larger screens.

plurals
  1. POpythonic way to wrap xmlrpclib calls in similar multicalls
    primarykey
    data
    text
    <p>I'm writing a class that interfaces to a MoinMoin wiki via xmlrpc (simplified code follows):</p> <pre><code>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): </code></pre> <p>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:</p> <pre><code>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 </code></pre> <p>is there any nicer way to do it other than repeating that stuff for each and every method?</p> <p>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...</p> <p>But maybe I'm missing something easier. The best I've come out with is something like:</p> <pre><code>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] </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload