U
    ÀïõS$  ã                   @   sh   d Z dZdZddlZddlZddlZe d¡ZG dd„ deƒZ	G dd	„ d	eƒZ
d
d„ Zedkrdeƒ  dS )zHandling configuration files.z%Sebastian Heinlein <devel@glatzor.de>)ÚConfigWriteré    NzAptDaemon.ConfigWriterc                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚValuea¾  Represents a value with position information.

    .. attribute:: string
        The value string.

    .. attribute:: line
        The line number of the configuration file in which the value is set.

    .. attribute:: start
        The position in the line at which the value starts.

    .. attribute:: end
        The position in the line at which the value ends.

    .. attribute:: quotes
        The outer qoutes of the value: ' or "
    c                 C   s"   d| _ || _d | _|| _|| _d S )NÚ )ÚstringÚstartÚendÚlineÚquotes)Úselfr   r   r	   © r   ú2/usr/lib/python3/dist-packages/aptdaemon/config.pyÚ__init__4   s
    zValue.__init__c                 C   s
   | j |kS )N)r   )r
   Úotherr   r   r   Ú__cmp__;   s    zValue.__cmp__c                 C   s   d| j | j| j| jf S )NzValue: '%s' (line %s: %s to %s))r   r   r   r   )r
   r   r   r   Ú__repr__>   s    
 ÿzValue.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r       s   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   z!Modifies apt configuration files.c                 C   sÊ  i }d}d}d}g }d}d}g }	d}
d}t |ƒD ]’\}}t |ƒD ]~\}}|sn|dkrn|dkrnd}d}qBnP|rœ|dkrœ|dkrœd}d}|dd… }qBn"|r¤n|sØ|dkr¸|dksÀ|d	krØd}|dd… } q0næ|d
kr.|r|j|kr||_| }n(|st|||ƒ}| }n| j|7  _n|rF| j|7  _nx|rz|dkrz|dkrz| |dd… ¡ d}nD| ¡ sŽ|dkrž|| ¡ 7 }n |dkrF|
r¾|	 |¡ d}qB|	rìt 	dd 
|¡|	¡ |	|d 
|¡< g }	n<|rt 	dd 
|¡|¡ ||d 
|¡< nt 	dd 
|¡¡ d}|dkr@| ¡  ng }nx|dkr^|d8 }d}
n`|dkrŠ|d7 }|r„| |¡ d}d}
n4|dkr®|r¾| |¡ d}d}
ntd||f ƒ‚|}qBq0|S )zÀParse an ISC based apt configuration.

        :param lines: The list of lines of a configuration file.

        :returns: Dictionary of key, values found in the parsed configuration.
        FNr   Tr   Ú*ú/éÿÿÿÿú#z'"ú:z/-:._+ú;zFound %s "%s"z::zSkipping empty key %sú}é   ú{z	
 zUnknown char '%s' in line: '%s')Ú	enumerater	   r   r   r   ÚappendÚisalphaÚlowerÚlogÚdebugÚjoinÚpopÚ
ValueError)r
   ÚlinesZoptionsZ
in_commentZin_valueZ	prev_charÚoptionÚvalueZoption_nameZ
value_listZin_bracketsÚlevelZline_nor   Zchar_noÚcharr   r   r   ÚparseG   s¤    ÿ


ÿ






ÿzConfigWriter.parsec              
   C   s  |dkrd}n|dkrd}nt |ƒ}tj tj d¡tj d¡¡}t |¡D ]Z}||k r\qLttj ||¡ƒ}| 	¡ }W 5 Q R X |  
|¡}z|| ¡  }	W n4 tk
rÐ   ||krÈ| d||f ¡ nY qLY n¢X |dkrè|	j ¡ dksL|dkr |	j ¡ d	ksLt |ƒ|	jkrqL||kr^||	j }
|
d
|	jd … }||7 }||
|	jd
… 7 }|||	j< nd||	j  ||	j< ttj ||¡dƒ}t d|¡ | |¡ W 5 Q R X qLtj tj ||¡¡sttj ||¡dƒ(}t d|¡ d||f }
| |
¡ W 5 Q R X d
S )a  Change the value of an option in the configuration.

        :param option: The name of the option, e.g.
            'apt::periodic::AutoCleanInterval'.
        :param value: The value of the option. Will be converted to string.
        :param defaultfile: The filename of the ``/etc/apt/apt.conf.d``
            configuration snippet in which the option should be set.
            If the value is overriden by a later configuration file snippet
            it will be disabled in the corresponding configuration file.
        TÚtrueFZfalsezDir::EtczDir::Etc::Partsz	%s '%s';
)ZyesÚwithZonÚenable)ZnoZwithoutZoffÚdisableNr   z// %sÚwzWritting %s)ÚstrÚosÚpathr$   Úapt_pkgÚconfigZfind_dirÚlistdirÚopenÚ	readlinesr,   r!   ÚKeyErrorr   r   r   r   r   r"   r#   Ú
writelinesÚexistsÚwrite)r
   r(   r)   ZdefaultfileZ	etc_partsÚfilenameÚfdr'   r6   Úvalr   Znew_liner   r   r   Ú	set_value¦   sZ    
ÿ

ÿýüú

zConfigWriter.set_valueN)r   r   r   r   r,   rA   r   r   r   r   r   C   s   _r   c                  C   sR   t  ¡  tƒ } tt d¡ƒD ]}td| ƒ ¡ }|  |¡ qt	|  
ddd¡ƒ d S )Nz/etc/apt/apt.conf.d/z/etc/apt/apt.conf.d/%sz	huhu::abcZlumpiZ	10glatzor)r5   Zinit_configr   Úsortedr3   r7   r8   r9   r,   ÚprintrA   )Zcwr>   r'   r   r   r   Úmainè   s    rD   Ú__main__)r   Ú
__author__Ú__all__Zloggingr3   r5   Z	getLoggerr"   Úobjectr   r   rD   r   r   r   r   r   Ú<module>   s   
# &