B
    F.\x                 @   s   d dl mZmZmZ d dlZd dlZddlmZ ej	dZ
dddZd	d
 Zdd ZdddZdddZdddZdd Zdd Zdd ZdS )    )print_functiondivisionabsolute_importN   )registryzdistributed.comm.default-schemeFc             C   sT   t | tjstd| jj | d\}}}|rD|sDd|  }t||sLt}||fS )z
    Split address into its scheme and scheme-dependent location string.

    >>> parse_address('tcp://127.0.0.1')
    ('tcp', '127.0.0.1')

    If strict is set to true the address must have a scheme.
    zexpected str, got %rz://zKInvalid url scheme. Must include protocol like tcp://localhost:8000. Got %s)	
isinstancesixZstring_types	TypeError	__class____name__
rpartition
ValueErrorDEFAULT_SCHEME)addrstrictschemeseplocmsg r   :lib/python3.7/site-packages/distributed/comm/addressing.pyparse_address   s    	r   c             C   s   d| |f S )zb
    Undo parse_address().

    >>> unparse_address('tcp', '127.0.0.1')
    'tcp://127.0.0.1'
    z%s://%sr   )r   r   r   r   r   unparse_address#   s    r   c             C   s   t t|  S )z
    Canonicalize address, adding a default scheme if necessary.

    >>> normalize_address('tls://[::1]')
    'tls://[::1]'
    >>> normalize_address('[::1]')
    'tcp://[::1]'
    )r   r   )r   r   r   r   normalize_address-   s    	r   c                s   t  tr S  fdd} fdd} dr~ dd d\}}}|sT|  |s`| }q|d	sp|  |dd }n* d	\}}}|s| }nd	|kr|  |t|fS )
zB
    Parse an endpoint address given in the form "host:port".
    c                  s   t d f d S )Nzinvalid address %r)r   r   )addressr   r   _fail@   s    zparse_host_port.<locals>._failc                  s   d krt d f S )Nz!missing port number in address %r)r   r   )r   default_portr   r   _defaultC   s    z!parse_host_port.<locals>._default[r   N]:)r   tuple
startswith	partitionint)r   r   r   r   hostr   tailportr   )r   r   r   parse_host_port9   s&    


r(   c             C   s2   d| kr|  dsd|  } |r*d| |f S | S dS )z!
    Undo parse_host_port().
    r    r   z[%s]z%s:%sN)r"   )r%   r'   r   r   r   unparse_host_port_   s
    r)   c             C   sL   t | |d\}}t|}y
||S  tk
rF   td| f Y nX dS )a  
    Get a (host, port) tuple out of the given address.
    For definition of strict check parse_address
    ValueError is raised if the address scheme doesn't allow extracting
    the requested information.

    >>> get_address_host_port('tcp://1.2.3.4:80')
    ('1.2.3.4', 80)
    )r   z6don't know how to extract host and port for address %rN)r   r   get_backendget_address_host_portNotImplementedErrorr   )r   r   r   r   backendr   r   r   r+   k   s    


r+   c             C   s    t | \}}t|}||S )a  
    Return a hostname / IP address identifying the machine this address
    is located on.

    In contrast to get_address_host_port(), this function should always
    succeed for well-formed addresses.

    >>> get_address_host('tcp://1.2.3.4:80')
    '1.2.3.4'
    )r   r   r*   get_address_host)r   r   r   r-   r   r   r   r.   ~   s    
r.   c             C   s&   t | \}}t|}t|||S )ah  
    Get a local listening address suitable for reaching *addr*.

    For instance, trying to reach an external TCP address will return
    a local TCP address that's routable to that external address.

    >>> get_local_address_for('tcp://8.8.8.8:1234')
    'tcp://192.168.1.68'
    >>> get_local_address_for('tcp://127.0.0.1:1234')
    'tcp://127.0.0.1'
    )r   r   r*   r   get_local_address_for)r   r   r   r-   r   r   r   r/      s    
r/   c             C   s&   t | \}}t|}t|||S )a  
    Apply scheme-specific address resolution to *addr*, replacing
    all symbolic references with concrete location specifiers.

    In practice, this can mean hostnames are resolved to IP addresses.

    >>> resolve_address('tcp://localhost:8786')
    'tcp://127.0.0.1:8786'
    )r   r   r*   r   resolve_address)r   r   r   r-   r   r   r   r0      s    

r0   )F)N)N)F)Z
__future__r   r   r   r   Zdask r   Zconfiggetr   r   r   r   r(   r)   r+   r.   r/   r0   r   r   r   r   <module>   s   


&

