
    JՁjp              	           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      Z
 e
d      Zdeeee
df      deeee
f      fdZeee
eeef   eeeef   ee   f   Zy)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                       e Zd ZdZdeed ef   ddfdZede	de
fd       ZdefdZdefd	Zdefd
Zdedeeeef   fdZdede
fdZdefdZddZy)	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).
    argreturnNc                     t        |t              r|| _        yt        |t              r|j	                         | _        yt        |t
              xr t        j                  t        |      }|st        |      |j                  d      r8t        |j                  d            }|dk7  r|dz   nd}t        ||      | _        yt        |j                  ddd      D cg c]  }|rt        |      nd c} | _        yc c}w )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            )
isinstanceslice_slicer	   to_slicestrrematchPAGE_RANGE_REr   groupint)selfr
   mstartstopgs         6/tmp/pip-target-jj2dfcxx/lib/python/pypdf/pagerange.py__init__zPageRange.__init__$   s    0 c5!DKc9%,,.DKsC ARXXmS%AS/!771:
OE %519Dt,DKAqAQ!RAA#a&4"7!RSDK!Rs   C;inputc                     t        | t        t        f      xs5 t        | t              xr# t	        t        j                  t        |             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.

        )r   r   r	   r   boolr   r   r   )r#   s    r!   validzPageRange.validO   s<     %%!34 
uc"KtBHH]E,J'K	
    c                     | j                   S )z/Return the slice equivalent of this page range.)r   r   s    r!   r   zPageRange.to_slice_   s    {{r'   c                 T   | j                   }|j                  V|j                  1|j                  |j                  dz   k(  rt	        |j                        S |j                  |j                  f}n#|j                  |j                  |j                  f}dj                  d |D              S )zA string like "1:2:3".r   :c              3   :   K   | ]  }|dn
t        |        y w)N )r   ).0is     r!   	<genexpr>z$PageRange.__str__.<locals>.<genexpr>n   s     EaiSV3Es   )r   stepr   r   r   join)r   sindicess      r!   __str__zPageRange.__str__c   s}    KK66>ww"qvv1'<177|#ggqvvoGggqvvqvv-GxxEWEEEr'   c                 6    dt        t        |             z   dz   S )z#A string like "PageRange('1:2:3')".z
PageRange())reprr   r)   s    r!   __repr__zPageRange.__repr__p   s    d3t9o-33r'   nc                 8    | j                   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   r4   )r   r:   s     r!   r4   zPageRange.indicest   s     {{""1%%r'   otherc                 V    t        |t              sy| j                  |j                  k(  S )NF)r   r	   r   )r   r<   s     r!   __eq__zPageRange.__eq__   s"    %+{{ell**r'   c                     t        | j                  | j                  j                  | j                  j                  | j                  j
                  ff      S )N)hash	__class__r   r   r   r1   r)   s    r!   __hash__zPageRange.__hash__   s;    T^^dkk&7&79I9I4;;K[K[%\]^^r'   c                    t        |t              st        dt        |             | j                  j
                  |j                  j
                  t        d      | j                  j                  | j                  j                  f}|j                  j                  |j                  j                  f}dt        t           dt        fd}dt        t           dt        fd} ||d          ||d         kD  r||}} ||d          ||d         kD  rt        d	       ||d          ||d         kD  r|d   n|d   }t        t        |d   |            S )
NzCan't add PageRange and zCan't add PageRange with stridevr   c                      | t        d      S | S )Nz-inffloatrD   s    r!   
_start_keyz%PageRange.__add__.<locals>._start_key   s    $%I5=414r'   c                      | t        d      S | S )NinfrF   rH   s    r!   	_stop_keyz$PageRange.__add__.<locals>._stop_key   s    #$95<3!3r'   r   r   zCan't add PageRanges with gap)r   r	   	TypeErrortyper   r1   
ValueErrorr   r   r   r   rG   r   )r   r<   abrI   rL   r   s          r!   __add__zPageRange.__add__   s@   %+6tE{mDEE;;'5<<+<+<+H>??KKt{{///LL 1 11	5(3- 	5E 	5	4# 	45 	4 adj1..aqA adi!o-<== 11Q48qtadqtT*++r'   )r<   r	   r   r	   )__name__
__module____qualname____doc__r   r   r   r"   staticmethodr   r%   r&   r   r5   r9   r   tupler4   objectr>   rB   rR    r'   r!   r	   r	      s     )TE%c"9: )Tt )TV 
S 
T 
 
% F F4# 4& &sC}!5 & +F +t +
_# _,r'   r	   r+   argsr   c                    g }d}d}g | dD ]}  }t         j                  |      r0|st        d      |J |j                  |t        |      f       d}H|r|s|j                  |t        f       t        |t               rJ |       |}d} |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&   rO   appendPAGE_RANGE_ALLr   )r[   pairspdf_filenamedid_page_ranger
   s        r!   parse_filename_page_rangesrb      s     *,E%)LN}t} #??3 N  ?"?LL,	#78!N NlN;<!#y16361L"N##$ Lr'   )rV   r   typingr   r   r   errorsr   _INT_REr   r	   r^   listr   rX   rb   r   PageRangeSpecrZ   r'   r!   <module>rh      s    
 ' ' 
WIRyG9CyHP, P,f 3$
uS)T)*
+$	%Y
 $N c9eCHouS#s]7KTRUYVWr'   