o
    ¦®Øf¤
  ã                   @   s*   d dl mZ d dlmZ G dd„ dƒZdS )é    )ÚStringIO)Úchop_commentc                   @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )Ú	HostsConfc                 C   s   || _ d | _d S ©N)Ú_textÚ	_contents)ÚselfÚtext© r
   úA/usr/lib/python3/dist-packages/cloudinit/distros/parsers/hosts.pyÚ__init__   s   
zHostsConf.__init__c                 C   s    | j d u r|  | j¡| _ d S d S r   )r   Ú_parser   )r   r
   r
   r   Úparse   s   
ÿzHostsConf.parsec                 C   sV   |   ¡  g }| jD ]\}}|dkr(|\}}t|ƒr(|d |kr(| |dd … ¡ q	|S )NÚoptionr   é   )r   r   ÚlenÚappend)r   ÚipÚoptionsÚ	line_typeÚ
componentsÚpiecesÚ_tailr
   r
   r   Ú	get_entry   s   €zHostsConf.get_entryc                 C   sv   |   ¡  g }| jD ],\}}|dkr| ||f¡ q	|\}}t|ƒr(|d |kr(q	t|ƒr5| |t|ƒf¡ q	|| _d S )Nr   r   )r   r   r   r   Úlist)r   r   Ú	n_entriesr   r   r   r   r
   r
   r   Údel_entries"   s   €
zHostsConf.del_entriesc                 G   s,   |   ¡  | j d||gt|ƒ dff¡ d S )Nr   Ú )r   r   r   r   )r   r   Úcanonical_hostnameÚaliasesr
   r
   r   Ú	add_entry1   s   ÿzHostsConf.add_entryc                 C   sx   g }|  ¡ D ]3}t| ¡ ƒs| d|gf¡ qt| ¡ dƒ\}}t|ƒs-| d|gf¡ q| d| d ¡|gf¡ q|S )NÚblankú#Úall_commentr   )Ú
splitlinesr   Ústripr   r   Úsplit)r   ÚcontentsÚentriesÚlineÚheadÚtailr
   r
   r   r   7   s   zHostsConf._parsec                 C   s˜   |   ¡  tƒ }| jD ]=\}}|dkr| d|d  ¡ q
|dkr*| d|d  ¡ q
|dkrG|\}}dd„ |D ƒ}d |¡}| d	||f ¡ q
| ¡ S )
Nr!   z%s
r   r#   r   c                 S   s   g | ]}t |ƒ‘qS r
   )Ústr)Ú.0Úpr
   r
   r   Ú
<listcomp>N   s    z%HostsConf.__str__.<locals>.<listcomp>ú	z%s%s
)r   r   r   ÚwriteÚjoinÚgetvalue)r   r'   r   r   r   r+   r
   r
   r   Ú__str__D   s   
€zHostsConf.__str__N)
Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r    r   r4   r
   r
   r
   r   r      s    
r   N)Úior   Úcloudinit.distros.parsersr   r   r
   r
   r
   r   Ú<module>   s   