a
    |P_%                     @   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yR   Y n0 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                   @   s   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   :   s   r   c                   @   s   e Zd ZdZdS )WatchFileFormatErrorz(Raised when the input is not valid.
    Nr   r   r   r   r   r   >   s   r   c                 C   s6   t t }||d< | D ]\}}| ||} q| S )zApply 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packageZ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 )	WatchFilezA 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 ]v}|jr`|||jd  ||j |jr|d|j  |j r|d|j   |jr|d|j  |d qBd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%   fr2   entryr   r   r   dumpu   s     


zWatchFile.dumpFc              	   C   s  g }g }|D ]T}| drq| s&q|ddrH||d q|| || g }q|rztd| || |sdS d|d}z|d	d
\}}W n t	y   t
 Y n0 | dkrt
 t| }	g }
g }|D ]}|	dkrdd |D }d| }|sq| dr|d dkrx|dd}|dkrZt	d| |d| }||d
 d }nBz|dd dd
\}}W n$ t	y   |dd }d}Y n0 |d}ng }|r|z|dd
\}}W n t	y    |}d}Y n0 td|}|rNt|d ft|dd
 }|dt|d  d
  }nt|dd}|t|g|R d|i q|
| q| ||
|	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
        #r3   \z
\z*watchfile ended with \; skipping last lineN r   =   r"      c                 S   s   g | ]}|  qS r   )lstrip).0chunkr   r   r   
<listcomp>       z(WatchFile.from_lines.<locals>.<listcomp>r.      r-      zNot matching " in %rr*   z/([^/]*\([^/]*\)[^/]*)$   r0   )r#   r$   r"   )
startswithstriprstripendswithappendr    r/   popsplit
ValueErrorr   intindexrefindallstrtuplelenWatchextend)clslinesr   Zjoined_lines	continuedline	firstlinekeyvaluer"   Zpersistent_optionsr#   ZchunkedZoptendZopts_strr0   r5   mpartsr   r   r   
from_lines   s~    










zWatchFile.from_lines)F)
r   r   r   r   DEFAULT_VERSIONr&   r)   r:   classmethodrd   r   r   r   r   r!   [   s   	
 r!   c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
rY   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   )r5   r6   r"   r7   r$   )r%   r5   r6   r"   r7   r0   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   r5   r6   r"   r7   r$   r(   r   r   r   __repr__  s
    zWatch.__repr__c                 C   sJ   t |tsdS |j| jkoH|j| jkoH|j| jkoH|j| jkoH|j| jkS )NF)
isinstancerY   r5   r6   r"   r7   r$   )r%   otherr   r   r   __eq__	  s    




zWatch.__eq__)NNNN)r   r   r   r   r&   rh   rk   r   r   r   r   rY      s       
rY   )r   rT   warningsr   typingr   r   r   r   r   r   r	   ImportErrorre   r   	Exceptionr   rQ   r   r   r    objectr!   rY   r   r   r   r   <module>   s&   (	 