a
    ˜ëjã  ã                	   @   sº   d Z ddlZddlmZmZmZ ddlmZ dZde› de› d	e› d	e› d
�	Z	G dd„ dƒZ
e
dƒZeeee
df  eeee
f  dœdd„Zeee
eeef eeeef ee f ZdS )zö
Representation and utils for ranges of PDF file pages.

Copyright (c) 2014, Steve Witham <switham_github@mac-guyver.com>.
All rights reserved. This software is available under a BSD license;
see https://github.com/py-pdf/pypdf/blob/main/LICENSE
é    N)ÚAnyÚOptionalÚUnioné   )Ú
ParseErrorz(0|-?[1-9]\d*)z^(z|(z?(:z?)?)))$c                   @   s°   e Zd ZdZeed ef ddœdd„Zee	e
dœdd„ƒZed	œd
d„Zed	œdd„Zed	œdd„Zeeeeef dœdd„Zee
dœdd„Zed	œdd„Zd d dœdd„ZdS )Ú	PageRangeaJ  
    A slice-like representation of a range of page indices.

    For example, page numbers, only starting at zero.

    The syntax is like what you would put between brackets [ ].
    The slice is one of the few Python types that can't be subclassed,
    but this class converts to and from slices, and allows similar use.

      -  PageRange(str) parses a string representing a page range.
      -  PageRange(slice) directly "imports" a slice.
      -  to_slice() gives the equivalent slice.
      -  str() and repr() allow printing.
      -  indices(n) is like slice.indices(n).
    N)ÚargÚreturnc                 C   sÆ   t |tƒr|| _dS t |tƒr,| ¡ | _dS t |tƒo@t t|¡}|sNt	|ƒ‚| 
d¡rˆt| 
d¡ƒ}|dkrv|d nd}t||ƒ| _n:dd„ | 
ddd	¡D ƒ}|d }|d
kr¸t	|ƒ‚t|Ž | _dS )aÓ  
        Initialize with either a slice -- giving the equivalent page range,
        or a PageRange object -- making a copy,
        or a string like
            "int", "[int]:[int]" or "[int]:[int]:[int]",
            where the brackets indicate optional ints.
        Remember, page indices start with zero.
        Page range expression examples:

            :     all pages.                   -1    last page.
            22    just the 23rd page.          :-1   all but the last page.
            0:3   the first three pages.       -2    second-to-last page.
            :3    the first three pages.       -2:   last two pages.
            5:    from the sixth page onward.  -3:-1 third & second to last.
        The third, "stride" or "step" number is also recognized.
            ::2       0 2 4 ... to the end.    3:0:-1    3 2 1 but not 0.
            1:10:2    1 3 5 7 9                2::-1     2 1 0.
            ::-1      all pages in reverse order.
        Note the difference between this notation and arguments to slice():
            slice(3) means the first three pages;
            PageRange("3") means the range of only the fourth page.
            However PageRange(slice(3)) means the first three pages.
        Né   éÿÿÿÿr   c                 S   s   g | ]}|rt |ƒnd ‘qS ©N)Úint)Ú.0Úg© r   úN/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pypdf/pagerange.pyÚ
<listcomp>M   ó    z&PageRange.__init__.<locals>.<listcomp>é   é   é   r   )Ú
isinstanceÚsliceÚ_slicer   Úto_sliceÚstrÚreÚmatchÚPAGE_RANGE_REr   Úgroupr   )Úselfr   ÚmÚstartÚstopZboundsÚstepr   r   r   Ú__init__$   s$    



zPageRange.__init__)Úinputr	   c                 C   sF   t | ttfƒrdS t | tƒs dS zt| ƒ W n ty@   Y dS 0 dS )zè
        True if input is a valid initializer for a PageRange.

        Args:
            input: A possible PageRange string or a PageRange object.

        Returns:
            True, if the ``input`` is a valid PageRange.

        TF)r   r   r   r   r   )r&   r   r   r   ÚvalidU   s    
