o
    W²ç_  ã                   @   sˆ   d Z ddlmZmZ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eƒZdS )z2WordprocessingML Package class and related objectsé    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literals)ÚImage)ÚRELATIONSHIP_TYPE)Ú
OpcPackage©ÚPackURI)Ú	ImagePart)Úlazypropertyc                   @   s4   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdd	„ Zd
S )ÚPackagez5Customizations specific to a WordprocessingML packagec                 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:\Projects\ConvertPro\env\Lib\site-packages\docx\package.pyÚafter_unmarshal   s   zPackage.after_unmarshalc                 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   Úimage_descriptorr   r   r   r      s   zPackage.get_or_add_image_partc                 C   s   t ƒ S )z0|ImageParts| collection object for this package.)Ú
ImagePartsr   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)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                   @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )r   zHCollection of |ImagePart| objects corresponding to images in the packagec                 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.appendc                 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   ÚimageZmatching_image_partr   r   r   r   D   s
   

z ImageParts.get_or_add_image_partc                 C   s&   |   |j¡}t ||¡}|  |¡ |S )zq
        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 )z~
        Return the image part in this collection having a SHA1 hash matching
        *sha1*, or |None| if not found.
        N)r!   r-   )r   r-   r3   r   r   r   r,   Z   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 )a  
        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©r1   r   r   Úimage_partnamek   r*   z7ImageParts._next_image_partname.<locals>.image_partnamec                 S   s   g | ]}|j j‘qS r   )r2   Úidx)Ú.0r3   r   r   r   Ú
<listcomp>m   s    z3ImageParts._next_image_partname.<locals>.<listcomp>é   )ÚrangeÚlen)r   r1   r6   Zused_numbersr4   r   r5   r   r0   d   s   ÿzImageParts._next_image_partnameN)r   r   r   r   r"   r$   r(   r)   r   r   r.   r,   r0   r   r   r   r   r   2   s    

r   N)r   Z
__future__r   r   r   r   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   Úobjectr   r   r   r   r   Ú<module>   s   #