
     h                     (   d Z ddlZddlmZmZmZmZ ddlmZ dZ	d
                    e	          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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/PyPDF2/blob/main/LICENSE
    N)AnyListTupleUnion   )
ParseErrorz(0|-?[1-9]\d*)z%^({int}|({int}?(:{int}?(:{int}?)?)))$intc                       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dZdS )	PageRangeaK  
    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                 2   t          |t                    r	|| _        dS t          |t                    r|                                | _        dS t          |t
                    ot          j        t          |          }|st          |          |
                    d          rFt          |
                    d                    }|dk    r|dz   nd}t          ||          | _        dS t          d |
                    ddd          D              | _        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                 4    g | ]}|rt          |          nd S )Nr	   ).0gs     U/var/www/flask_pdf_extract_app/myenv/lib/python3.11/site-packages/PyPDF2/pagerange.py
<listcomp>z&PageRange.__init__.<locals>.<listcomp>M   s'    !R!R!RAA"7#a&&&4!R!R!R             )
isinstanceslice_slicer   to_slicestrrematchPAGE_RANGE_REr   groupr
   )selfr   mstartstops        r   __init__zPageRange.__init__%   s    . c5!! 	DKFc9%% 	,,..DKFsC  ARXmS%A%A 	TS//!WWQZZ 	T

OOE %5199Dt,,DKKK!R!RAqAQAQ!R!R!RSDKKKr   inputc                     t          | t          t          f          p;t          | t                    o&t	          t          j        t          |                     S )z5True if input is a valid initializer for a PageRange.)r   r   r   r   boolr    r!   r"   )r)   s    r   validzPageRange.validO   sF     %%!344 
uc""KtBH]E,J,J'K'K	
r   c                     | j         S )z/Return the slice equivalent of this page range.)r   r$   s    r   r   zPageRange.to_sliceV   s
    {r   c                     | j         }|j        =|j        '|j        |j        dz   k    rt	          |j                  S |j        |j        f}n|j        |j        |j        f}d                    d |D                       S )zA string like "1:2:3".Nr   :c              3   <   K   | ]}|dnt          |          V  d S )N )r   )r   is     r   	<genexpr>z$PageRange.__str__.<locals>.<genexpr>e   s0      EEaiSVVEEEEEEr   )r   stepr&   r'   r   join)r$   sindicess      r   __str__zPageRange.__str__Z   sz    K6>w"qv1'<'<17||#gqvoGGgqvqv-GxxEEWEEEEEEr   c                 F    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__g   s    d3t99oo-33r   nc                 6    | j                             |          S )z
        n is the length of the list of pages to choose from.

        Returns arguments for range().  See help(slice.indices).
        )r   r8   )r$   r>   s     r   r8   zPageRange.indicesk   s     {""1%%%r   otherc                 P    t          |t                    sdS | j        |j        k    S )NF)r   r   r   )r$   r@   s     r   __eq__zPageRange.__eq__s   s(    %++ 	5{el**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         |d         k    r||}}|d         |d         k    rt          d          t          t          |d         t          |d         |d                                       S )NzCan't add PageRange and zCan't add PageRange with strider   r   zCan't add PageRanges with gap)r   r   	TypeErrortyper   r5   
ValueErrorr&   r'   r   max)r$   r@   abs       r   __add__zPageRange.__add__x   s    %++ 	FDtE{{DDEEE;'5<+<+H>???Kt{//L 11Q4!A$;;aqA Q4!A$;;<===qtS1qt__55666r   )r@   r   r   r   )__name__
__module____qualname____doc__r   r   r   r(   staticmethodr   r+   r,   r   r9   r=   r
   r   r8   rB   rJ    r   r   r   r      s:        "(TE%c"9: (Tt (T (T (T (TT 
S 
T 
 
 
 \
%    F F F F F4# 4 4 4 4& &sC}!5 & & & &+C +D + + + +
7 7 7 7 7 7r   r   r0   argsr   c                    g }d}d}| dgz   D ]x}t                               |          r8|st          d          |                    |t          |          f           d}T|r|s|                    |t          f           |}d}y|S )a-  
    Given a list of filenames and page ranges, return a list of (filename, page_range) pairs.

    First arg must be a filename; other ags are filenames, page-range
    expressions, slice objects, or PageRange objects.
    A filename not followed by a page range indicates all pages of the file.
    NFz8The first argument must be a filename, not a page range.T)r   r,   rF   appendPAGE_RANGE_ALL)rQ   pairspdf_filenamedid_page_ranger   s        r   parse_filename_page_rangesrX      s     *,ELNtf} # #??3 	#  N   LL,	#7888!NN  =N =lN;<<<L"NNLr   )rN   r    typingr   r   r   r   errorsr   _INT_REformatr"   r   rT   r   rX   r
   PageRangeSpecrP   r   r   <module>r^      s"    
			 * * * * * * * * * * * *      
7>>7>KKt7 t7 t7 t7 t7 t7 t7 t7n 3
uS)T)*
+	%Y
    @ c9eCHouS#s]7KTRUYVWr   