o
    …Úãa%  ã                   @   sº   d Z ddlZddlmZ zddlmZmZmZmZm	Z	m
Z
mZ W n	 ey)   Y nw dZdddd	d
œZG dd„ deƒZG dd„ deƒZdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZdS )z'Functions for working with watch files.é    N)Úwarn)ÚIterableÚIteratorÚListÚOptionalÚSequenceÚTextIOÚTupleé   z[-_]?(\d[\-+\.:\~\da-zA-Z]*)z2(?i)\.(?:tar\.xz|tar\.bz2|tar\.gz|zip|tgz|tbz|txz)zL(?i)\.(?:tar\.xz|tar\.bz2|tar\.gz|zip|tgz|tbz|txz)\.(?:asc|pgp|gpg|sig|sign)z%[\+~](debian|dfsg|ds|deb)(\.)?(\d+)?$)z@ANY_VERSION@z@ARCHIVE_EXT@z@SIGNATURE_EXT@z	@DEB_EXT@c                   @   ó   e Zd ZdZdS )ÚMissingVersionzThe version= line is missing.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú./usr/lib/python3/dist-packages/debian/watch.pyr   :   ó    r   c                   @   r   )ÚWatchFileFormatErrorz(Raised when the input is not valid.
    Nr   r   r   r   r   r   >   r   r   c                 C   s6   t t ¡ ƒ}||d< | ¡ D ]
\}}|  ||¡} q| S )z°Apply substitutions to a string.

    :param text: text to apply substitutions to
    :param package: package name, as a string
    :return: text with subsitutions applied
    z	@PACKAGE@)ÚdictÚSUBSTITUTIONSÚitemsÚreplace)ÚtextÚpackageÚsubstsÚkÚvr   r   r   ÚexpandC   s
   r   c                 C   s   |rt | ƒ‚t| ƒ d S ©N)r   r   )ÚmsgÚstrictr   r   r   Ú	_complainT   s   r#   c                   @   s>   e Zd ZdZddefdd„Zdd„ Zdd„ Zedd
d„ƒZ	dS )Ú	WatchFilezÂA Debian watch file.

    :ivar entries: list of Watch entries
    :ivar options: optional list of global options, applied to all Watch
        entries
    :ivar version: watch file version
    Nc                 C   s.   || _ |d u r	g }|| _|d u rg }|| _d S r    )ÚversionÚentriesÚoptions)Úselfr&   r'   r%   r   r   r   Ú__init__d   s   
zWatchFile.__init__c                 C   s
   t | jƒS r    )Úiterr&   ©r(   r   r   r   Ú__iter__q   s   
zWatchFile.__iter__c                 C   s¾   dd„ }| j dur| d| j  ¡ | jr| || jƒd ¡ | jD ];}|jr0| ||jƒd ¡ | |j¡ |jrA| d|j ¡ |j rL| d|j  ¡ |jrW| d|j ¡ | d¡ q!dS )aO  Write the contents of a watch file to a file-like object.

        Note that this will not preserve the formatting of the original file,
        and thus it is currently not possible to use this function to
        parse and reserialize a file and end up with the same contents.

        :param f: File-like object to write to
        c                 S   s.   d  | ¡}d|v sd|v rd| d S d| S )Nú,ú ú	zopts="ú"úopts=)Újoin)ÚoptsÚsr   r   r   Úserialize_options   s   
z)WatchFile.dump.<locals>.serialize_optionsNzversion=%d
Ú
r.   )r%   Úwriter'   r&   ÚurlÚmatching_patternÚscript)r(   Úfr5   Úentryr   r   r   Údumpu   s"   


özWatchFile.dumpFc              	   C   s‚  g }g }|D ]*}|  d¡rq| ¡ sq| d¡ d¡r$| | d¡¡ q| |¡ | |¡ g }q|r=td|ƒ | |¡ |sAdS d | d¡¡}z
| d	d
¡\}}W n
 t	y]   t
