
    lh3                         d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ dZ G d	 d
eej                  Z G d de          ZdS )zc:mod:`wand.sequence` --- Sequences
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 0.3.0

    N   )	libmagicklibrary)abcbinaryxrange)	BaseImageImageProperty)MAGICK_VERSION_INFO)SequenceSingleImagec                        e Zd ZdZ fdZed             Zej        d             Zej	        d             Z
d Zd Zdd	Zd
 Zd Zd Zd Zd ZddZd Z xZS )r   zThe list-like object that contains every :class:`SingleImage`
    in the :class:`~wand.image.Image` container.  It implements
    :class:`collections.abc.Sequence` protocol.

    .. versionadded:: 0.3.0

    c                 f    t          t          |                               |           g | _        d S N)superr   __init__	instances)selfimage	__class__s     P/var/www/html/research.buxo.ca/env/lib/python3.11/site-packages/wand/sequence.pyr   zSequence.__init__   s,    h&&u---    c                 >    t          j        | j        j                  S )z(:class:`numbers.Integral`) The current index of
        its internal iterator.

        .. note::

           It's only for internal use.

        )r   MagickGetIteratorIndexr   wandr   s    r   current_indexzSequence.current_index    s     -djo>>>r   c                 D    t          j        | j        j        |           d S r   )r   MagickSetIteratorIndexr   r   )r   indexs     r   r   zSequence.current_index,   s    &tz>>>>>r   c              #   f   K   |                      |          }| j        }|| _        |V  || _        dS )zScoped setter of :attr:`current_index`.  Should be
        used for :keyword:`with` statement e.g.::

            with image.sequence.index_context(3):
                print(image.size)

        .. note::

           It's only for internal use.

        N)validate_positionr   )r   r    tmp_idxs      r   index_contextzSequence.index_context0   sB       &&u--$"$r   c                 >    t          j        | j        j                  S r   )r   MagickGetNumberImagesr   r   r   s    r   __len__zSequence.__len__C   s    ,TZ_===r   c                    t          |t          j                  st          dt	          |          z             t          |           }||k    s|| k     r#t          d                    ||                    |dk     r||z  }|S )Nzindex must be integer, not zout of index: {0} (total: {1})r   )
isinstancenumbersIntegral	TypeErrorreprlen
IndexErrorformat)r   r    lengths      r   r"   zSequence.validate_positionF   s    %!122 	I9DKKGHHHTF??evgoo077vFF   199VOEr   Fc                    |j         |j         dk    st          d          t          |           }|j        d}n|j        dk     r||j        z   }n|j        }t	          ||          }|j        d}n|j        dk     r||j        z   }n|j        }t	          ||p|          }|rt          ||          nt          ||d           S )Nr   z slicing with step is unsupportedr   )step
ValueErrorr.   startminstopr   slice)r   slice_as_ranger1   r5   r7   s         r   validate_slicezSequence.validate_sliceR   s    #v{a'7'7?@@@T<EE\AV\)EELEFE"";DD[1__FK'DD;D64>6**&.LveT"""E%t4L4LLr   c                     t          |t                    r;                     |          } fdt          |j        |j                  D             S                      |          } j        }t          |          }||k     r||         }|t          |dd           |S n4||z
  dz   }|
                    d t          |          D                         j        j        }t          j        |          }t          j        ||           t          j        |          }	t#          j                    }
