
[XMc           @   s  d  Z  d d l m Z m Z d d l Z d d l m Z m Z m Z d d l	 m
 Z
 m Z d d l m Z d d l m Z d d l m Z d d	 l m Z d
 d d d d d d d g Z d e f d     YZ d e
 f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d d d  Z d d e d   Z d! d d d d d"  Z! i e d# 6e d$ 6e! d% 6Z" e# d&  \ Z$ Z% d'   Z& d(   Z' i e d) 6e d* 6e d+ 6Z( i e d) 6e d* 6e d+ 6Z) e   Z* d d,  Z+ d-   Z, d.   Z- d/   Z. d0   Z/ d1   Z0 d2   Z1 d3   Z2 i e/ d) 6e1 d* 6e2 d+ 6Z3 d4   Z4 d S(5   s7  
Implementations of L{IStreamServerEndpoint} and L{IStreamClientEndpoint} that
wrap the L{IReactorTCP}, L{IReactorSSL}, and L{IReactorUNIX} interfaces.

This also implements an extensible mini-language for describing endpoints,
parsed by the L{clientFromString} and L{serverFromString} functions.

@since: 10.1
i(   t
   implementst   directlyProvidesN(   t
   interfacest   defert   error(   t   ClientFactoryt   Protocol(   t
   getPlugins(   t!   IStreamServerEndpointStringParser(   t!   IStreamClientEndpointStringParser(   t   FilePatht   clientFromStringt   serverFromStringt   TCP4ServerEndpointt   TCP4ClientEndpointt   UNIXServerEndpointt   UNIXClientEndpointt   SSL4ServerEndpointt   SSL4ClientEndpointt   _WrappingProtocolc           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s1  
    Wrap another protocol in order to notify my user when a connection has
    been made.

    @ivar _connectedDeferred: The L{Deferred} that will callback
        with the C{wrappedProtocol} when it is connected.

    @ivar _wrappedProtocol: An L{IProtocol} provider that will be
        connected.
    c         C   s>   | |  _  | |  _ t j j |  j  r: t |  t j  n  d S(   s   
        @param connectedDeferred: The L{Deferred} that will callback
            with the C{wrappedProtocol} when it is connected.

        @param wrappedProtocol: An L{IProtocol} provider that will be
            connected.
        N(   t   _connectedDeferredt   _wrappedProtocolR   t   IHalfCloseableProtocolt
   providedByR   (   t   selft   connectedDeferredt   wrappedProtocol(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   __init__,   s
    			c         C   s*   |  j  j |  j  |  j j |  j   d S(   s   
        Connect the C{self._wrappedProtocol} to our C{self.transport} and
        callback C{self._connectedDeferred} with the C{self._wrappedProtocol}
        N(   R   t   makeConnectiont	   transportR   t   callback(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   connectionMade;   s    c         C   s   |  j  j |  S(   sM   
        Proxy C{dataReceived} calls to our C{self._wrappedProtocol}
        (   R   t   dataReceived(   R   t   data(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR    D   s    c         C   s   |  j  j |  S(   sO   
        Proxy C{connectionLost} calls to our C{self._wrappedProtocol}
        (   R   t   connectionLost(   R   t   reason(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR"   K   s    c         C   s   |  j  j   d S(   sl   
        Proxy L{IHalfCloseableProtocol.readConnectionLost} to our
        C{self._wrappedProtocol}
        N(   R   t   readConnectionLost(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR$   R   s    c         C   s   |  j  j   d S(   sm   
        Proxy L{IHalfCloseableProtocol.writeConnectionLost} to our
        C{self._wrappedProtocol}
        N(   R   t   writeConnectionLost(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR%   Z   s    (	   t   __name__t
   __module__t   __doc__R   R   R    R"   R$   R%   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR       s   
						t   _WrappingFactoryc           B   s/   e  Z d  Z e Z d   Z d   Z d   Z RS(   sD  
    Wrap a factory in order to wrap the protocols it builds.

    @ivar _wrappedFactory:  A provider of I{IProtocolFactory} whose
        buildProtocol method will be called and whose resulting protocol
        will be wrapped.

    @ivar _onConnection: An L{Deferred} that fires when the protocol is
        connected
    c         C   s"   | |  _  t j d |  |  _ d S(   s.  
        @param wrappedFactory: A provider of I{IProtocolFactory} whose
            buildProtocol method will be called and whose resulting protocol
            will be wrapped.
        @param canceller: An object that will be called to cancel the
            L{self._onConnection} L{Deferred}
        t	   cancellerN(   t   _wrappedFactoryR   t   Deferredt   _onConnection(   R   t   wrappedFactoryR*   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR   p   s    	c         C   sD   y |  j  j |  } Wn |  j j   n X|  j |  j |  Sd S(   s   
        Proxy C{buildProtocol} to our C{self._wrappedFactory} or errback
        the C{self._onConnection} L{Deferred}.

        @return: An instance of L{_WrappingProtocol} or C{None}
        N(   R+   t   buildProtocolR-   t   errbackt   protocol(   R   t   addrt   proto(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR/   |   s
    c         C   s   |  j  j |  d S(   si   
        Errback the C{self._onConnection} L{Deferred} when the
        client connection fails.
        N(   R-   R0   (   R   t	   connectorR#   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   clientConnectionFailed   s    (   R&   R'   R(   R   R1   R   R/   R5   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR)   c   s
   
		c           B   s3   e  Z d  Z e e j  d d d  Z d   Z RS(   sr  
    TCP server endpoint with an IPv4 configuration

    @ivar _reactor: An L{IReactorTCP} provider.

    @type _port: int
    @ivar _port: The port number on which to listen for incoming connections.

    @type _backlog: int
    @ivar _backlog: size of the listen queue

    @type _interface: str
    @ivar _interface: the hostname to bind to, defaults to '' (all)
    i2   t    c         C   s@   | |  _  | |  _ t d d d d  |  _ | |  _ | |  _ d S(   s   
        @param reactor: An L{IReactorTCP} provider.
        @param port: The port number used listening
        @param backlog: size of the listen queue
        @param interface: the hostname to bind to, defaults to '' (all)
        t   backlogi2   t	   interfaceR6   N(   t   _reactort   _portt   dictt   _listenArgst   _backlogt
   _interface(   R   t   reactort   portR7   R8   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR      s
    			c         C   s.   t  j |  j j |  j | d |  j d |  j S(   sU   
        Implement L{IStreamServerEndpoint.listen} to listen on a TCP socket
        R7   R8   (   R   t   executeR9   t	   listenTCPR:   R=   R>   (   R   t   protocolFactory(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   listen   s
    	(   R&   R'   R(   R    R   t   IStreamServerEndpointR   RD   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR      s   c           B   s3   e  Z d  Z e e j  d d d  Z d   Z RS(   s  
    TCP client endpoint with an IPv4 configuration.

    @ivar _reactor: An L{IReactorTCP} provider.

    @type _host: str
    @ivar _host: The hostname to connect to as a C{str}

    @type _port: int
    @ivar _port: The port to connect to as C{int}

    @type _timeout: int
    @ivar _timeout: number of seconds to wait before assuming the
        connection has failed.

    @type _bindAddress: tuple
    @type _bindAddress: a (host, port) tuple of local address to bind
        to, or None.
    i   c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d S(   sx  
        @param reactor: An L{IReactorTCP} provider
        @param host: A hostname, used when connecting
        @param port: The port number, used when connecting
        @param timeout: number of seconds to wait before assuming the
            connection has failed.
        @param bindAddress: a (host, port tuple of local address to bind to,
            or None.
        N(   R9   t   _hostR:   t   _timeoutt   _bindAddress(   R   R?   t   hostR@   t   timeoutt   bindAddress(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR      s
    
				c            sn     f d   } yJ t  | |  } |  j j |  j |  j | d |  j d |  j   | j SWn t j	   SXd S(   sP   
        Implement L{IStreamClientEndpoint.connect} to connect via TCP.
        c            s*     j    |  j t j   j     d  S(   N(   t   stopConnectingR0   R   t   ConnectingCancelledErrort   getDestination(   t   deferred(   R4   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt
   _canceller   s    
RJ   RK   N(
   R)   R9   t
   connectTCPRF   R:   RG   RH   R-   R   t   fail(   R   RC   RP   t   wf(    (   R4   s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   connect   s    	N(	   R&   R'   R(   R    R   t   IStreamClientEndpointt   NoneR   RT   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR      s   c           B   s3   e  Z d  Z e e j  d d d  Z d   Z RS(   sS  
    SSL secured TCP server endpoint with an IPv4 configuration.

    @ivar _reactor: An L{IReactorSSL} provider.

    @type _host: str
    @ivar _host: The hostname to connect to as a C{str}

    @type _port: int
    @ivar _port: The port to connect to as C{int}

    @type _sslContextFactory: L{OpenSSLCertificateOptions}
    @var _sslContextFactory: SSL Configuration information as an
        L{OpenSSLCertificateOptions}

    @type _backlog: int
    @ivar _backlog: size of the listen queue

    @type _interface: str
    @ivar _interface: the hostname to bind to, defaults to '' (all)
    i2   R6   c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d S(   s  
        @param reactor: An L{IReactorSSL} provider.
        @param port: The port number used listening
        @param sslContextFactory: An instance of
            L{twisted.internet._sslverify.OpenSSLCertificateOptions}.
        @param timeout: number of seconds to wait before assuming the
            connection has failed.
        @param bindAddress: a (host, port tuple of local address to bind to,
            or None.
        N(   R9   R:   t   _sslContextFactoryR=   R>   (   R   R?   R@   t   sslContextFactoryR7   R8   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR     s
    				c      
   C   s7   t  j |  j j |  j | d |  j d |  j d |  j S(   sf   
        Implement L{IStreamServerEndpoint.listen} to listen for SSL on a
        TCP socket.
        t   contextFactoryR7   R8   (   R   RA   R9   t	   listenSSLR:   RW   R=   R>   (   R   RC   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyRD   %  s
    		(   R&   R'   R(   R    R   RE   R   RD   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR      s   c           B   s3   e  Z d  Z e e j  d d d  Z d   Z RS(   s  
    SSL secured TCP client endpoint with an IPv4 configuration

    @ivar _reactor: An L{IReactorSSL} provider.

    @type _host: str
    @ivar _host: The hostname to connect to as a C{str}

    @type _port: int
    @ivar _port: The port to connect to as C{int}

    @type _sslContextFactory: L{OpenSSLCertificateOptions}
    @var _sslContextFactory: SSL Configuration information as an
        L{OpenSSLCertificateOptions}

    @type _timeout: int
    @ivar _timeout: number of seconds to wait before assuming the
        connection has failed.

    @type _bindAddress: tuple
    @ivar _bindAddress: a (host, port) tuple of local address to bind
        to, or None.
    i   c         C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d S(   s  
        @param reactor: An L{IReactorSSL} provider.
        @param host: A hostname, used when connecting
        @param port: The port number, used when connecting
        @param sslContextFactory: SSL Configuration information as An instance
            of L{OpenSSLCertificateOptions}.
        @param timeout: number of seconds to wait before assuming the
            connection has failed.
        @param bindAddress: a (host, port tuple of local address to bind to,
            or None.
        N(   R9   RF   R:   RW   RG   RH   (   R   R?   RI   R@   RX   RJ   RK   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR   L  s    					c      	      st     f d   } yP t  | |  } |  j j |  j |  j | |  j d |  j d |  j   | j SWn t	 j
   SXd S(   sb   
        Implement L{IStreamClientEndpoint.connect} to connect with SSL over
        TCP.
        c            s*     j    |  j t j   j     d  S(   N(   RL   R0   R   RM   RN   (   RO   (   R4   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyRP   f  s    
RJ   RK   N(   R)   R9   t
   connectSSLRF   R:   RW   RG   RH   R-   R   RR   (   R   RC   RP   RS   (    (   R4   s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyRT   a  s    	N(	   R&   R'   R(   R    R   RU   RV   R   RT   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR   2  s   c           B   s6   e  Z d  Z e e j  d d d d  Z d   Z RS(   s-  
    UnixSocket server endpoint.

    @type path: str
    @ivar path: a path to a unix socket on the filesystem.

    @type _listenArgs: dict
    @ivar _listenArgs: A C{dict} of keyword args that will be passed
        to L{IReactorUNIX.listenUNIX}

    @var _reactor: An L{IReactorTCP} provider.
    i2   i  i    c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d S(   sC  
        @param reactor: An L{IReactorUNIX} provider.
        @param address: The path to the Unix socket file, used when listening
        @param listenArgs: An optional dict of keyword args that will be
            passed to L{IReactorUNIX.listenUNIX}
        @param backlog: number of connections to allow in backlog.
        @param mode: mode to set on the unix socket.  This parameter is
            deprecated.  Permissions should be set on the directory which
            contains the UNIX socket.
        @param wantPID: if True, create a pidfile for the socket.
        N(   R9   t   _addressR=   t   _modet   _wantPID(   R   R?   t   addressR7   t   modet   wantPID(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR     s
    				c      
   C   s7   t  j |  j j |  j | d |  j d |  j d |  j S(   sW   
        Implement L{IStreamServerEndpoint.listen} to listen on a UNIX socket.
        R7   R`   Ra   (   R   RA   R9   t
   listenUNIXR\   R=   R]   R^   (   R   RC   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyRD     s
    		(   R&   R'   R(   R    R   RE   R   RD   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR   v  s   c           B   s3   e  Z d  Z e e j  d d d  Z d   Z RS(   s  
    UnixSocket client endpoint.

    @type _path: str
    @ivar _path: a path to a unix socket on the filesystem.

    @type _timeout: int
    @ivar _timeout: number of seconds to wait before assuming the connection
        has failed.

    @type _checkPID: bool
    @ivar _checkPID: if True, check for a pid file to verify that a server
        is listening.

    @var _reactor: An L{IReactorUNIX} provider.
    i   i    c         C   s(   | |  _  | |  _ | |  _ | |  _ d S(   s\  
        @param reactor: An L{IReactorUNIX} provider.
        @param path: The path to the Unix socket file, used when connecting
        @param timeout: number of seconds to wait before assuming the
            connection has failed.
        @param checkPID: if True, check for a pid file to verify that a server
            is listening.
        N(   R9   t   _pathRG   t	   _checkPID(   R   R?   t   pathRJ   t   checkPID(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR     s    				c            sh     f d   } yD t  | |  } |  j j |  j | d |  j d |  j   | j SWn t j   SXd S(   sa   
        Implement L{IStreamClientEndpoint.connect} to connect via a
        UNIX Socket
        c            s*     j    |  j t j   j     d  S(   N(   RL   R0   R   RM   RN   (   RO   (   R4   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyRP     s    
RJ   Rf   N(	   R)   R9   t   connectUNIXRc   RG   Rd   R-   R   RR   (   R   RC   RP   RS   (    (   R4   s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyRT     s    		(   R&   R'   R(   R    R   RU   R   RT   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR     s   R6   i2   c         C   s*   t  |  |  f i | d 6t  |  d 6f S(   s  
    Internal parser function for L{_parseServer} to convert the string
    arguments for a TCP(IPv4) stream endpoint into the structured arguments.

    @param factory: the protocol factory being parsed, or C{None}.  (This was a
        leftover argument from when this code was in C{strports}, and is now
        mostly None and unused.)

    @type factory: L{IProtocolFactory} or C{NoneType}

    @param port: the integer port number to bind
    @type port: C{str}

    @param interface: the interface IP to listen on
    @param backlog: the length of the listen queue
    @type backlog: C{str}

    @return: a 2-tuple of (args, kwargs), describing  the parameters to
        L{IReactorTCP.listenTCP} (or, modulo argument 2, the factory, arguments
        to L{TCP4ServerEndpoint}.
    R8   R7   (   t   int(   t   factoryR@   R8   R7   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt	   _parseTCP  s    t   666c         C   s@   | |  f i t  | d  d 6t  |  d 6t t  |   d 6f S(   s  
    Internal parser function for L{_parseServer} to convert the string
    arguments for a UNIX (AF_UNIX/SOCK_STREAM) stream endpoint into the
    structured arguments.

    @param factory: the protocol factory being parsed, or C{None}.  (This was a
        leftover argument from when this code was in C{strports}, and is now
        mostly None and unused.)

    @type factory: L{IProtocolFactory} or C{NoneType}

    @param address: the pathname of the unix socket
    @type address: C{str}

    @param backlog: the length of the listen queue
    @type backlog: C{str}

    @param lockfile: A string '0' or '1', mapping to True and False
        respectively.  See the C{wantPID} argument to C{listenUNIX}

    @return: a 2-tuple of (args, kwargs), describing  the parameters to
        L{IReactorTCP.listenUNIX} (or, modulo argument 2, the factory,
        arguments to L{UNIXServerEndpoint}.
    i   R`   R7   Ra   (   Rh   t   bool(   Ri   R_   R`   R7   t   lockfile(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt
   _parseUNIX  s    	 s
   server.pemc   
      C   s   d d l  m } | d k r% | } n  i  } | d k	 rP t | j |  | d <n  | j | | |  }	 t |  |  |	 f i | d 6t |  d 6f S(   s  
    Internal parser function for L{_parseServer} to convert the string
    arguments for an SSL (over TCP/IPv4) stream endpoint into the structured
    arguments.

    @param factory: the protocol factory being parsed, or C{None}.  (This was a
        leftover argument from when this code was in C{strports}, and is now
        mostly None and unused.)

    @type factory: L{IProtocolFactory} or C{NoneType}

    @param port: the integer port number to bind
    @type port: C{str}

    @param interface: the interface IP to listen on
    @param backlog: the length of the listen queue
    @type backlog: C{str}

    @param privateKey: The file name of a PEM format private key file.
    @type privateKey: C{str}

    @param certKey: The file name of a PEM format certificate file.
    @type certKey: C{str}

    @param sslmethod: The string name of an SSL method, based on the name of a
        constant in C{OpenSSL.SSL}.  Must be one of: "SSLv23_METHOD",
        "SSLv2_METHOD", "SSLv3_METHOD", "TLSv1_METHOD".
    @type sslmethod: C{str}

    @return: a 2-tuple of (args, kwargs), describing  the parameters to
        L{IReactorSSL.listenSSL} (or, modulo argument 2, the factory, arguments
        to L{SSL4ServerEndpoint}.
    i(   t   sslt	   sslmethodR8   R7   N(   t   twisted.internetRo   RV   t   getattrt   SSLt   DefaultOpenSSLContextFactoryRh   (
   Ri   R@   t
   privateKeyt   certKeyRp   R8   R7   Ro   t   kwt   cf(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt	   _parseSSL  s    #	t   tcpt   unixRo   i   c         c   s   d } d } i d d 6d d 6} t  |   }  xl |  D]d } | | k rn t | f Vt | f Vd } | | } q3 | d k r | |  j   7} q3 | | 7} q3 Wt | f Vd S(   s~  
    Tokenize a strports string and yield each token.

    @param description: a string as described by L{serverFromString} or
        L{clientFromString}.

    @return: an iterable of 2-tuples of (L{_OP} or L{_STRING}, string).  Tuples
        starting with L{_OP} will contain a second element of either ':' (i.e.
        'next parameter') or '=' (i.e. 'assign parameter value').  For example,
        the string 'hello:greet\=ing=world' would result in a generator
        yielding these values::

            _STRING, 'hello'
            _OP, ':'
            _STRING, 'greet=ing'
            _OP, '='
            _STRING, 'world'
    R6   s   :=t   :t   =s   \N(   t   itert   _STRINGt   _OPt   next(   t   descriptiont   currentt   opst   nextOpst   n(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt	   _tokenizeJ  s    c            s   g  i        f d   } d } xU t  |   D]G \ } } | t k rZ | | f 7} q2 | d k r2 | |  d } q2 q2 W| |     f S(   s  
    Convert a description string into a list of positional and keyword
    parameters, using logic vaguely like what Python does.

    @param description: a string as described by L{serverFromString} or
        L{clientFromString}.

    @return: a 2-tuple of C{(args, kwargs)}, where 'args' is a list of all
        ':'-separated C{str}s not containing an '=' and 'kwargs' is a map of
        all C{str}s which do contain an '='.  For example, the result of
        C{_parse('a:b:d=1:c')} would be C{(['a', 'b', 'c'], {'d': '1'})}.
    c            s<   t  |   d k r&   j |  d  n |  d  |  d <d  S(   Ni   i    (   t   lent   append(   t   sofar(   t   argsRw   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   add}  s    R|   (    (    (   R   R   (   R   R   R   t   typet   value(    (   R   Rw   s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   _parseo  s    

t   TCPRs   t   UNIXc   	      C   s.  t  |   \ } } | s2 t |  d k r | r d |  f } | d	 k rm d } t j | d t d d n | t k r t |   n  | g | d d +n  | d } t j	 |  } | d	 k rx4 t
 t  D]& } | j | k r | | d | f Sq Wt d | f   n  | j   f | | | d |  S(
   s  
    Parse a stports description into a 2-tuple of arguments and keyword values.

    @param description: A description in the format explained by
        L{serverFromString}.
    @type description: C{str}

    @param factory: A 'factory' argument; this is left-over from
        twisted.application.strports, it's not really used.
    @type factory: L{IProtocolFactory} or L{None}

    @param default: Deprecated argument, specifying the default parser mode to
        use for unqualified description strings (those which do not have a ':'
        and prefix).
    @type default: C{str} or C{NoneType}

    @return: a 3-tuple of (plugin or name, arguments, keyword arguments)
    i   so   Unqualified strport description passed to 'service'.Use qualified endpoint descriptions; for example, 'tcp:%s'.Rz   t   categoryt
   stackleveli   i    s   Unknown endpoint type: '%s'N(   R   R   RV   t   warningst   warnt   DeprecationWarningt   _NO_DEFAULTt
   ValueErrort   _serverParserst   getR   R   t   prefixt   upper(	   R   Ri   t   defaultR   Rw   t   deprecationMessaget   endpointTypet   parsert   plugin(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   _parseServer  s&     

c         C   sr   t  | d |  \ } } } t |  t k	 rF | } | j |  | |  S| } | d  | d } t | |  | |  S(   s   
    Underlying implementation of L{serverFromString} which avoids exposing the
    deprecated 'default' argument to anything but L{strports.service}.
    i   i   N(   R   RV   R   t   strt   parseStreamServert   _endpointServerFactories(   R?   R   R   t   nameOrPluginR   Rw   R   t   name(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   _serverFromStringLegacy  s    c         C   s   t  |  | t  S(   s2	  
    Construct a stream server endpoint from an endpoint description string.

    The format for server endpoint descriptions is a simple string.  It is a
    prefix naming the type of endpoint, then a colon, then the arguments for
    that endpoint.

    For example, you can call it like this to create an endpoint that will
    listen on TCP port 80::

        serverFromString(reactor, "tcp:80")

    Additional arguments may be specified as keywords, separated with colons.
    For example, you can specify the interface for a TCP server endpoint to
    bind to like this::

        serverFromString(reactor, "tcp:80:interface=127.0.0.1")

    SSL server endpoints may be specified with the 'ssl' prefix, and the
    private key and certificate files may be specified by the C{privateKey} and
    C{certKey} arguments::

        serverFromString(reactor, "ssl:443:privateKey=key.pem:certKey=crt.pem")

    If a private key file name (C{privateKey}) isn't provided, a "server.pem"
    file is assumed to exist which contains the private key. If the certificate
    file name (C{certKey}) isn't provided, the private key file is assumed to
    contain the certificate as well.

    You may escape colons in arguments with a backslash, which you will need to
    use if you want to specify a full pathname argument on Windows::

        serverFromString(reactor,
            "ssl:443:privateKey=C\:/key.pem:certKey=C\:/cert.pem")

    finally, the 'unix' prefix may be used to specify a filesystem UNIX socket,
    optionally with a 'mode' argument to specify the mode of the socket file
    created by C{listen}::

        serverFromString(reactor, "unix:/var/run/finger")
        serverFromString(reactor, "unix:/var/run/finger:mode=660")

    This function is also extensible; new endpoint types may be registered as
    L{IStreamServerEndpointStringParser} plugins.  See that interface for more
    information.

    @param reactor: The server endpoint will be constructed with this reactor.

    @param description: The strports description to parse.

    @return: A new endpoint which can be used to listen with the parameters
        given by by C{description}.

    @rtype: L{IStreamServerEndpoint<twisted.internet.interfaces.IStreamServerEndpoint>}

    @raise ValueError: when the 'description' string cannot be parsed.

    @since: 10.2
    (   R   R   (   R?   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR     s    <c         C   s   |  j  d d  j  d d  S(   s~  
    Quote an argument to L{serverFromString} and L{clientFromString}.  Since
    arguments are separated with colons and colons are escaped with
    backslashes, some care is necessary if, for example, you have a pathname,
    you may be tempted to interpolate into a string like this::

        serverFromString("ssl:443:privateKey=%s" % (myPathName,))

    This may appear to work, but will have portability issues (Windows
    pathnames, for example).  Usually you should just construct the appropriate
    endpoint type rather than interpolating strings, which in this case would
    be L{SSL4ServerEndpoint}.  There are some use-cases where you may need to
    generate such a string, though; for example, a tool to manipulate a
    configuration file which has strports descriptions in it.  To be correct in
    those cases, do this instead::

        serverFromString("ssl:443:privateKey=%s" %
                         (quoteStringArgument(myPathName),))

    @param argument: The part of the endpoint description string you want to
        pass through.

    @type argument: C{str}

    @return: The quoted argument.

    @rtype: C{str}
    s   \s   \\R|   s   \:(   t   replace(   t   argument(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   quoteStringArgument  s    c          K   sD   t  |  d  |  d <y t  |  d  |  d <Wn t k
 r? n X|  S(   s   
    Perform any argument value coercion necessary for TCP client parameters.

    Valid keyword arguments to this function are all L{IReactorTCP.connectTCP}
    arguments.

    @return: The coerced values as a C{dict}.
    R@   RJ   (   Rh   t   KeyError(   t   kwargs(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   _parseClientTCP<  s    	c         C   s   d d l  m } i  } x |  j   D] } | j   j d  d j   d k sT q# n  y | j   } Wn t k
 rz q# n Xy | j j	 |  } Wn | j
 j k
 r q# X| j | | j   <q# W| j   S(   s   
    Load certificate-authority certificate objects in a given directory.

    @param directoryPath: a L{FilePath} pointing at a directory to load .pem
        files from.

    @return: a C{list} of L{OpenSSL.crypto.X509} objects.
    i(   Ro   t   .t   pem(   Rq   Ro   t   childrent   basenamet   splitt   lowert
   getContentt   IOErrort   Certificatet   loadPEMRs   t   Errort   originalt   digestt   values(   t   directoryPathRo   t   caCertst   childR!   t   theCert(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   _loadCAsFromDirN  s    	%c          K   s,  d d l  m } t |    }  |  j d d  } |  j d d  } |  j d d  } | d k	 r | j j t |  j    j	 } n d } | d k	 r | j
 j t |  j    j j	 } n d } | d k	 r t } t t |   } n t } d } | j d | j j d | d | d | d	 |  |  d
 <|  S(   sX  
    Perform any argument value coercion necessary for SSL client parameters.

    Valid keyword arguments to this function are all L{IReactorSSL.connectSSL}
    arguments except for C{contextFactory}.  Instead, C{certKey} (the path name
    of the certificate file) C{privateKey} (the path name of the private key
    associated with the certificate) are accepted and used to construct a
    context factory.
    
    @param caCertsDir: The one parameter which is not part of
        L{IReactorSSL.connectSSL}'s signature, this is a path name used to
        construct a list of certificate authority certificates.  The directory
        will be scanned for files ending in C{.pem}, all of which will be
        considered valid certificate authorities for this connection.

    @type caCertsDir: C{str}

    @return: The coerced values as a C{dict}.
    i(   Ro   Rv   Ru   t
   caCertsDirt   methodt   certificatet   verifyR   RX   N(   Rq   Ro   R   t   popRV   R   R   R
   R   R   t   PrivateCertificateRu   t   TrueR   t   Falset   CertificateOptionsRs   t   SSLv23_METHOD(   R   Ro   Rv   Ru   R   t   certx509R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   _parseClientSSLm  s2    			c          K   sg   y# t  t |  j d    |  d <Wn t k
 r6 n Xy t |  d  |  d <Wn t k
 rb n X|  S(   s:  
    Perform any argument value coercion necessary for UNIX client parameters.

    Valid keyword arguments to this function are all L{IReactorUNIX.connectUNIX}
    arguments except for C{checkPID}.  Instead, C{lockfile} is accepted and has
    the same meaning.

    @return: The coerced values as a C{dict}.
    Rm   Rf   RJ   (   Rl   Rh   R   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   _parseClientUNIX  s    
#c         C   s   t  |  \ } } | j d  } | j   } x9 t t  D]+ } | j j   | k r: | j | |   Sq: W| t k r t d | f   n  t | | |   } t	 | |  |  S(   s  
    Construct a client endpoint from a description string.

    Client description strings are much like server description strings,
    although they take all of their arguments as keywords, since even the
    simplest client endpoint (plain TCP) requires at least 2 arguments (host
    and port) to construct.

    You can create a TCP client endpoint with the 'host' and 'port' arguments,
    like so::

        clientFromString(reactor, "tcp:host=www.example.com:port=80")

    or an SSL client endpoint with those arguments, plus the arguments used by
    the server SSL, for a client certificate::

        clientFromString(reactor, "ssl:host=web.example.com:port=443:"
                                  "privateKey=foo.pem:certKey=foo.pem")

    to specify your certificate trust roots, you can identify a directory with
    PEM files in it with the C{caCertsDir} argument::

        clientFromString(reactor, "ssl:host=web.example.com:port=443:"
                                  "caCertsDir=/etc/ssl/certs")

    This function is also extensible; new endpoint types may be registered as
    L{IStreamClientEndpointStringParser} plugins.  See that interface for more
    information.

    @param reactor: The client endpoint will be constructed with this reactor.

    @param description: The strports description to parse.

    @return: A new endpoint which can be used to connect with the parameters
        given by by C{description}.
    @rtype: L{IStreamClientEndpoint<twisted.internet.interfaces.IStreamClientEndpoint>}

    @since: 10.2
    i    s   Unknown endpoint type: %r(
   R   R   R   R   R	   R   t   parseStreamClientt   _clientParsersR   t   _endpointClientFactories(   R?   R   R   R   t   anameR   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyR     s    ((5   R(   t   zope.interfaceR    R   R   Rq   R   R   R   t   twisted.internet.protocolR   R   t   twisted.pluginR   t   twisted.internet.interfacesR   R	   t   twisted.python.filepathR
   t   __all__R   R)   t   objectR   R   R   R   R   R   Rj   R   Rn   RV   Ry   R   t   rangeR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/endpoints.pyt   <module>   sh   C1+;8D.8 ,

	%	

	-		@	!			4	
