annotate jabberbot/multicall.py @ 3183:952f8f1268c5

- Remove dummy formatter which did a lot of stuff to output text, instead simply serialize dom to text when endDocument is called. - Output valid doctype - Temporarily disable Include macro support which required the dummy formatter. - Update copyright
author Mikko Virkkil? <mvirkkil@cc.hut.fi>
date Tue, 04 Mar 2008 23:12:23 +0100
parents 01f05e74aa9c
children
rev   line source
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
1 """ XMLRPC MultiCall support for Python 2.3. Copied from xmlrpclib.py of Python 2.4.3. """
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
2
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
3 try:
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
4 from xmlrpclib import MultiCall
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
5 except ImportError:
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
6 from xmlrpclib import Fault
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
7
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
8 class _MultiCallMethod:
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
9 # some lesser magic to store calls made to a MultiCall object
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
10 # for batch execution
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
11 def __init__(self, call_list, name):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
12 self.__call_list = call_list
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
13 self.__name = name
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
14 def __getattr__(self, name):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
15 return _MultiCallMethod(self.__call_list, "%s.%s" % (self.__name, name))
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
16 def __call__(self, *args):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
17 self.__call_list.append((self.__name, args))
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
18
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
19 class MultiCallIterator:
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
20 """Iterates over the results of a multicall. Exceptions are
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
21 thrown in response to xmlrpc faults."""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
22
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
23 def __init__(self, results):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
24 self.results = results
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
25
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
26 def __getitem__(self, i):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
27 item = self.results[i]
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
28 if type(item) == type({}):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
29 raise Fault(item['faultCode'], item['faultString'])
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
30 elif type(item) == type([]):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
31 return item[0]
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
32 else:
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
33 raise ValueError,\
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
34 "unexpected type in multicall result"
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
35
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
36 class MultiCall:
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
37 """server -> a object used to boxcar method calls
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
38
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
39 server should be a ServerProxy object.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
40
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
41 Methods can be added to the MultiCall using normal
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
42 method call syntax e.g.:
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
43
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
44 multicall = MultiCall(server_proxy)
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
45 multicall.add(2,3)
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
46 multicall.get_address("Guido")
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
47
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
48 To execute the multicall, call the MultiCall object e.g.:
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
49
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
50 add_result, address = multicall()
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
51 """
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
52
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
53 def __init__(self, server):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
54 self.__server = server
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
55 self.__call_list = []
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
56
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
57 def __repr__(self):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
58 return "<MultiCall at %x>" % id(self)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
59
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
60 __str__ = __repr__
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
61
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
62 def __getattr__(self, name):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
63 return _MultiCallMethod(self.__call_list, name)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
64
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
65 def __call__(self):
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
66 marshalled_list = []
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
67 for name, args in self.__call_list:
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
68 marshalled_list.append({'methodName': name, 'params': args})
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2120
diff changeset
69
781
e46109ce944e Introduced multicall support. Refactored XmlRpcBase (moved methods, separated dispatcher). Added Python 2.3 support.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
70 return MultiCallIterator(self.__server.system.multicall(marshalled_list))