
    Ϫf                         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 Zd	 Z G d
 dej"                        Zy)z
hosts(5) support.
    )defer)isIPAddressisIPv6Address)commondns)failure)nativeString)FilePathc                    g }	 | j                         j                         }|j                         }|D ]  }|j	                  d      }|dk7  r|d| }|s!|j                         }|j                         |dd D cg c]  }|j                          c}v sc	 t        |d         }t        |      st        |      s|j                  |        |S # t        $ r |cY S w xY wc c}w # t        $ r Y w xY w)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.
       #N   r   )
getContent
splitlinesBaseExceptionlowerfindsplitr	   
ValueErrorr   r   append)		hostsFilenameresultslineslineidxpartssmaybeIPs	            5/usr/lib/python3/dist-packages/twisted/names/hosts.pysearchFileForAllr!      s     G$$&113 ::<D (iio"9:D

::<uQRy9!AGGI99&uQx0 7#}W'=w'( N'   :  s)   C ;C#C(C C (	C43C4c                 <    t        t        |       |      }|r|d   S y)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   	addressess      r    searchFileForr%   9   s$     !$6I|    c                   >    e Zd ZdZd	dZd Zd Zd Zd
dZd
dZ	eZ
y)Resolverz9
    A resolver that services hosts(5) format files.
    c                 ^    t         j                  j                  |        || _        || _        y N)r   ResolverBase__init__r#   ttl)selfr#   r-   s      r    r,   zResolver.__init__S   s$    $$T*	r&   c                 j     t         f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      K   | ]l  }t        |      r_t        j                  t        j                  t        j                  j
                  t        j                  |j
                               n y wr*   )r   r   RRHeaderAINr-   Record_A.0addrr   r.   s     r    	<genexpr>z%Resolver._aRecords.<locals>.<genexpr>]   sM      
4  LLsuucffdhhT4888TU
   A2A5tupler!   r
   r#   r.   r   s   ``r    	_aRecordszResolver._aRecordsX   s/    
  
($)))<dC
 
 	
r&   c                 j     t         f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      K   | ]l  }t        |      r_t        j                  t        j                  t        j                  j
                  t        j                  |j
                               n y wr*   )r   r   r1   AAAAr3   r-   Record_AAAAr5   s     r    r8   z(Resolver._aaaaRecords.<locals>.<genexpr>h   sR      
 T"	 LLchh#//$2Q
r9   r:   r<   s   ``r    _aaaaRecordszResolver._aaaaRecordsc   s1    
  
 )$)))<dC	
 
 	
r&   c                     |rt        j                  |ddf      S t        j                  t        j                  t        j                  |                  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   succeedfailr   Failurer   DomainError)r.   r   recordss      r    _respondzResolver._respondp   s>     =='2r!233zz'//#//$*?@AAr&   Nc                 n    t        j                  |      }| j                  || j                  |            S )zm
        Read any IPv4 addresses from C{self.file} and return them as
        L{Record_A} instances.
        )r   domainStringrJ   r=   r.   r   timeouts      r    lookupAddresszResolver.lookupAddress   s.    
 %}}T4>>$#788r&   c                 n    t        j                  |      }| j                  || j                  |            S )zp
        Read any IPv6 addresses from C{self.file} and return them as
        L{Record_AAAA} instances.
        )r   rL   rJ   rB   rM   s      r    lookupIPV6AddresszResolver.lookupIPV6Address   s0    
 %}}T4#4#4T#:;;r&   )s
   /etc/hostsi  r*   )__name__
__module____qualname____doc__r,   r=   rB   rJ   rO   rQ   lookupAllRecordsrD   r&   r    r(   r(   N   s.    
	

B&9< %r&   r(   N)rU   twisted.internetr   twisted.internet.abstractr   r   twisted.namesr   r   twisted.pythonr   twisted.python.compatr	   twisted.python.filepathr
   r!   r%   r+   r(   rD   r&   r    <module>r]      s=   

 # @ % " . ,$N*I%v"" I%r&   