zPageRange.valid)r	   c                 C   s   | j S )z/Return the slice equivalent of this page range.)r   ©r    r   r   r   r   k   s    zPageRange.to_slicec                 C   sf   | j }|jdu rB|jdur4|j|jd kr4t|jƒS |j|jf}n|j|j|jf}d dd„ |D ƒ¡S )zA string like "1:2:3".Nr   ú:c                 s   s"   | ]}|d u rdnt |ƒV  qd S )NÚ )r   )r   Úir   r   r   Ú	<genexpr>z   r   z$PageRange.__str__.<locals>.<genexpr>)r   r$   r"   r#   r   Újoin)r    ÚsÚindicesr   r   r   Ú__str__o   s    

zPageRange.__str__c                 C   s   dt t| ƒƒ d S )z#A string like "PageRange('1:2:3')".z
PageRange(ú))Úreprr   r(   r   r   r   Ú__repr__|   s    zPageRange.__repr__)Únr	   c                 C   s   | j  |¡S )a/  
        Assuming a sequence of length n, calculate the start and stop indices,
        and the stride length of the PageRange.

        See help(slice.indices).

        Args:
            n:  the length of the list of pages to choose from.

        Returns:
            Arguments for range().

        )r   r/   )r    r4   r   r   r   r/   €   s    zPageRange.indices)Úotherr	   c                 C   s   t |tƒsdS | j|jkS )NF)r   r   r   )r    r5   r   r   r   Ú__eq__�   s    
zPageRange.__eq__c                 C   s    t | j| jj| jj| jjffƒS r   )ÚhashÚ	__class__r   r"   r#   r$   r(   r   r   r   Ú__hash__•   s    zPageRange.__hash__c                 C   s   t |tƒstdt|ƒ› �ƒ‚| jjd us4|jjd ur<tdƒ‚| jj| jjf}|jj|jjf}t	t
 tdœdd„}t	t
 tdœdd„}||d ƒ||d ƒkr¦|| }}||d ƒ||d	 ƒkrÆtd
ƒ‚||d	 ƒ||d	 ƒkræ|d	 n|d	 }tt|d |ƒƒS )NzCan't add PageRange and zCan't add PageRange with stride)Úvr	   c                 S   s   | d u rt dƒS | S )Nz-inf©Úfloat©r:   r   r   r   Ú
_start_key¡   s    z%PageRange.__add__.<locals>._start_keyc                 S   s   | d u rt dƒS | S )NÚinfr;   r=   r   r   r   Ú	_stop_key¤   s    z$PageRange.__add__.<locals>._stop_keyr   r   zCan't add PageRanges with gap)r   r   Ú	TypeErrorÚtyper   r$   Ú
ValueErrorr"   r#   r   r   r<   r   )r    r5   ÚaÚbr>   r@   r#   r   r   r   Ú__add__˜   s    

(zPageRange.__add__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r%   Ústaticmethodr   Úboolr'   r   r0   r3   r   Útupler/   Úobjectr6   r9   rF   r   r   r   r   r      s   1r   r)   )Úargsr	   c                 C   sŒ   g }d}d}g | ¢d‘D ]n}t  |¡rV|s2tdƒ‚|dus>J ‚| |t |ƒf¡ d}q|rl|sl| |tf¡ t|t ƒr~J |ƒ‚|}d}q|S )a©  
    Given a list of filenames and page ranges, return a list of (filename, page_range) pairs.

    Args:
        args: A list where the first element is a filename. The other elements are
            filenames, page-range expressions, slice objects, or PageRange objects.
            A filename not followed by a page range indicates all pages of the file.

    Returns:
        A list of (filename, page_range) pairs.

    NFz8The first argument must be a filename, not a page range.T)r   r'   rC   ÚappendÚPAGE_RANGE_ALLr   )rO   ÚpairsZpdf_filenameZdid_page_ranger   r   r   r   Úparse_filename_page_rangesµ   s$    
ÿrS   )rJ   r   Útypingr   r   r   Úerrorsr   Z_INT_REr   r   rQ   Úlistr   rM   rS   r   ZPageRangeSpecr   r   r   r   Ú<module>   s     þ'