commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

r6459 - in trunk/gnue-common/src/rpc/drivers: _helpers xmlrpc/pw_xmlrpc


From: johannes
Subject: r6459 - in trunk/gnue-common/src/rpc/drivers: _helpers xmlrpc/pw_xmlrpc
Date: Thu, 7 Oct 2004 04:46:53 -0500 (CDT)

Author: johannes
Date: 2004-10-07 04:46:52 -0500 (Thu, 07 Oct 2004)
New Revision: 6459

Modified:
   trunk/gnue-common/src/rpc/drivers/_helpers/DirectoryServer.py
   trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ClientAdapter.py
   trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ServerAdapter.py
Log:
Better handling of exceptions on method invocation


Modified: trunk/gnue-common/src/rpc/drivers/_helpers/DirectoryServer.py
===================================================================
--- trunk/gnue-common/src/rpc/drivers/_helpers/DirectoryServer.py       
2004-10-06 21:33:33 UTC (rev 6458)
+++ trunk/gnue-common/src/rpc/drivers/_helpers/DirectoryServer.py       
2004-10-07 09:46:52 UTC (rev 6459)
@@ -28,10 +28,19 @@
 #
 
 
-from gnue.common.apps import GDebug
+from gnue.common.apps import errors
 from gnue.common.rpc.drivers import Base
 import string
 
+# =============================================================================
+# Exceptions
+# =============================================================================
+
+class MethodNotFoundError (errors.ApplicationError):
+  def __init__ (self, method):
+    msg = u_("The requested method '%s' does not exist") % method
+    errors.ApplicationError.__init__ (self, msg)
+
 ##############################################################################
 #
 # ServerAdapter
@@ -51,9 +60,9 @@
     # to our internal "service directory"
     self.mapObjects(rpcdef,bindings)
 
