~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/support/multicall.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" XMLRPC MultiCall support for Python 2.3. Copied from xmlrpclib.py of Python 2.4.3. """
 
2
 
 
3
try:
 
4
    from xmlrpclib import MultiCall
 
5
except ImportError: 
 
6
    from xmlrpclib import Fault
 
7
    
 
8
    class _MultiCallMethod:
 
9
        # some lesser magic to store calls made to a MultiCall object
 
10
        # for batch execution
 
11
        def __init__(self, call_list, name):
 
12
            self.__call_list = call_list
 
13
            self.__name = name
 
14
        def __getattr__(self, name):
 
15
            return _MultiCallMethod(self.__call_list, "%s.%s" % (self.__name, name))
 
16
        def __call__(self, *args):
 
17
            self.__call_list.append((self.__name, args))
 
18
    
 
19
    class MultiCallIterator:
 
20
        """Iterates over the results of a multicall. Exceptions are
 
21
        thrown in response to xmlrpc faults."""
 
22
    
 
23
        def __init__(self, results):
 
24
            self.results = results
 
25
    
 
26
        def __getitem__(self, i):
 
27
            item = self.results[i]
 
28
            if type(item) == type({}):
 
29
                raise Fault(item['faultCode'], item['faultString'])
 
30
            elif type(item) == type([]):
 
31
                return item[0]
 
32
            else:
 
33
                raise ValueError,\
 
34
                      "unexpected type in multicall result"
 
35
    
 
36
    class MultiCall:
 
37
        """server -> a object used to boxcar method calls
 
38
    
 
39
        server should be a ServerProxy object.
 
40
    
 
41
        Methods can be added to the MultiCall using normal
 
42
        method call syntax e.g.:
 
43
    
 
44
        multicall = MultiCall(server_proxy)
 
45
        multicall.add(2,3)
 
46
        multicall.get_address("Guido")
 
47
    
 
48
        To execute the multicall, call the MultiCall object e.g.:
 
49
    
 
50
        add_result, address = multicall()
 
51
        """
 
52
    
 
53
        def __init__(self, server):
 
54
            self.__server = server
 
55
            self.__call_list = []
 
56
    
 
57
        def __repr__(self):
 
58
            return "<MultiCall at %x>" % id(self)
 
59
    
 
60
        __str__ = __repr__
 
61
    
 
62
        def __getattr__(self, name):
 
63
            return _MultiCallMethod(self.__call_list, name)
 
64
    
 
65
        def __call__(self):
 
66
            marshalled_list = []
 
67
            for name, args in self.__call_list:
 
68
                marshalled_list.append({'methodName' : name, 'params' : args})
 
69
    
 
70
            return MultiCallIterator(self.__server.system.multicall(marshalled_list))