
    Pqh                        d Z ddlmZ ddlmZ ddlmZ ddlmZ	 ddl
mZ ddlmZ ddlmZ dd	lmZ  G d
 de      Z G d d      Zy)z3WordprocessingML Package class and related objects.    )annotations)IO)Image)RELATIONSHIP_TYPE)
OpcPackagePackURI)	ImagePart)lazypropertyc                  6    e Zd ZdZd ZddZedd       Zd Zy)	Packagez6Customizations specific to a WordprocessingML package.c                $    | j                          y)zCalled by loading code after all parts and relationships have been loaded.

        This method affords the opportunity for any required post-processing.
        N)_gather_image_partsselfs    M/srv/www/ollama/chat-ollama/venv/lib/python3.12/site-packages/docx/package.pyafter_unmarshalzPackage.after_unmarshal   s    
 	  "    c                8    | j                   j                  |      S )zReturn |ImagePart| containing image specified by `image_descriptor`.

        The image-part is newly created if a matching one is not already present in the
        collection.
        )image_partsget_or_add_image_part)r   image_descriptors     r   r   zPackage.get_or_add_image_part   s     556FGGr   c                    t               S )z0|ImageParts| collection object for this package.)
ImagePartsr   s    r   r   zPackage.image_parts!   s     |r   c                    | j                         D ]k  }|j                  r|j                  t        j                  k7  r.|j
                  | j                  v rG| j                  j                  |j
                         m y)zCLoad the image part collection with all the image parts in package.N)	iter_relsis_externalreltypeRTIMAGEtarget_partr   append)r   rels     r   r   zPackage._gather_image_parts&   sb    >># 	5C{{bhh&$"2"22##COO4	5r   Nr   zstr | IO[bytes]returnr
   )r%   r   )	__name__
__module____qualname____doc__r   r   r   r   r    r   r   r   r      s)    @#H  	5r   r   c                  H    e Zd ZdZd Zd Zd Zd Zd ZddZ	d Z
d	 Zd
 Zy)r   zICollection of |ImagePart| objects corresponding to images in the package.c                    g | _         y N)_image_partsr   s    r   __init__zImageParts.__init__5   s
    r   c                8    | j                   j                  |      S r-   )r.   __contains__r   items     r   r1   zImageParts.__contains__8   s      --d33r   c                6    | j                   j                         S r-   )r.   __iter__r   s    r   r5   zImageParts.__iter__;   s      ))++r   c                6    | j                   j                         S r-   )r.   __len__r   s    r   r7   zImageParts.__len__>   s      ((**r   c                :    | j                   j                  |       y r-   )r.   r"   r2   s     r   r"   zImageParts.appendA   s      &r   c                    t        j                  |      }| j                  |j                        }||S | j	                  |      S )zReturn |ImagePart| object containing image identified by `image_descriptor`.

        The image-part is newly created if a matching one is not present in the
        collection.
        )r   	from_file_get_by_sha1sha1_add_image_part)r   r   imagematching_image_parts       r   r   z ImageParts.get_or_add_image_partD   sE      01"//

;*&&##E**r   c                    | j                  |j                        }t        j                  ||      }| j	                  |       |S )z_Return an |ImagePart| instance newly created from image and appended to the
        collection.)_next_image_partnameextr
   
from_imager"   )r   r>   partname
image_parts       r   r=   zImageParts._add_image_partP   s<     ,,UYY7))%:
Jr   c                L    | j                   D ]  }|j                  |k(  s|c S  y)zlReturn the image part in this collection having a SHA1 hash matching `sha1`,
        or |None| if not found.N)r.   r<   )r   r<   rE   s      r   r;   zImageParts._get_by_sha1X   s0     ++ 	"J$&!!	" r   c                    fd}| D cg c]  }|j                   j                   }}t        dt        |       dz         D ]  }||vs ||      c S   |t        |       dz         S c c}w )zThe next available image partname, starting from ``/word/media/image1.{ext}``
        where unused numbers are reused.

        The partname is unique by number, without regard to the extension. `ext` does
        not include the leading period.
        c                $    t        d| fz        S )Nz/word/media/image%d.%sr   )nrB   s    r   image_partnamez7ImageParts._next_image_partname.<locals>.image_partnameh   s    3q#h>??r      )rD   idxrangelen)r   rB   rJ   rE   used_numbersrI   s    `    r   rA   zImageParts._next_image_partname`   sx    	@ CGGJ
++//GGq#d)a-( 	)A$%a((	) c$i!m,,	 Hs   A)Nr$   )r&   r'   r(   r)   r/   r1   r5   r7   r"   r   r=   r;   rA   r*   r   r   r   r   2   s2    S4,+'
+-r   r   N)r)   
__future__r   typingr   docx.image.imager   docx.opc.constantsr   r   docx.opc.packager   docx.opc.packurir	   docx.parts.imager
   docx.sharedr   r   r   r*   r   r   <module>rX      s7    9 "  " 6 ' $ & $ 5j  5F=- =-r   