U
    ˆjÇ  ã                   @   sz   d Z ddlmZ ddl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	„ Zd
d„ ZG dd„ dejƒZdS )z
hosts(5) support.
é    )Údefer)ÚisIPAddressÚisIPv6Address)ÚcommonÚdns)Úfailure)ÚnativeString)ÚFilePathc              	   C   sÒ   g }z|   ¡  ¡ }W n tk
r,   | Y S X | ¡ }|D ]’}| d¡}|dkr\|d|… }|sbq:| ¡ }| ¡ dd„ |dd… D ƒkr:zt|d ƒ}W n tk
r°   Y q:Y nX t|ƒsÂt	|ƒr:| 
|¡ q:|S )a±  
    Search the given file, which is in hosts(5) standard format, for addresses
    associated with a given name.

    @param hostsFile: The name of the hosts(5)-format file to search.
    @type hostsFile: L{FilePath}

    @param name: The name to search for.
    @type name: C{bytes}

    @return: L{None} if the name is not found in the file, otherwise a
        C{str} giving the address in the file associated with the name.
    ó   #éÿÿÿÿNc                 S   s   g | ]}|  ¡ ‘qS © )Úlower)Ú.0Úsr   r   ú7/tmp/pip-unpacked-wheel-j5h_c8_q/twisted/names/hosts.pyÚ
<listcomp>/   s     z$searchFileForAll.<locals>.<listcomp>é   r   )Z
getContentÚ
splitlinesÚBaseExceptionr   ÚfindÚsplitr   Ú
ValueErrorr   r   Úappend)Z	hostsFileÚnameÚresultsÚlinesÚlineÚidxÚpartsZmaybeIPr   r   r   ÚsearchFileForAll   s*    


r   c                 C   s   t t| ƒ|ƒ}|r|d S dS )a²  
    Grep given file, which is in hosts(5) standard format, for an address
    entry with a given name.

    @param file: The name of the hosts(5)-format file to search.
    @type file: C{str} or C{bytes}

    @param name: The name to search for.
    @type name: C{bytes}

    @return: L{None} if the name is not found in the file, otherwise a
        C{str} giving the first address in the file associated with
        the name.
    r   N)r   r	   )Úfiler   Ú	addressesr   r   r   ÚsearchFileFor9   s    r"   c                   @   sJ   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zddd„Zddd„Z	eZ
dS )ÚResolverz9
    A resolver that services hosts(5) format files.
    ó
   /etc/hostsé  c                 C   s   t j | ¡ || _|| _d S ©N)r   ÚResolverBaseÚ__init__r    Úttl)Úselfr    r)   r   r   r   r(   S   s    zResolver.__init__c                    s$   t ‡ ‡fdd„ttˆjƒˆ ƒD ƒƒS )zv
        Return a tuple of L{dns.RRHeader} instances for all of the IPv4
        addresses in the hosts file.
        c                 3   s8   | ]0}t |ƒrt ˆ tjtjˆjt |ˆj¡¡V  qd S r&   )r   r   ÚRRHeaderÚAÚINr)   ZRecord_A©r   Úaddr©r   r*   r   r   Ú	<genexpr>]   s   þz%Resolver._aRecords.<locals>.<genexpr>©Útupler   r	   r    ©r*   r   r   r0   r   Ú	_aRecordsX   s    þzResolver._aRecordsc                    s$   t ‡ ‡fdd„ttˆjƒˆ ƒD ƒƒS )zv
        Return a tuple of L{dns.RRHeader} instances for all of the IPv6
        addresses in the hosts file.
        c                 3   s8   | ]0}t |ƒrt ˆ tjtjˆjt |ˆj¡¡V  qd S r&   )r   r   r+   ZAAAAr-   r)   ZRecord_AAAAr.   r0   r   r   r1   h   s   ü    ÿz(Resolver._aaaaRecords.<locals>.<genexpr>r2   r4   r   r0   r   Ú_aaaaRecordsc   s    üzResolver._aaaaRecordsc                 C   s*   |rt  |ddf¡S t  t t |¡¡¡S )aF  
        Generate a response for the given name containing the given result
        records, or a failure if there are no result records.

        @param name: The DNS name the response is for.
        @type name: C{str}

        @param records: A tuple of L{dns.RRHeader} instances giving the results
            that will go into the response.

        @return: A L{Deferred} which will fire with a three-tuple of result
            records, authority records, and additional records, or which will
            fail with L{dns.DomainError} if there are no result records.
        r   )r   ZsucceedZfailr   ZFailurer   ZDomainError)r*   r   Úrecordsr   r   r   Ú_respondp   s    zResolver._respondNc                 C   s   t  |¡}|  ||  |¡¡S )zm
        Read any IPv4 addresses from C{self.file} and return them as
        L{Record_A} instances.
        )r   ÚdomainStringr8   r5   ©r*   r   Útimeoutr   r   r   ÚlookupAddressƒ   s    
zResolver.lookupAddressc                 C   s   t  |¡}|  ||  |¡¡S )zp
        Read any IPv6 addresses from C{self.file} and return them as
        L{Record_AAAA} instances.
        )r   r9   r8   r6   r:   r   r   r   ÚlookupIPV6Address‹   s    
zResolver.lookupIPV6Address)r$   r%   )N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r5   r6   r8   r<   r=   ZlookupAllRecordsr   r   r   r   r#   N   s   


r#   N)rA   Ztwisted.internetr   Ztwisted.internet.abstractr   r   Ztwisted.namesr   r   Ztwisted.pythonr   Ztwisted.python.compatr   Ztwisted.python.filepathr	   r   r"   r'   r#   r   r   r   r   Ú<module>   s   '