<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> from socket import AF_INET6 , inet_pton , error <EOL> from zope . interface import implementer <EOL> from twisted . python . compat import _PY3 , unicode , lazyByteSlice <EOL> from twisted . python import reflect , failure <EOL> from twisted . internet import interfaces , main <EOL> if _PY3 : <EOL> def _concatenate ( bObj , offset , bArray ) : <EOL> return bObj [ offset : ] + b"<STR_LIT>" . join ( bArray ) <EOL> else : <EOL> def _concatenate ( bObj , offset , bArray ) : <EOL> return buffer ( bObj , offset ) + b"<STR_LIT>" . join ( bArray ) <EOL> class _ConsumerMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> producer = None <EOL> producerPaused = False <EOL> streamingProducer = False <EOL> def startWriting ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def registerProducer ( self , producer , streaming ) : <EOL> """<STR_LIT>""" <EOL> if self . producer is not None : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( producer , self . producer ) ) <EOL> if self . disconnected : <EOL> producer . stopProducing ( ) <EOL> else : <EOL> self . producer = producer <EOL> self . streamingProducer = streaming <EOL> if not streaming : <EOL> producer . resumeProducing ( ) <EOL> def unregisterProducer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . producer = None <EOL> if self . connected and self . disconnecting : <EOL> self . startWriting ( ) <EOL> @ implementer ( interfaces . ILoggingContext ) <EOL> class _LogOwner ( object ) : <EOL> """<STR_LIT>""" <EOL> def _getLogPrefix ( self , applicationObject ) : <EOL> """<STR_LIT>""" <EOL> if interfaces . ILoggingContext . providedBy ( applicationObject ) : <EOL> return applicationObject . logPrefix ( ) <EOL> return applicationObject . __class__ . __name__ <EOL> def logPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT:->" <EOL> @ implementer ( <EOL> interfaces . IPushProducer , interfaces . IReadWriteDescriptor , <EOL> interfaces . IConsumer , interfaces . ITransport , <EOL> interfaces . IHalfCloseableDescriptor ) <EOL> class FileDescriptor ( _ConsumerMixin , _LogOwner ) : <EOL> """<STR_LIT>""" <EOL> connected = <NUM_LIT:0> <EOL> disconnected = <NUM_LIT:0> <EOL> disconnecting = <NUM_LIT:0> <EOL> _writeDisconnecting = False <EOL> _writeDisconnected = False <EOL> dataBuffer = b"<STR_LIT>" <EOL> offset = <NUM_LIT:0> <EOL> SEND_LIMIT = <NUM_LIT> * <NUM_LIT> <EOL> def __init__ ( self , reactor = None ) : <EOL> """<STR_LIT>""" <EOL> if not reactor : <EOL> from twisted . internet import reactor <EOL> self . reactor = reactor <EOL> self . _tempDataBuffer = [ ] <EOL> self . _tempDataLen = <NUM_LIT:0> <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . disconnected = <NUM_LIT:1> <EOL> self . connected = <NUM_LIT:0> <EOL> if self . producer is not None : <EOL> self . producer . stopProducing ( ) <EOL> self . producer = None <EOL> self . stopReading ( ) <EOL> self . stopWriting ( ) <EOL> def writeSomeData ( self , data ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" % <EOL> reflect . qual ( self . __class__ ) ) <EOL> def doRead ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" % <EOL> reflect . qual ( self . __class__ ) ) <EOL> def doWrite ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . dataBuffer ) - self . offset < self . SEND_LIMIT : <EOL> self . dataBuffer = _concatenate ( <EOL> self . dataBuffer , self . offset , self . _tempDataBuffer ) <EOL> self . offset = <NUM_LIT:0> <EOL> self . _tempDataBuffer = [ ] <EOL> self . _tempDataLen = <NUM_LIT:0> <EOL> if self . offset : <EOL> l = self . writeSomeData ( lazyByteSlice ( self . dataBuffer , self . offset ) ) <EOL> else : <EOL> l = self . writeSomeData ( self . dataBuffer ) <EOL> if isinstance ( l , Exception ) or l < <NUM_LIT:0> : <EOL> return l <EOL> self . offset += l <EOL> if self . offset == len ( self . dataBuffer ) and not self . _tempDataLen : <EOL> self . dataBuffer = b"<STR_LIT>" <EOL> self . offset = <NUM_LIT:0> <EOL> self . stopWriting ( ) <EOL> if self . producer is not None and ( ( not self . streamingProducer ) <EOL> or self . producerPaused ) : <EOL> self . producerPaused = False <EOL> self . producer . resumeProducing ( ) <EOL> elif self . disconnecting : <EOL> return self . _postLoseConnection ( ) <EOL> elif self . _writeDisconnecting : <EOL> self . _writeDisconnected = True <EOL> result = self . _closeWriteConnection ( ) <EOL> return result <EOL> return None <EOL> def _postLoseConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> return main . CONNECTION_DONE <EOL> def _closeWriteConnection ( self ) : <EOL> pass <EOL> def writeConnectionLost ( self , reason ) : <EOL> self . connectionLost ( reason ) <EOL> def readConnectionLost ( self , reason ) : <EOL> self . connectionLost ( reason ) <EOL> def _isSendBufferFull ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . dataBuffer ) + self . _tempDataLen > self . bufferSize <EOL> def _maybePauseProducer ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . producer is not None and self . streamingProducer : <EOL> if self . _isSendBufferFull ( ) : <EOL> self . producerPaused = True <EOL> self . producer . pauseProducing ( ) <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( data , unicode ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not self . connected or self . _writeDisconnected : <EOL> return <EOL> if data : <EOL> self . _tempDataBuffer . append ( data ) <EOL> self . _tempDataLen += len ( data ) <EOL> self . _maybePauseProducer ( ) <EOL> self . startWriting ( ) <EOL> def writeSequence ( self , iovec ) : <EOL> """<STR_LIT>""" <EOL> for i in iovec : <EOL> if isinstance ( i , unicode ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not self . connected or not iovec or self . _writeDisconnected : <EOL> return <EOL> self . _tempDataBuffer . extend ( iovec ) <EOL> for i in iovec : <EOL> self . _tempDataLen += len ( i ) <EOL> self . _maybePauseProducer ( ) <EOL> self . startWriting ( ) <EOL> def loseConnection ( self , _connDone = failure . Failure ( main . CONNECTION_DONE ) ) : <EOL> """<STR_LIT>""" <EOL> if self . connected and not self . disconnecting : <EOL> if self . _writeDisconnected : <EOL> self . stopReading ( ) <EOL> self . stopWriting ( ) <EOL> self . connectionLost ( _connDone ) <EOL> else : <EOL> self . stopReading ( ) <EOL> self . startWriting ( ) <EOL> self . disconnecting = <NUM_LIT:1> <EOL> def loseWriteConnection ( self ) : <EOL> self . _writeDisconnecting = True <EOL> self . startWriting ( ) <EOL> def stopReading ( self ) : <EOL> """<STR_LIT>""" <EOL> self . reactor . removeReader ( self ) <EOL> def stopWriting ( self ) : <EOL> """<STR_LIT>""" <EOL> self . reactor . removeWriter ( self ) <EOL> def startReading ( self ) : <EOL> """<STR_LIT>""" <EOL> self . reactor . addReader ( self ) <EOL> def startWriting ( self ) : <EOL> """<STR_LIT>""" <EOL> self . reactor . addWriter ( self ) <EOL> producer = None <EOL> bufferSize = <NUM_LIT:2> ** <NUM_LIT:2> ** <NUM_LIT:2> ** <NUM_LIT:2> <EOL> def stopConsuming ( self ) : <EOL> """<STR_LIT>""" <EOL> self . unregisterProducer ( ) <EOL> self . loseConnection ( ) <EOL> def resumeProducing ( self ) : <EOL> if self . connected and not self . disconnecting : <EOL> self . startReading ( ) <EOL> def pauseProducing ( self ) : <EOL> self . stopReading ( ) <EOL> def stopProducing ( self ) : <EOL> self . loseConnection ( ) <EOL> def fileno ( self ) : <EOL> """<STR_LIT>""" <EOL> return - <NUM_LIT:1> <EOL> def isIPAddress ( addr ) : <EOL> """<STR_LIT>""" <EOL> dottedParts = addr . split ( '<STR_LIT:.>' ) <EOL> if len ( dottedParts ) == <NUM_LIT:4> : <EOL> for octet in dottedParts : <EOL> try : <EOL> value = int ( octet ) <EOL> except ValueError : <EOL> return False <EOL> else : <EOL> if value < <NUM_LIT:0> or value > <NUM_LIT:255> : <EOL> return False <EOL> return True <EOL> return False <EOL> def isIPv6Address ( addr ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:%>' in addr : <EOL> addr = addr . split ( '<STR_LIT:%>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if not addr : <EOL> return False <EOL> try : <EOL> inet_pton ( AF_INET6 , addr ) <EOL> except ( ValueError , error ) : <EOL> return False <EOL> return True <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] </s>
