3
S$                 @   sh   d Z dZdZddlZddlZddlZej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}xt |D ]\}}xt |D ]~\}}| rx|dkrx|dkrxd}d}qJnN|r|dkr|dkrd}d}|dd }qJn |rn| r|dkr|dks|d	krd}|dd }P n|d
kr6|o|j|kr
||_| }n(|s$t|||}| }n| j|7  _n|rN| j|7  _nx|r|dkr|dkr|j|dd  d}nD|j s|dkr||j 7 }n |dkrN|
r|	j| d}qJ|	rtj	ddj
||	 |	|dj
|< g }	n<|rtj	ddj
|| ||dj
|< ntj	ddj
| d}|dkrH|j  ng }nx|dkrf|d8 }d}
n`|dkr|d7 }|r|j| d}d}
n4|dkr|r|j| d}d}
ntd||f |}qJW q4W |S )zParse 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'r   r   )	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jtjjdtjjd}xhtj|D ]X}||k r`qPttjj||}|j	 }W dQ R X | j
|}y||j  }	W n2 tk
r   ||kr|jd||f  nwPY nX |dkr|	jj dksP|dkr|	jj dksPt ||	jkrqP||kr`||	j }
|
d|	jd  }||7 }||
|	jd 7 }|||	j< nd||	j  ||	j< ttjj||d}tjd| |j| W dQ R X qPW tjjtjj||sttjj||d(}tjd| d||f }
|j|
 W d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::PartsNz	%s '%s';
yeswithonenablenowithoutoffdisabler   z// %swzWritting %s)r.   r/   r0   r1   )r2   r3   r4   r5   )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'   r;   valr   Znew_liner   r   r   	set_value   sT    



zConfigWriter.set_valueN)r   r   r   r   r,   rF   r   r   r   r   r   C   s   _c              C   sV   t j  t } x0ttjdD ]}td| j }| j| qW t	| j
ddd d S )Nz/etc/apt/apt.conf.d/z/etc/apt/apt.conf.d/%sz	huhu::abcZlumpiZ	10glatzor)r:   Zinit_configr   sortedr8   r<   r=   r>   r,   printrF   )ZcwrC   r'   r   r   r   main   s    rI   __main__)r   )r   
__author____all__Zloggingr8   r:   Z	getLoggerr"   objectr   r   rI   r   r   r   r   r   <module>   s   
# &