t#          j        |	t)          t+          |                    |
          }t#          j        |
           t          j        |          }t#          j        |          }t          j        ||           t3          | j        |	          }| j        |<   |S )Nc                      g | ]
}|         S  r>   ).0ir   s     r   
<listcomp>z(Sequence.__getitem__.<locals>.<listcomp>i   s    GGGDGGGGr   
c_resourcer   c              3      K   | ]}d V  d S r   r>   )r?   _s     r   	<genexpr>z'Sequence.__getitem__.<locals>.<genexpr>t   s"      DDaTDDDDDDr   )r)   r8   r;   r   r5   r7   r"   r   r.   getattrextendr   r   r   r   r   GetImageFromMagickWandr   AcquireExceptionInfoCloneImagesr   strDestroyExceptionInfoNewMagickWandFromImageDestroyImager   )r   r    r9   r   instances_lengthinstancenumber_to_extendr   r#   r   excsingle_imagesingle_wands   `            r   __getitem__zSequence.__getitem__f   s   eU## 	H((//FGGGGVFL&+%F%FGGGG&&u--N	y>>### 'H$HlD99E$'77!;DD62B+C+CDDDDDDz066&tU333.t44,.. ,UF3u::4F4FLL&s+++4\BB -l;;&tW555{DJ>> (ur   c                    t          |t                    rD| j        }|                     |          }| |= |                     ||j                   || _        d S t          |t                    st          dt          |          z             | 	                    |          5 }t          j        | j        j                  rCt          j        | j        j        |j                   t          j        | j        j                   nBt          j        | j        j                   t          j        | j        j        |j                   d d d            d S # 1 swxY w Y   d S )N)offset7image must be an instance of wand.image.BaseImage, not )r)   r8   r   r;   rG   r5   r	   r,   r-   r$   r   MagickHasNextImager   r   MagickAddImageMagickRemoveImage)r   r    r   r#   r9   s        r   __setitem__zSequence.__setitem__   s   eU## 	H(G((//FVKKflK333!(DeY// A !248KK!@ A A A##E** He-djo>> H*4:?EJGGG-djo>>>>-djo>>>*4:?EJGGGH H H H H H H H H H H H H H H H H Hs   #B$EEEc                 d   t          |t                    r.|                     |d          }t          |          D ]}| |= d S |                     |          5 }t          j        | j        j                   |t          | j
                  k     r| j
        |= d d d            d S # 1 swxY w Y   d S )NT)r:   )r)   r8   r;   reversedr$   r   r[   r   r   r.   r   )r   r    range_r@   s       r   __delitem__zSequence.__delitem__   s   eU## 	.(((>>Ff%%  GG  ##E** .e)$*/:::3t~....u-. . . . . . . . . . . . . . . . . .s   ?B%%B),B)c                    	 |                      |          }n# t          $ r t          |           }Y nw xY wt          |t                    st          dt          |          z             | s&t          j        | j	        j
        |j
                   n<|dk    r| j        }| j	        j
        }|j        d         j
        }	 t          dk    r*t          j        |           t          j        ||           nhd| _        t          j        || j	        j        d         j
                   d| _        t          j        ||           d| _        t          j        |           || _        nk# || _        w xY w|                     |dz
            5  t          j        | j	        j
        |j        d         j
                   d d d            n# 1 swxY w Y   | j                            |d            d S )NrX   r         rc   r   r   )r"   r/   r.   r)   r	   r,   r-   r   rZ   r   r   r   sequencer   MagickSetFirstIteratorr[   r$   r   insert)r   r    r   r#   	self_wandr   s         r   rg   zSequence.insert   sF   	**511EE 	 	 	IIEEE	%++ 	= .04U< = = = 	P"4:?EJ????aZZ(G
I>!$)D- ',6629===*9d;;;;)*D&*9+/:+>q+A+FH H H)*D&*9d;;;)*D&-i888%,""W",,,,##EAI.. P P&tzq8I8NOOOP P P P P P P P P P P P P P PeT*****s+    44?BE$ $	E-0GGGc                 d   t          |t                    st          dt          |          z             | j        j        }| j        }	 t          j        |           t          j	        ||j
        d         j                   || _        n# || _        w xY w| j                            d            d S )NrX   r   )r)   r	   r,   r-   r   r   r   r   MagickSetLastIteratorrZ   re   r   append)r   r   r   r#   s       r   rk   zSequence.append   s    %++ 	= .04U< = = =z$	))$///"4):)?@@@!(DD((((d#####s   	9B
 
	BNc                 Z   | j         }| j        j        }d}	 |t          j        | j        j                   nG|dk    r7t          |          }|                     dt          |                     |dz  }|dz
  | _         t          |t          |                     r/t          j
        ||j        j                   t          |          }nt          dk    rdnd}|D ]}t          |t                    st          dt          |          z             t          j
        ||j        d         j                   g | _        |t          j        | j        j                   n| xj         |z  c_         |dz  }|| _         n# || _         w xY wd g|z  }||| j        |d <   d S || j        ||<   d S )Nr   r   rb      zCimages must consist of only instances of wand.image.BaseImage, not )r   r   r   r   rj   iterrg   nextr)   typerZ   r.   r   r	   r,   r-   re   r   )	r   imagesrW   r#   r   r1   deltar   	null_lists	            r   rG   zSequence.extend   s   $z	)~-djo>>>>Q;;!&\\FKK4<<000aKF%+aZ"&$t**-- $&tV\->???V0L@@a# $ $E%eY77 $'9;?;;G  
  .tU^A5F5KLLL)+!>#9$*/JJJJ ..%7..!!(DD((((FVO	>&/DN677###,5DN6&=)))s   EE= =	Fc                 f   t          j        | j        j                   t          j        | j        j        d          }t          j                    }t          j        |t          j        |                    }|r7|j	        r0t          j
        ||j	                  }t          j        |          }|S d S )Nr   )r   MagickResetIteratorr   r   MagickAppendImagesctypesc_size_tMagickGetImagesBlobbyrefvalue	string_atMagickRelinquishMemory)r   	repr_wandr1   blob_pblobs        r   