ƒ ‚w | ¡ dkrgt
ƒ ‚t| ¡ ƒ}	g }
g }|D ]Æ}|	dkr€dd„ |D ƒ}d |¡ ¡ }|sŠqs|  d¡rÚ|d dkr´| dd¡}|dkr¥t	d| ƒ‚|d|… }||d
 d… }n z|dd…  dd
¡\}}W n t	yÓ   |dd… }d}Y nw | d¡}ng }|�r4z
| dd
¡\}}W n t	yö   |}d}Y nw t d|¡}|�rt|d ƒft| dd
¡ƒ }|dt|d ƒ d
 … }nt| dd¡ƒ}| t|g|¢R d|iŽ¡ qs|
 |¡ qs| ||
|	d�S )a@  Parse from the contents that make up a watch file.

        :param lines: watch file lines to parse
        :return: instance or None if there are no non-comment lines in the file
        :raise MissingVersion: if there is no version number declared
        :raise ValueError: when syntax errors are encountered
        ú#r6   ú\z
\z*watchfile ended with \; skipping last lineNÚ r   ú=é   r%   é   c                 S   s   g | ]}|  ¡ ‘qS r   )Úlstrip)Ú.0Úchunkr   r   r   Ú
<listcomp>À   s    z(WatchFile.from_lines.<locals>.<listcomp>r1   é   r0   é   éÿÿÿÿzNot matching " in %rr-   z/([^/]*\([^/]*\)[^/]*)$é   r3   )r&   r'   r%   )Ú
startswithÚstripÚrstripÚendswithÚappendr#   r2   ÚpopÚsplitÚ
ValueErrorr   ÚintÚindexÚreÚfindallÚstrÚtupleÚlenÚWatchÚextend)ÚclsÚlinesr"   Újoined_linesÚ	continuedÚlineÚ	firstlineÚkeyÚvaluer%   Úpersistent_optionsr&   ÚchunkedÚoptendÚopts_strr3   r8   ÚmÚpartsr   r   r   Ú
from_lines•   s„   





ÿ
þþÿzWatchFile.from_lines)F)
r   r   r   r   ÚDEFAULT_VERSIONr)   r,   r=   Úclassmethodrk   r   r   r   r   r$   [   s    	
ý r$   c                   @   s2   e Zd ZdZ				d	dd„Zdd„ Zdd„ ZdS )
r[   aO  Watch line entry.

    This will contain the attributes documented in uscan(1):

    :ivar url: The URL (possibly including the filename regex)
    :ivar matching_pattern: a filename regex, optional
    :ivar version: version policy, optional
    :ivar script: script to run, optional
    :ivar opts: a list of options, as strings
    Nc                 C   s.   || _ || _|| _|| _|d u rg }|| _d S r    )r8   r9   r%   r:   r'   )r(   r8   r9   r%   r:   r3   r   r   r   r)   ó   s   
zWatch.__init__c                 C   s"   d| j j| j| j| j| j| jf S )Nz;%s(%r, matching_pattern=%r, version=%r, script=%r, opts=%r))Ú	__class__r   r8   r9   r%   r:   r'   r+   r   r   r   Ú__repr__  s
   þÿzWatch.__repr__c                 C   sJ   t |tƒsdS |j| jko$|j| jko$|j| jko$|j| jko$|j| jkS )NF)Ú
isinstancer[   r8   r9   r%   r:   r'   )r(   Úotherr   r   r   Ú__eq__	  s   

ÿ
þ
ý
üzWatch.__eq__)NNNN)r   r   r   r   r)   ro   rr   r   r   r   r   r[   ç   s    
ûr[   )r   rV   Úwarningsr   Útypingr   r   r   r   r   r   r	   ÚImportErrorrl   r   Ú	Exceptionr   rS   r   r   r#   Úobjectr$   r[   r   r   r   r   Ú<module>   s*   (	þô 