• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/opal/ZSI/build/lib/ZSI/twisted/interfaces.py

00001 ###########################################################################
00002 # Joshua R. Boverhof, LBNL
00003 # See Copyright for copyright notice!
00004 # $Id: $
00005 ###########################################################################
00006 
00007 import sys, warnings
00008 
00009 # twisted & related imports
00010 from zope.interface import classProvides, implements, Interface
00011 
00012 # ZSI imports
00013 from ZSI import EvaluateException, ParseException, ParsedSoap, SoapWriter
00014 
00015 
00016 # 
00017 # Stability: Unstable
00018 # 
00019 
00020 def CheckInputArgs(*interfaces):
00021     """Must provide at least one interface, the last one may be repeated.
00022     """
00023     l = len(interfaces)
00024     def wrapper(func):
00025         def check_args(self, *args, **kw):
00026             for i in range(len(args)):
00027                 if (l > i and interfaces[i].providedBy(args[i])) or interfaces[-1].providedBy(args[i]):
00028                     continue
00029                 if l > i: raise TypeError, 'arg %s does not implement %s' %(args[i], interfaces[i])
00030                 raise TypeError, 'arg %s does not implement %s' %(args[i], interfaces[-1])
00031             func(self, *args, **kw)
00032         return check_args
00033     return wrapper
00034 
00035 
00036 class HandlerChainInterface(Interface):
00037     
00038     def processRequest(self, input, **kw):
00039         """returns a representation of the request, the 
00040         last link in the chain must return a response
00041         pyobj with a typecode attribute.
00042         Parameters:
00043             input --
00044         Keyword Parameters:
00045             request -- HTTPRequest instance
00046             resource  -- Resource instance
00047         """
00048     def processResponse(self, output, **kw):
00049         """returns a string representing the soap response.
00050         Parameters
00051             output --
00052         Keyword Parameters:
00053             request -- HTTPRequest instance
00054             resource  -- Resource instance
00055         """
00056 
00057 class CallbackChainInterface(Interface):
00058     
00059     def processRequest(self, input, **kw):
00060         """returns a response pyobj with a typecode 
00061         attribute.
00062         Parameters:
00063             input --
00064         Keyword Parameters:
00065             request -- HTTPRequest instance
00066             resource  -- Resource instance
00067         """
00068 
00069 class DataHandler:
00070     """
00071     class variables:
00072         readerClass -- factory class to create reader for ParsedSoap instances.
00073         writerClass -- ElementProxy implementation to use for SoapWriter instances.
00074     """
00075     classProvides(HandlerChainInterface)
00076     readerClass = None
00077     writerClass = None
00078 
00079     @classmethod
00080     def processRequest(cls, input, **kw):
00081         return ParsedSoap(input, readerclass=cls.readerClass)
00082 
00083     @classmethod
00084     def processResponse(cls, output, **kw):
00085         sw = SoapWriter(outputclass=cls.writerClass)
00086         sw.serialize(output)
00087         return sw
00088 
00089 
00090 class DefaultHandlerChain:
00091 
00092     @CheckInputArgs(CallbackChainInterface, HandlerChainInterface)
00093     def __init__(self, cb, *handlers):
00094         self.handlercb = cb
00095         self.handlers = handlers
00096         
00097     def processRequest(self, arg, **kw):
00098         
00099         for h in self.handlers:
00100             arg = h.processRequest(arg, **kw)
00101             
00102         return self.handlercb.processRequest(arg, **kw)
00103             
00104     def processResponse(self, arg, **kw):
00105 
00106         if arg is None: 
00107             return
00108 
00109         for h in self.handlers:
00110             arg = h.processResponse(arg, **kw)
00111             
00112         s = str(arg)
00113         
00114         return s
00115 

Generated on Wed Oct 20 2010 11:12:16 for APBS by  doxygen 1.7.2