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 )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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 )	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 ];}|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rdd |D }d| }|sqs| dr|d dkr|dd}|dkrt	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*   (	 