o
    	Ð'f  ã                   @  s†   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„ deƒZG dd„ dƒZdS )z3WordprocessingML Package class and related objects.é    )Úannotations)ÚIO)ÚImage)ÚRELATIONSHIP_TYPE)Ú
OpcPackage©ÚPackURI)Ú	ImagePart)Úlazypropertyc                   @  s8   e Zd ZdZdd„ Zddd	„Zeddd„ƒZdd„ ZdS )ÚPackagez6Customizations specific to a WordprocessingML package.c                 C  s   |   ¡  dS )z¢Called 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©Úself© r   úD/home/ubuntu/flask/venv/lib/python3.10/site-packages/docx/package.pyÚafter_unmarshal   s   zPackage.after_unmarshalÚimage_descriptorústr | IO[bytes]Úreturnr	   c                 C  ó   | j  |¡S )zºReturn |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   r   r   r   r   r      s   zPackage.get_or_add_image_partÚ
ImagePartsc                 C  s   t ƒ S )z0|ImageParts| collection object for this package.)r   r   r   r   r   r   !   s   zPackage.image_partsc                 C  sD   |   ¡ D ]}|jr
q|jtjkrq|j| jv rq| j |j¡ qdS )zCLoad the image part collection with all the image parts in package.N)Ú	iter_relsÚis_externalÚreltypeÚRTÚIMAGEÚtarget_partr   Úappend)r   Úrelr   r   r   r   &   s   ùzPackage._gather_image_partsN©r   r   r   r	   )r   r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r
   r   r   r   r   r   r   r      s    
r   c                   @  sZ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	dd„ Z
dd„ Zdd„ ZdS )r   zICollection of |ImagePart| objects corresponding to images in the package.c                 C  s
   g | _ d S ©N)Ú_image_partsr   r   r   r   Ú__init__5   ó   
zImageParts.__init__c                 C  r   r&   )r'   Ú__contains__©r   Úitemr   r   r   r*   8   s   zImageParts.__contains__c                 C  ó
   | j  ¡ S r&   )r'   Ú__iter__r   r   r   r   r.   ;   r)   zImageParts.__iter__c                 C  r-   r&   )r'   Ú__len__r   r   r   r   r/   >   r)   zImageParts.__len__c                 C  s   | j  |¡ d S r&   )r'   r   r+   r   r   r   r   A   ó   zImageParts.appendr   r   r   r	   c                 C  s,   t  |¡}|  |j¡}|dur|S |  |¡S )zºReturn |ImagePart| object containing image identified by `image_descriptor`.

        The image-part is newly created if a matching one is not present in the
        collection.
        N)r   Ú	from_fileÚ_get_by_sha1Úsha1Ú_add_image_part)r   r   ÚimageÚmatching_image_partr   r   r   r   D   s
   

z ImageParts.get_or_add_image_partc                 C  s&   |   |j¡}t ||¡}|  |¡ |S )z_Return an |ImagePart| instance newly created from image and appended to the
        collection.)Ú_next_image_partnameÚextr	   Ú
from_imager   )r   r5   ÚpartnameÚ
image_partr   r   r   r4   P   s   
zImageParts._add_image_partc                 C  s"   | j D ]}|j|kr|  S qdS )zlReturn the image part in this collection having a SHA1 hash matching `sha1`,
        or |None| if not found.N)r'   r3   )r   r3   r;   r   r   r   r2   X   s
   

ÿzImageParts._get_by_sha1c                   sV   ‡ fdd„}dd„ | D ƒ}t dt| ƒd ƒD ]}||vr"||ƒ  S q|t| ƒd ƒS )zþThe 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                   s   t d| ˆ f ƒS )Nz/word/media/image%d.%sr   )Ún©r8   r   r   Úimage_partnameh   r0   z7ImageParts._next_image_partname.<locals>.image_partnamec                 S  s   g | ]}|j j‘qS r   )r:   Úidx)Ú.0r;   r   r   r   Ú
<listcomp>k   s    z3ImageParts._next_image_partname.<locals>.<listcomp>é   )ÚrangeÚlen)r   r8   r>   Úused_numbersr<   r   r=   r   r7   `   s   ÿzImageParts._next_image_partnameNr!   )r"   r#   r$   r%   r(   r*   r.   r/   r   r   r4   r2   r7   r   r   r   r   r   2   s    
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   r   Ú<module>   s    #