o
    ¿Z2hƒ  ã                   @  sŠ   d Z ddlmZ ddlmZ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Úcast)Ú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   ú]/home/skpark/git/infrasmart_work/infrasmart/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  sJ   |   ¡ D ]}|jr
q|jtjkrq|j| jv rq| j td|jƒ¡ qdS )zCLoad the image part collection with all the image parts in package.r
   N)	Ú	iter_relsÚis_externalÚreltypeÚRTÚIMAGEÚtarget_partr   Úappendr   )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                   @  sd   e Zd ZdZdd„ Zd"dd„Zdd	„ Zd
d„ Zd#dd„Zd$dd„Z	d%dd„Z
d&dd„Zd'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__ÚitemÚobjectc                 C  r   r'   )r(   Ú__contains__©r   r+   r   r   r   r-   8   s   zImageParts.__contains__c                 C  ó
   | j  ¡ S r'   )r(   Ú__iter__r   r   r   r   r0   ;   r*   zImageParts.__iter__c                 C  r/   r'   )r(   Ú__len__r   r   r   r   r1   >   r*   zImageParts.__len__r
   c                 C  s   | j  |¡ d S r'   )r(   r    r.   r   r   r   r    A   ó   zImageParts.appendr   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_partr7   r   c                 C  s&   |   |j¡}t ||¡}|  |¡ |S )zVReturn |ImagePart| instance newly created from `image` and appended to the collection.)Ú_next_image_partnameÚextr
   Ú
from_imager    )r   r7   ÚpartnameÚ
image_partr   r   r   r6   P   s   
zImageParts._add_image_partr5   ÚstrúImagePart | Nonec                 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(   r5   )r   r5   r=   r   r   r   r4   W   s
   

ÿzImageParts._get_by_sha1r:   r	   c                   sX   d‡ 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.
        ÚnÚintr   r	   c                   s   t d| ˆ f ƒS )Nz/word/media/image%d.%sr   )r@   ©r:   r   r   Úimage_partnameg   r2   z7ImageParts._next_image_partname.<locals>.image_partnamec                 S  s   g | ]}|j j‘qS r   )r<   Úidx)Ú.0r=   r   r   r   Ú
<listcomp>j   s    z3ImageParts._next_image_partname.<locals>.<listcomp>é   N)r@   rA   r   r	   )ÚrangeÚlen)r   r:   rC   Úused_numbersr@   r   rB   r   r9   _   s   ÿzImageParts._next_image_partnameN)r+   r,   )r+   r
   r"   )r7   r   )r5   r>   r   r?   )r:   r>   r   r	   )r#   r$   r%   r&   r)   r-   r0   r1   r    r   r6   r4   r9   r   r   r   r   r   2   s    




r   N)r&   Ú
__future__r   Útypingr   r   Ú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    #