o
    u]                     @   sN   d Z ddlZddlZddlZddlmZmZmZ d	ddZ	G dd deZ
dS )
z
Parsing for `TorDNSEL <https://www.torproject.org/projects/tordnsel.html.en>`_
exit list files.

::

  TorDNSEL - Exit list provided by TorDNSEL
    N)
Descriptor_read_until_keywords_descriptor_componentsFc                 k   sR    t d| dd 	 t d| }|t d| 7 }|r&ttd||fi |V  ndS q	)a  
  Iterates over a tordnsel file.

  :returns: iterator for :class:`~stem.descriptor.tordnsel.TorDNSEL`
    instances in the file

  :raises:
    * **ValueError** if the contents is malformed and validate is **True**
    * **IOError** if the file can't be read
  ExitNodeT)skipExitAddress    N)r   TorDNSELbytesjoin)Ztordnsel_filevalidatekwargscontents r   :/usr/lib/python3/dist-packages/stem/descriptor/tordnsel.py_parse_file   s   
r   c                       s,   e Zd ZdZdZ fddZdd Z  ZS )r	   ad  
  TorDNSEL descriptor (`exitlist specification
  <https://www.torproject.org/tordnsel/exitlist-spec.txt>`_)

  :var str fingerprint: **\*** authority's fingerprint
  :var datetime published: **\*** time in UTC when this descriptor was made
  :var datetime last_status: **\*** time in UTC when the relay was seen in a v2 network status
  :var list exit_addresses: **\*** list of (str address, datetime date) tuples consisting of the found IPv4 exit address and the time

  **\*** attribute is either required when we're parsed with validation or has
  a default value, others are left as **None** if undefined
  Ztordnselc                    sP   t t| | tjj|}t||}d | _d | _	d | _
g | _| || d S )N)superr	   __init__stemutil	str_toolsZ_to_unicoder   fingerprint	publishedlast_statusexit_addresses_parse)selfZraw_contentsr   entries	__class__r   r   r   A   s   
zTorDNSEL.__init__c           
   
   C   s  t | D ]\}}|d \}}}|r|rtd| |dkr2|r.tjj|s.td| || _q|dkrRz
tjj	|| _
W q tyQ   |rOtd| Y qw |dkrrz
tjj	|| _W q tyq   |rotd| Y qw |d	kr|D ]F\}}}|d
d\}}	|rtjj|std| |rtd| ztjj	|	}	| j||	f W qx ty   |rtd| Y qxw q|rtd| qd S )Nr   zUnexpected block content: %sr   z6Tor relay fingerprints consist of forty hex digits: %sZ	Publishedz"Published time wasn't parsable: %sZ
LastStatusz#LastStatus time wasn't parsable: %sr       z*ExitAddress isn't a valid IPv4 address: %sz*ExitAddress found time wasn't parsable: %szUnrecognized keyword: %s)listitems
ValueErrorr   r   Z	tor_toolsZis_valid_fingerprintr   r   Z_parse_timestampr   r   splitZ
connectionZis_valid_ipv4_addressr   append)
r   r   r   keywordvaluesvalueZ
block_typeZblock_contentZaddressdater   r   r   r   M   sZ   zTorDNSEL._parse)__name__
__module____qualname____doc__ZTYPE_ANNOTATION_NAMEr   r   __classcell__r   r   r   r   r	   1   s
    r	   )F)r.   Zstem.util.connectionr   Zstem.util.str_toolsZstem.util.tor_toolsZstem.descriptorr   r   r   r   r	   r   r   r   r   <module>   s   	
