U
    L¬÷dÁ
  ã                   @   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 kr|  | j¡| _ 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   ÚipZoptionsÚ	line_typeÚ
componentsÚpiecesÚ_tailr
   r
   r   Ú	get_entry   s    zHostsConf.get_entryc                 C   sx   |   ¡  g }| jD ]Z\}}|dkr4| ||f¡ qq|\}}t|ƒrR|d |krRqt|ƒr| |t|ƒf¡ q|| _d S )Nr   r   )r   r   r   r   Úlist)r   r   Z	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   Zcanonical_hostnameÚaliasesr
   r
   r   Ú	add_entry1   s    ÿzHostsConf.add_entryc                 C   sx   g }|  ¡ D ]f}t| ¡ ƒs.| d|gf¡ qt| ¡ dƒ\}}t|ƒsZ| 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 ]z\}}|dkr8| d|d  ¡ q|dkrT| d|d  ¡ q|dkr|\}}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   r1   r
   r
   r
   r   r      s   
r   N)Úior   Zcloudinit.distros.parsersr   r   r
   r
   r
   r   Ú<module>   s   