U
    cqe  ã                   @  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                   @  sB   e Zd ZdZdd„ Zdddœdd„Zed	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   ú0/tmp/pip-unpacked-wheel-gx5hpqp3/docx/package.pyÚafter_unmarshal   s    zPackage.after_unmarshalústr | IO[bytes]r	   ©Úimage_descriptorÚreturnc                 C  s   | 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Ú
ImageParts)r   c                 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 ]6}|jrq|jtjkr"q|j| jkr0q| j |j¡ qdS )zCLoad the image part collection with all the image parts in package.N)Z	iter_relsZis_externalZreltypeÚRTZIMAGEZtarget_partr   Úappend)r   Úrelr   r   r   r   &   s    zPackage._gather_image_partsN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r
   r   r   r   r   r   r   r      s   r   c                   @  s`   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd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   s    zImageParts.__init__c                 C  s   | j  |¡S r    )r!   Ú__contains__©r   Úitemr   r   r   r#   8   s    zImageParts.__contains__c                 C  s
   | j  ¡ S r    )r!   Ú__iter__r   r   r   r   r&   ;   s    zImageParts.__iter__c                 C  s
   | j  ¡ S r    )r!   Ú__len__r   r   r   r   r'   >   s    zImageParts.__len__c                 C  s   | j  |¡ d S r    )r!   r   r$   r   r   r   r   A   s    zImageParts.appendr   r	   r   c                 C  s,   t  |¡}|  |j¡}|dk	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   ÚimageZ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	   Z
from_imager   )r   r,   ÚpartnameÚ
image_partr   r   r   r+   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!   r*   )r   r*   r0   r   r   r   r)   X   s    


zImageParts._get_by_sha1c                   sV   ‡ fdd„}dd„ | D ƒ}t dt| ƒd ƒD ]}||k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©r.   r   r   Úimage_partnameh   s    z7ImageParts._next_image_partname.<locals>.image_partnamec                 S  s   g | ]}|j j‘qS r   )r/   Úidx)Ú.0r0   r   r   r   Ú
<listcomp>k   s     z3ImageParts._next_image_partname.<locals>.<listcomp>é   )ÚrangeÚlen)r   r.   r3   Zused_numbersr1   r   r2   r   r-   `   s    zImageParts._next_image_partnameN)r   r   r   r   r"   r#   r&   r'   r   r   r+   r)   r-   r   r   r   r   r   2   s   r   N)r   Ú
__future__r   Útypingr   Zdocx.image.imager   Zdocx.opc.constantsr   r   Zdocx.opc.packager   Zdocx.opc.packurir   Zdocx.parts.imager	   Zdocx.sharedr
   r   r   r   r   r   r   Ú<module>   s   #