-    GDebug.printMesg(3,'XML-RPC Service Directory:\n * %s' % \
+    gDebug (3, 'XML-RPC Service Directory:\n * %s' % \
             string.join(self.directory.keys(),'\n * '))
-    GDebug.printMesg(5,'XML-RPC Service Directory:\n%s' % \
+    gDebug (5,'XML-RPC Service Directory:\n%s' % \
             self.directory.keys())
     
     self._dispatchers=[self.methodDispatcher]
@@ -100,8 +109,8 @@
 
       # the direct mapping
       if bindings.has_key(object.binding):
-        GDebug.printMesg(3,'GNURPC Binding Information:');
-        GDebug.printMesg(3,' * %s is bound to \n * %s' % \
+        gDebug (3, 'GNURPC Binding Information:');
+        gDebug (3, ' * %s is bound to \n * %s' % \
                          (object.binding,bindings[object.binding]))
 
         # for services create an "static" object 
@@ -134,7 +143,7 @@
             object._realbinding=getattr(parent._realbinding,\
                                          object.name)
 
-            GDebug.printMesg(8,'* %s is bound to \n * %s' % \
+            gDebug (8, '* %s is bound to \n * %s' % \
                              (object._path,object._realbinding))
           except:
             tmsg = u_("GNURPC cannot bind service '%(name)s' to service "
@@ -167,7 +176,7 @@
         if parent._type == 'RpService':
           try:
             bindto=getattr(parent._realbinding,object.name)
-            GDebug.printMesg(8,'* %s is bound to \n * %s' % \
+            gDebug (8,'* %s is bound to \n * %s' % \
                              (object._path,bindto))
           except:
             tmsg = u_("GNURPC cannot bind method/attribut '%(name)s' to "
@@ -249,13 +258,16 @@
                                    'binding': None   } # TODO
 
 
-  def getMethodDirEntry(self,method):
-    GDebug.printMesg(6,'GNURPC Directory entry:\n (%s,%s,%s)' % \
-                     (self.directory[method]['signature'],\
-                      self.directory[method]['help'],\
-                      self.directory[method]['binding']))
+  def getMethodDirEntry (self, method):
+    if not self.directory.has_key (method):
+      raise MethodNotFoundError, method
+
+    gDebug (6, 'GNURPC Directory entry:\n (%s,%s,%s)' % \
+                     (self.directory [method]['signature'],\
+                      self.directory [method]['help'],\
+                      self.directory [method]['binding']))
     
-    return self.directory[method]
+    return self.directory [method]
 
 
 
@@ -279,7 +291,7 @@
       if rtype:
         # check for empty results (not allowed for XMLRPC)
         if (result==None) or (result==[None]):
-          GDebug.printMesg(3,'Transform result None into 1')
+          gDebug (3,'Transform result None into 1')
           result=1
           return result
     
@@ -341,8 +353,7 @@
     mparts.reverse()
     calltype=mparts[0]
     calltype=calltype[:4]
-    GDebug.printMesg(7,'method %s has calling type %s' %\
-                     (method,calltype))
+    gDebug (7,'method %s has calling type %s' % (method,calltype))
     
     if calltype=='set_':
       # setAttribut method

Modified: trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ClientAdapter.py
===================================================================
--- trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ClientAdapter.py 
2004-10-06 21:33:33 UTC (rev 6458)
+++ trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ClientAdapter.py 
2004-10-07 09:46:52 UTC (rev 6459)
@@ -93,6 +93,7 @@
         
     try:
       result = to_call (*__args, **params)
+
     except xmlrpclib.Fault, e:
       (exType, exName, exMessage, exDetail) = string.split (e.faultString,
                                                             u'\x91')

Modified: trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ServerAdapter.py
===================================================================
--- trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ServerAdapter.py 
2004-10-06 21:33:33 UTC (rev 6458)
+++ trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ServerAdapter.py 
2004-10-07 09:46:52 UTC (rev 6459)
@@ -189,8 +189,13 @@
   #
   # Call the requested method
   #
+
+  # ---------------------------------------------------------------------------
+  # Call the requested method
+  # ---------------------------------------------------------------------------
+
   def call(self, method, params):
-    if self._loglevel>0:
+    if self._loglevel > 0:
       print _("Dispatching: "), method, params
     
 
@@ -201,158 +206,162 @@
     ##                     params=""
     ## Call to an method: (of a service=one object)
     ##                     method="DonutPlace.Management.Restart"
+    try:
+      if method [0] == '[':
+        # call to an object
+        # 1. get the object from the objectlibrarian
+        # 2. check, if the object is supported by the gfd
 
-    if method[0]=='[':
-      # call to an object
-      # 1. get the object from the objectlibrarian
-      # 2. check, if the object is supported by the gfd
-      try:
-        i=string.index(method,']',1)
-        objhandle=method[1:i]
-        method=method[i+2:]
-      except ValueError:
-        tmsg = u_("Wrong format of object handle in method call %s") % method
-        raise AttributeError, tmsg
-      # TODO check in service dir, if obj is supported or not
-      o=ObjectLibrarian.retrieveObject(objhandle)
-      try:
-        server_method=getattr(o,method)
-        server_attribute=None
-      except AttributeError:
-        server_method=None
         try:
-          server_attribute=getattr(o,method[4:])
+          i = string.index (method, ']', 1)
+          objhandle = method [1:i]
+          method = method [i+2:]
+
+        except ValueError:
+          raise errors.ApplicationError, \
+              u_("Wrong format of object handle in method call %s") % method
+
+        # TODO check in service dir, if obj is supported or not
+        o = ObjectLibrarian.retrieveObject (objhandle)
+
+        try:
+          server_method = getattr (o, method)
+          server_attribute = None
+
         except AttributeError:
-          
-          if method!="_close":
-            msg = u_("Internal XMLRPC server error: method %s can be "
+          server_method = None
+
+          try:
+            server_attribute = getattr (o, method [4:])
+
+          except AttributeError:
+            if method != "_close":
+              raise errors.ApplicationError, \
+                  u_("Internal XMLRPC server error: method %s can be "
                      "found in the directory (build out of a .grpc file), "
                      "but the object doesn't contain this method/attribut. "
                      "Please check you .grpc file for wrong return types.") \
-                 % method
-            
-            raise AttributeError, msg
-        
-      if method!="_close":
-        direntry = self.getMethodDirEntry(o._type+"."+method)
-        signature=direntry['signature']
+                   % method
+          
+        if method != "_close":
+          direntry  = self.getMethodDirEntry (o._type + "." + method)
+          signature = direntry ['signature']
+        else:
+          signature = ('string',)
+  
       else:
-        signature= ('string',)                
-
-    else:
-
-      # call to a service method or a helper call (get/set) for
-      # a service attribut
-      try:
-        direntry = self.getMethodDirEntry(method)
-        server_method = direntry['binding']
+        # call to a service method or a helper call (get/set) for
+        # a service attribut
+        direntry         = self.getMethodDirEntry (method)
+        server_method    = direntry ['binding']
         server_attribute = None
-        
+          
         # check if it is an real method (binding -> method)
         # or an get/set method for an attribut (binding-> attribut)
-        if (type(server_method)!=type(self.call)):
+
+        if (type (server_method) != type (self.call)):
           server_attribute = server_method
-          server_method=None
-          
-        signature=direntry['signature']
+          server_method    = None
+            
+        signature = direntry ['signature']
+  
+        if (server_method is None) and (server_attribute is None):
+          raise errors.SystemError, \
+              u_("Server XML-RPC method '%s' is not bound to real method") \
+              % method
+  
+      try:
+        # TODO:  Compare submitted attributs with signature
+        pass
 
-        if (server_method==None) and (server_attribute==None):
-          tmsg = u_("Server XML-RPC method '%s' is not bound to real method") \
-                 % method
-          raise AttributeError, tmsg
       except KeyError:
-        tmsg = u_("Server does not have XML-RPC procedure %s") % method
-        raise AttributeError, tmsg
-    try:
-      #
-      pass
-        # TODO:  Compare submitted attributs with signature
-    except KeyError:
-      tmsg = u_("Server XML-RPC procedure %(method)s accepts just %(attr)s "
-                "as attributs") \
-             % {'method': method,
-                'attr'  : attr}
-      raise AttributeError, tmsg
-    
+        raise errors.ApplicationError, \
+            u_("Server XML-RPC procedure %(method)s accepts just %(attr)s "
+               "as attributes") \
+            % {'method': method,
+               'attr'  : attr}
+      
+  
+      # replace object handles in param with the real object
+      counter = 0
+      while counter < len (params):
+        p = params [counter]
+        if type (p) == type (""):
+          if (len (p) == 42) and (p [0] == "[") and (p [41] == "]"):
+            try:
+              p = p [1:41]
+              obj = ObjectLibrarian.retrieveObject (p)
+              newp = params [0:counter-1] + (obj,) + params [counter+1:]
+              params = newp
 
-    # replace object handles in param with the real object
-    counter=0
-    while counter<len(params):
-      p=params[counter]
-      if type(p)==type(""):
-        if (len(p)==42) and (p[0]=="[") and (p[41]=="]"):
-          try:
-            p=p[1:41]
-            obj=ObjectLibrarian.retrieveObject(p)
-            newp=params[0:counter-1]+(obj,)+params[counter+1:]
-            params=newp
-          except:
-            pass
-      counter=counter+1;
+            except:
+              pass
 
-    # check if it is an real method (binding -> method)
-    # or an get/set method for an attribut (binding-> attribut)
-
-    __params = typeconv.rpc_to_python (params, server.InvalidParameter)
-
-    if (server_method!=None):
-            
-      # call method with params
-      try:
-        result = server_method(*__params)
-      except:
-        stack = string.join (errors.getException (1), u'\x91')
-        return xmlrpclib.Fault (1, stack)
-
-      else:
+        counter += 1
+  
+      # check if it is an real method (binding -> method)
+      # or an get/set method for an attribut (binding-> attribut)
+  
+      __params = typeconv.rpc_to_python (params, server.InvalidParameter)
+  
+      if (server_method is not None):
+              
+        # call method with params
+        result = server_method (*__params)
         result = typeconv.python_to_rpc (result, server.InvalidParameter)
-
-    # check if it's the close object method
-    elif (method=="_close"):
-      
-      result=self.releaseDynamicObject(o)
-      
-    else:
-      
-      ## check wether its the set or the get method for the attribut
-      mparts=string.splitfields(method,'.')
-      mparts.reverse()
-      calltype=mparts[0]
-      calltype=calltype[:4]
-      gDebug (7, 'method %s has calling type %s' % (method,calltype))
-      if calltype=='set_':
-        # setAttribut method
-        server_attribute=params[0]
-      elif calltype=='get_':
-        # getAttribut method
-        result=server_attribute
+  
+      # check if it's the close object method
+      elif (method == "_close"):
+        result = self.releaseDynamicObject (o)
+        
       else:
-        tmsg = u_("Internal Server XML-RPC error: method type (get/set "
-                  "attribute) couldn't be detected (method %s)") % method
-        raise AttributeError, tmsg
-    
+        
+        ## check wether it's the set or the get method for the attribute
+        mparts = string.splitfields (method, '.')
+        mparts.reverse ()
+        calltype = mparts [0]
+        calltype = calltype [:4]
+        gDebug (7, 'method %s has calling type %s' % (method, calltype))
 
-    # replace real object in param with an object handle
-    if type(result)==type(self):  ## both should be instances
-       ObjectLibrarian.archiveObject(result)
+        if calltype == 'set_':
+          # setAttribut method
+          server_attribute = params [0]
 
-       # get the type of the result
-       rtype=signature[0]
-       # delete the surrounding brackets < >
-       rtype=rtype[1:len(rtype)-1]
-       # store typeinfo in new object
-       result._type=rtype
+        elif calltype == 'get_':
+          # getAttribut method
+          result = server_attribute
+        else:
+          raise errors.SystemError, \
+              u_("Internal Server XML-RPC error: method type (get/set "
+                 "attribute) couldn't be detected (method %s)") % method
+      
+  
+      # replace real object in param with an object handle
+      if type (result) == type (self):  ## both should be instances
+         ObjectLibrarian.archiveObject (result)
+  
+         # get the type of the result
+         rtype = signature [0]
+         # delete the surrounding brackets < >
+         rtype = rtype [1:len(rtype)-1]
+         # store typeinfo in new object
+         result._type = rtype
+  
+         result = ObjectLibrarian.getObjectReference (result)
+         self.registerDynamicObject (result, rtype)
+  
+      # check for empty results (not allowed for XMLRPC)
+      if (result is None) or (result == [None]):
+        gDebug (3, 'Transform result None into 1')
+        result = 1
 
-       result=ObjectLibrarian.getObjectReference(result)
-       self.registerDynamicObject(result,rtype)
+    except:
+      stack  = string.join (errors.getException (1), u'\x91')
+      result = xmlrpclib.Fault (1, stack)
 
-    # check for empty results (not allowed for XMLRPC)
-    if (result==None) or (result==[None]):
-      gDebug (3, 'Transform result None into 1')
-      result=1
-
     return result
-
+  
+  
   def registerDynamicObject(self,objhandle,type):
     #
     #  add new methods to the xmlrpc server





reply via email to

[Prev in Thread] Current Thread [Next in Thread]