_repr_png_zSequence._repr_png_   s    #DJO444.tzBB	"",Y-3\&-A-AC C 	fl 	#FFL99D3F;;FK4r   )Fr   )__name__
__module____qualname____doc__r   propertyr   setter
contextlibcontextmanagerr$   r'   r"   r;   rU   r\   r`   rg   rk   rG   r   __classcell__r   s   @r   r   r      sD             	? 	? X	? ? ? ? % % %$> > >
 
 
M M M M(  :H H H&	. 	. 	."+ "+ "+H$ $ $&6 &6 &6 &6P      r   r   c                        e Zd ZdZdZ fdZed             Zed             Zed             Z	e	j
        d             Z	d Z fd	Zd
 Z xZS )r   aX  Each single image in :class:`~wand.image.Image` container.
    For example, it can be a frame of GIF animation.

    Note that all changes on single images are invisible to their
    containers unless they are altered a ``with ...`` context manager.

        with Image(filename='animation.gif') as container:
            with container.sequence[0] as frame:
                frame.negate()

    .. versionadded:: 0.3.0

    .. versionchanged:: 0.5.1
       Only sync changes of a :class:`SingleImage` when exiting a ``with ...``
       context. Not when parent :class:`~wand.image.Image` closes.
    Nc                     t          t          |                               |           || _        || _        d | _        d S r   )r   r   r   	containerc_original_resource_delay)r   r   r   r   r   s       r   r   zSingleImage.__init__  s;    k4  ))$///"#6 r   c                     | fS r   r>   r   s    r   re   zSingleImage.sequence#  s	    ur   c                     | j         j        }t          j        |           t          j        |          }d}| j        |k    r(|r&t          j        |          }|dz  }| j        |k    r|&|sJ | j        |k    sJ |S )zk(:class:`numbers.Integral`) The index of the single image in
        the :attr:`container` image.

        r   r   )r   r   r   ru   rH   r   r   GetNextImageInList)r   r   r   r@   s       r   r    zSingleImage.index'  s     ~"#D))).t44&%//E/077EFA &%//E/ '50000r   c                     | j         \| j        }|j                            | j                  5  t          j        |j                  | _         ddd           n# 1 swxY w Y   | j         S )z(:class:`numbers.Integral`) The delay to pause before display
        the next image (in the :attr:`~wand.image.BaseImage.sequence` of
        its :attr:`container`).  It's hundredths of a second.

        N)r   r   re   r$   r    r   MagickGetImageDelayr   )r   r   s     r   delayzSingleImage.delay8  s     ;I#11$*== J J%9).IIJ J J J J J J J J J J J J J J{s   AA Ac                 `   t          |t          j                  st          dt	          |          z             |dk     rt          d          | j        }|j                            | j	                  5  t          j        |j        |           d d d            n# 1 swxY w Y   || _        d S )Nzdelay must be an integer, not r   zdelay cannot be less than zero)r)   r*   r+   r,   r-   r4   r   re   r$   r    r   MagickSetImageDelayr   r   )r   r   r   s      r   r   zSingleImage.delayE  s    %!122 	?<tE{{JKKKQYY=>>>N	--dj99 	? 	?'	>>>	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?s   5BB #B c                 N    | j         r| | j        j        | j        <   d| _         dS dS )zIf instances was flagged as :attr:`dirty` by any manipulation
        methods, then this instance will overwrite :attr:`container` internal
        version at :attr:`index`.

        .. versionadded:: 0.5.1
        FN)dirtyr   re   r    r   s    r   _sync_container_sequencez$SingleImage._sync_container_sequenceP  s4     : 	26DN#DJ/DJJJ	 	r   c                     |                                   t          t          |                               |||           d S r   )r   r   r   __exit__)r   type_r{   	tracebackr   s       r   r   zSingleImage.__exit__[  s=    %%'''k4  ))%	BBBBBr   c                     t          |           }t          | dd            d                    |j        |j                  S d                    |j        |j        | j        d d         | j        | j                  S )NrB   z<{0}.{1}: (closed)>z<{0}.{1}: {2} ({3}x{4})>rd   )rp   rF   r0   r   r   	signaturewidthheight)r   clss     r   __repr__zSingleImage.__repr___  sp    4jj4t,,4(//MMM)00NCLN2A2
DK
 
 	
r   )r   r   r   r   r   r   r   re   r    r   r   r   r   r   r   r   s   @r   r   r     s         $ I       X   X  
 
 X
 \  \	 	 	C C C C C
 
 
 
 
 
 
r   r   )r   r   rw   r*   apir   r   compatr   r   r   r   r	   r
   versionr   __all__MutableSequencer   r   r>   r   r   <module>r      s
          # # # # # # # # ' ' ' ' ' ' ' ' ' ' + + + + + + + + ( ( ( ( ( (
#r r r r r}c1 r r rj^
 ^
 ^
 ^
 ^
) ^
 ^
 ^
 ^
 ^
r   