[Python-checkins] python/dist/src/Lib xmlrpclib.py,1.30,1.31

loewis at users.sourceforge.net loewis at users.sourceforge.net
Fri Oct 31 08:49:39 EST 2003


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv14689/Lib

Modified Files:
	xmlrpclib.py 
Log Message:
Patch #531629: Add multicall support.


Index: xmlrpclib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xmlrpclib.py,v
retrieving revision 1.30
retrieving revision 1.31
diff -C2 -d -r1.30 -r1.31
*** xmlrpclib.py	20 Oct 2003 14:01:51 -0000	1.30
--- xmlrpclib.py	31 Oct 2003 13:49:36 -0000	1.31
***************
*** 51,54 ****
--- 51,55 ----
  # 2003-06-15 gn  Add support for time.struct_time
  # 2003-07-12 gp  Correct marshalling of Faults
+ # 2003-10-31 mvl Add multicall support
  #
  # Copyright (c) 1999-2002 by Secret Labs AB.
***************
*** 109,112 ****
--- 110,114 ----
    ServerProxy    Represents a logical connection to an XML-RPC server
  
+   MultiCall      Executor of boxcared xmlrpc requests 
    Boolean        boolean wrapper to generate a "boolean" XML-RPC value
    DateTime       dateTime wrapper for an ISO 8601 string or time tuple or
***************
*** 876,880 ****
--- 878,944 ----
      dispatch["methodName"] = end_methodName
  
+ ## Multicall support
+ #
+ 
+ class _MultiCallMethod:
+     # some lesser magic to store calls made to a MultiCall object
+     # for batch execution
+     def __init__(self, call_list, name):
+         self.__call_list = call_list
+         self.__name = name
+     def __getattr__(self, name):
+         return _MultiCallMethod(self.__call_list, "%s.%s" % (self.__name, name))
+     def __call__(self, *args):
+         self.__call_list.append((self.__name, args))
+ 
+ def MultiCallIterator(results):
+     """Iterates over the results of a multicall. Exceptions are
+     thrown in response to xmlrpc faults."""
+     
+     for i in results:
+         if type(i) == type({}):
+             raise Fault(i['faultCode'], i['faultString'])
+         elif type(i) == type([]):
+             yield i[0]
+         else:
+             raise ValueError,\
+                   "unexpected type in multicall result"
+         
+ class MultiCall:
+     """server -> a object used to boxcar method calls
+ 
+     server should be a ServerProxy object.
+ 
+     Methods can be added to the MultiCall using normal
+     method call syntax e.g.:
+ 
+     multicall = MultiCall(server_proxy)
+     multicall.add(2,3)
+     multicall.get_address("Guido")
+ 
+     To execute the multicall, call the MultiCall object e.g.:
  
+     add_result, address = multicall()
+     """
+     
+     def __init__(self, server):
+         self.__server = server
+         self.__call_list = []
+         
+     def __repr__(self):
+         return "<MultiCall at %x>" % id(self)
+     
+     __str__ = __repr__
+ 
+     def __getattr__(self, name):
+         return _MultiCallMethod(self.__call_list, name)
+ 
+     def __call__(self):
+         marshalled_list = []
+         for name, args in self.__call_list:
+             marshalled_list.append({'methodName' : name, 'params' : args})
+ 
+         return MultiCallIterator(self.__server.system.multicall(marshalled_list))
+         
  # --------------------------------------------------------------------
  # convenience functions
***************
*** 1329,1333 ****
  
      __str__ = __repr__
! 
      def __getattr__(self, name):
          # magic method dispatcher
--- 1393,1397 ----
  
      __str__ = __repr__
!     
      def __getattr__(self, name):
          # magic method dispatcher





More information about the Python-checkins mailing list