U
    ¹êW[©  ã                   @   sŽ   d 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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.
é    )ÚdivisionÚabsolute_import)ÚnativeString)Údns)Úfailure)ÚFilePath)Údefer)ÚisIPAddress)Úcommonc                 C   s˜   g }z|   ¡  ¡ }W n   | Y S X | ¡ }|D ]`}| d¡}|dkrT|d|… }|sZq2| ¡ }| ¡ dd„ |dd… D ƒkr2| t|d ƒ¡ q2|S )a«  
    Search the given file, which is in hosts(5) standard format, for an address
    entry 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{str}

    @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   ú5/usr/lib/python3/dist-packages/twisted/names/hosts.pyÚ
<listcomp>1   s     z$searchFileForAll.<locals>.<listcomp>é   r   )Z
getContentÚ
splitlinesr   ÚfindÚsplitÚappendr   )Z	hostsFileÚnameZresultsÚlinesÚlineÚidxÚpartsr   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.

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

    @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.
    r   N)r   r   )Úfiler   Z	addressesr   r   r   ÚsearchFileFor7   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$   O   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                    s6   g | ].}t |ƒrt ˆ tjtjˆjt |ˆj¡¡‘qS r   )r	   r   ÚRRHeaderÚAÚINr%   ZRecord_A©r   Zaddr©r   r&   r   r   r   Z   s
   üÿz&Resolver._aRecords.<locals>.<listcomp>©Útupler   r   r   ©r&   r   r   r+   r   Ú	_aRecordsU   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                    s6   g | ].}t |ƒst ˆ tjtjˆjt |ˆj¡¡‘qS r   )r	   r   r'   ZAAAAr)   r%   ZRecord_AAAAr*   r+   r   r   r   g   s
   üÿz)Resolver._aaaaRecords.<locals>.<listcomp>r,   r.   r   r+   r   Ú_aaaaRecordsb   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   Zrecordsr   r   r   Ú_respondo   s    zResolver._respondNc                 C   s   |   ||  |¡¡S )zm
        Read any IPv4 addresses from C{self.file} and return them as L{Record_A}
        instances.
        )r1   r/   ©r&   r   Ztimeoutr   r   r   ÚlookupAddressƒ   s    zResolver.lookupAddressc                 C   s   |   ||  |¡¡S )zp
        Read any IPv6 addresses from C{self.file} and return them as
        L{Record_AAAA} instances.
        )r1   r0   r2   r   r   r   ÚlookupIPV6Address‹   s    zResolver.lookupIPV6Address)r!   r"   )N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r/   r0   r1   r3   r4   ZlookupAllRecordsr   r   r   r   r    K   s   



r    N)r8   Z
__future__r   r   Ztwisted.python.compatr   Ztwisted.namesr   Ztwisted.pythonr   Ztwisted.python.filepathr   Ztwisted.internetr   Ztwisted.internet.abstractr	   r
   r   r   r#   r    r   r   r   r   Ú<module>   s   #