home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / ZSI / twisted / client.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  9.2 KB  |  273 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import time
  5. from zope.interface import classProvides, implements, Interface
  6. from twisted.web import client
  7. from twisted.internet import defer
  8. from twisted.internet import reactor
  9. from twisted.python import log
  10. from twisted.python.failure import Failure
  11. from ZSI.parse import ParsedSoap
  12. from ZSI.writer import SoapWriter
  13. from ZSI.fault import FaultFromFaultMessage
  14. from ZSI.wstools.Namespaces import WSA
  15. from WSresource import HandlerChainInterface, CheckInputArgs
  16.  
  17. class HTTPPageGetter(client.HTTPPageGetter):
  18.     
  19.     def handleStatus_500(self):
  20.         log.err('HTTP Error 500')
  21.  
  22.     
  23.     def handleStatus_404(self):
  24.         log.err('HTTP Error 404')
  25.  
  26.  
  27. client.HTTPClientFactory.protocol = HTTPPageGetter
  28.  
  29. def getPage(url, contextFactory = None, *args, **kwargs):
  30.     (scheme, host, port, path) = client._parse(url)
  31.     factory = client.HTTPClientFactory(url, *args, **kwargs)
  32.     if scheme == 'https':
  33.         if contextFactory is None:
  34.             raise RuntimeError, 'must provide a contextFactory'
  35.         contextFactory is None
  36.         conn = reactor.connectSSL(host, port, factory, contextFactory)
  37.     else:
  38.         conn = reactor.connectTCP(host, port, factory)
  39.     return factory
  40.  
  41.  
  42. class ClientDataHandler:
  43.     classProvides(HandlerChainInterface)
  44.     readerClass = None
  45.     writerClass = None
  46.     
  47.     def processResponse(cls, soapdata, **kw):
  48.         if len(soapdata) == 0:
  49.             raise TypeError('Received empty response')
  50.         len(soapdata) == 0
  51.         ps = ParsedSoap(soapdata, readerclass = cls.readerClass)
  52.         if ps.IsAFault() is True:
  53.             log.msg('Received SOAP:Fault', debug = True)
  54.             raise FaultFromFaultMessage(ps)
  55.         ps.IsAFault() is True
  56.         return ps
  57.  
  58.     processResponse = classmethod(processResponse)
  59.     
  60.     def processRequest(cls, obj, nsdict = { }, header = True, **kw):
  61.         tc = None
  62.         if kw.has_key('requesttypecode'):
  63.             tc = kw['requesttypecode']
  64.         elif kw.has_key('requestclass'):
  65.             tc = kw['requestclass'].typecode
  66.         else:
  67.             tc = getattr(obj.__class__, 'typecode', None)
  68.         sw = SoapWriter(nsdict = nsdict, header = header, outputclass = cls.writerClass)
  69.         sw.serialize(obj, tc)
  70.         return sw
  71.  
  72.     processRequest = classmethod(processRequest)
  73.  
  74.  
  75. class WSAddressHandler:
  76.     implements(HandlerChainInterface)
  77.     uri = WSA.ADDRESS
  78.     
  79.     def processResponse(self, ps, wsaction = None, soapaction = None, **kw):
  80.         addr = self.address
  81.         addr.parse(ps)
  82.         action = addr.getAction()
  83.         if not action:
  84.             raise WSActionException('No WS-Action specified in Request')
  85.         action
  86.         if not soapaction:
  87.             return ps
  88.         soapaction = soapaction.strip('\'"')
  89.         if soapaction and soapaction != wsaction:
  90.             raise WSActionException('SOAP Action("%s") must match WS-Action("%s") if specified.' % (soapaction, wsaction))
  91.         soapaction != wsaction
  92.         return ps
  93.  
  94.     
  95.     def processRequest(self, sw, wsaction = None, url = None, endPointReference = None, **kw):
  96.         Address = Address
  97.         import ZSI.address
  98.         if sw is None:
  99.             self.address = None
  100.             return None
  101.         if not sw.header:
  102.             raise RuntimeError, 'expecting SOAP:Header'
  103.         sw.header
  104.         addr.setRequest(endPointReference, wsaction)
  105.         addr.serialize(sw, typed = False)
  106.         return sw
  107.  
  108.  
  109.  
  110. class DefaultClientHandlerChain:
  111.     
  112.     def __init__(self, *handlers):
  113.         self.handlers = handlers
  114.         self.debug = len(log.theLogPublisher.observers) > 0
  115.         self.flow = None
  116.  
  117.     __init__ = CheckInputArgs(HandlerChainInterface)(__init__)
  118.     
  119.     def parseResponse(ps, replytype):
  120.         return ps.Parse(replytype)
  121.  
  122.     parseResponse = staticmethod(parseResponse)
  123.     
  124.     def processResponse(self, arg, replytype, **kw):
  125.         if self.debug:
  126.             log.msg('--->PROCESS REQUEST\n%s' % arg, debug = 1)
  127.         
  128.         for h in self.handlers:
  129.             arg.addCallback(h.processResponse, **kw)
  130.         
  131.         arg.addCallback(self.parseResponse, replytype)
  132.  
  133.     
  134.     def processRequest(self, arg, **kw):
  135.         if self.debug:
  136.             log.msg('===>PROCESS RESPONSE: %s' % str(arg), debug = 1)
  137.         
  138.         if arg is None:
  139.             return None
  140.         for h in self.handlers:
  141.             arg = h.processRequest(arg, **kw)
  142.         
  143.         s = str(arg)
  144.         if self.debug:
  145.             log.msg(s, debug = 1)
  146.         
  147.         return s
  148.  
  149.  
  150.  
  151. class DefaultClientHandlerChainFactory:
  152.     protocol = DefaultClientHandlerChain
  153.     
  154.     def newInstance(cls):
  155.         return cls.protocol(ClientDataHandler)
  156.  
  157.     newInstance = classmethod(newInstance)
  158.  
  159.  
  160. class WSAddressClientHandlerChainFactory:
  161.     protocol = DefaultClientHandlerChain
  162.     
  163.     def newInstance(cls):
  164.         return cls.protocol(ClientDataHandler, WSAddressHandler())
  165.  
  166.     newInstance = classmethod(newInstance)
  167.  
  168.  
  169. class Binding:
  170.     agent = 'ZSI.twisted client'
  171.     factory = DefaultClientHandlerChainFactory
  172.     defer = False
  173.     
  174.     def __init__(self, url = None, nsdict = None, contextFactory = None, tracefile = None, **kw):
  175.         self.url = url
  176.         if not nsdict:
  177.             pass
  178.         self.nsdict = { }
  179.         self.contextFactory = contextFactory
  180.         self.http_headers = {
  181.             'content-type': 'text/xml' }
  182.         self.trace = tracefile
  183.  
  184.     
  185.     def addHTTPHeader(self, key, value):
  186.         self.http_headers[key] = value
  187.  
  188.     
  189.     def getHTTPHeaders(self):
  190.         return self.http_headers
  191.  
  192.     
  193.     def Send(self, url, opname, pyobj, nsdict = { }, soapaction = None, chain = None, **kw):
  194.         if not url:
  195.             pass
  196.         url = self.url
  197.         cookies = None
  198.         if chain is not None:
  199.             cookies = chain.flow.cookies
  200.         
  201.         d = { }
  202.         d.update(self.nsdict)
  203.         d.update(nsdict)
  204.         if soapaction is not None:
  205.             self.addHTTPHeader('SOAPAction', soapaction)
  206.         
  207.         chain = self.factory.newInstance()
  208.         soapdata = chain.processRequest(pyobj, nsdict = nsdict, soapaction = soapaction, **kw)
  209.         if self.trace:
  210.             print >>self.trace, '_' * 33, time.ctime(time.time()), 'REQUEST:'
  211.             print >>self.trace, soapdata
  212.         
  213.         f = getPage(str(url), contextFactory = self.contextFactory, postdata = soapdata, agent = self.agent, method = 'POST', headers = self.getHTTPHeaders(), cookies = cookies)
  214.         if isinstance(f, Failure):
  215.             return f
  216.         chain.flow = f
  217.         self.chain = chain
  218.         return chain
  219.  
  220.     
  221.     def Receive(self, replytype, chain = None, **kw):
  222.         if not chain:
  223.             pass
  224.         chain = self.chain
  225.         d = chain.flow.deferred
  226.         if self.trace:
  227.             
  228.             def trace(soapdata):
  229.                 print >>self.trace, '_' * 33, time.ctime(time.time()), 'RESPONSE:'
  230.                 print >>self.trace, soapdata
  231.                 return soapdata
  232.  
  233.             d.addCallback(trace)
  234.         
  235.         chain.processResponse(d, replytype, **kw)
  236.         if self.defer:
  237.             return d
  238.         failure = []
  239.         append = failure.append
  240.         
  241.         def errback(result):
  242.             append(result)
  243.  
  244.         d.addErrback(errback)
  245.         while not d.called:
  246.             reactor.runUntilCurrent()
  247.             t2 = reactor.timeout()
  248.             if reactor.running:
  249.                 pass
  250.             t = t2
  251.             reactor.doIteration(t)
  252.             continue
  253.             (self.defer,)
  254.         pyobj = d.result
  255.         if len(failure):
  256.             failure[0].raiseException()
  257.         
  258.         return pyobj
  259.  
  260.  
  261.  
  262. def trace():
  263.     if trace:
  264.         print >>trace, '_' * 33, time.ctime(time.time()), 'RESPONSE:'
  265.         for i in (self.reply_code, self.reply_msg):
  266.             print >>trace, str(i)
  267.         
  268.         print >>trace, '-------'
  269.         print >>trace, str(self.reply_headers)
  270.         print >>trace, self.data
  271.     
  272.  
  273.