U
    d                     @   s`   d Z ddlZddlmZ dZdjedZG dd	 d	eZd
Z	G dd de
ZedZdd 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/mstamy2/PyPDF3/blob/master/LICENSE
    N   )isStringz(0|-?[1-9]\d*)z%^({int}|({int}?(:{int}?(:{int}?)?)))$intc                   @   s   e Zd ZdS )
ParseErrorN)__name__
__module____qualname__ r
   r
   4/tmp/pip-unpacked-wheel-eeeohlmn/PyPDF3/pagerange.pyr      s   r   a  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.
c                   @   sZ   e Zd ZdZdd Zejr*ejjede_edd Zdd Z	d	d
 Z
dd Zdd ZdS )	PageRangeaC  
    A slice-like representation of a range of page indices,
        i.e. 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.
      o  PageRange(str) parses a string representing a page range.
      o  PageRange(slice) directly "imports" a slice.
      o  to_slice() gives the equivalent slice.
      o  str() and repr() allow printing.
      o  indices(n) is like slice.indices(n).
    c                 C   s   t |tr|| _dS t |tr,| | _dS t|o>tt|}|sNt	|nX|
drt|
d}|dkrv|d nd}t||| _ntd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.
        {page_range_help}
        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 )Nr   ).0gr
   r
   r   
<listcomp>P   s   z&PageRange.__init__.<locals>.<listcomp>         )
isinstanceslice_slicer   to_slicer   rematchPAGE_RANGE_REr   groupr   )selfargmstartstopr
   r
   r   __init__2   s     




zPageRange.__init__)Zpage_range_helpc                 C   s,   t | tp*t | tp*t| o*ttt| S )z7 True if input is a valid initializer for a PageRange. )r   r   r   r   boolr   r   r   )inputr
   r
   r   validW   s    
zPageRange.validc                 C   s   | j S )z1 Return the slice equivalent of this page range. )r   r   r
   r
   r   r   _   s    zPageRange.to_slicec                 C   sf   | j }|jdkrB|jdk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 krdnt |V  qd S )N )str)r   ir
   r
   r   	<genexpr>m   s     z$PageRange.__str__.<locals>.<genexpr>)r   stepr    r!   r)   join)r   sindicesr
   r
   r   __str__c   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__o   s    zPageRange.__repr__c                 C   s   | j |S )z
        n is the length of the list of pages to choose from.
        Returns arguments for range().  See help(slice.indices).
        )r   r/   )r   nr
   r
   r   r/   s   s    zPageRange.indicesN)r   r   r	   __doc__r"   formatPAGE_RANGE_HELPstaticmethodr%   r   r0   r3   r/   r
   r
   r
   r   r   $   s   "
r   r'   c                 C   sl   g }d}d}| dg D ]P}t |rH|s0td||t |f d}q|r^|s^||tf |}d}q|S )a0  
    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%   
ValueErrorappendPAGE_RANGE_ALL)argspairsZpdf_filenameZdid_page_ranger   r
   r
   r   parse_filename_page_ranges~   s    
r>   )r5   r   utilsr   Z_INT_REr6   r   	Exceptionr   r7   objectr   r;   r>   r
   r
   r
   r   <module>   s   W