o
    ‹°S¬  ã                   @   s„   d Z ddlmZ ddlmZ ddlmZmZ ddl	m
Z
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 )z’
Provides a low-level, write-only API to a serialized Open Packaging
Convention (OPC) package, essentially an implementation of OpcPackage.save()
é    )Úabsolute_importé   )ÚCONTENT_TYPE)ÚCT_TypesÚserialize_part_xml)ÚCONTENT_TYPES_URIÚPACKAGE_URI)ÚPhysPkgWriter)ÚCaseInsensitiveDict)Údefault_content_typesc                   @   s@   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZedd	„ ƒZd
S )ÚPackageWriterzý
    Writes a zip-format OPC package to *pkg_file*, where *pkg_file* can be
    either a path to a zip file (a string) or a file-like object. Its single
    API method, :meth:`write`, is static, so this class is not intended to
    be instantiated.
    c                 C   s8   t | ƒ}t ||¡ t ||¡ t ||¡ | ¡  dS )z»
        Write a physical package (.pptx file) to *pkg_file* containing
        *pkg_rels* and *parts* and a content types stream based on the
        content types of the parts.
        N)r	   r   Ú_write_content_types_streamÚ_write_pkg_relsÚ_write_partsÚclose)Zpkg_fileÚpkg_relsÚpartsÚphys_writer© r   úBD:\Projects\ConvertPro\env\Lib\site-packages\docx\opc\pkgwriter.pyÚwrite   s
   zPackageWriter.writec                 C   s   t  |¡}|  t|j¡ dS )z
        Write ``[Content_Types].xml`` part to the physical package with an
        appropriate content type lookup target for each part in *parts*.
        N)Ú_ContentTypesItemÚ
from_partsr   r   Úblob)r   r   Úctir   r   r   r   &   s   
z)PackageWriter._write_content_types_streamc                 C   s<   |D ]}|   |j|j¡ t|jƒr|   |jj|jj¡ qdS )z˜
        Write the blob of each part in *parts* to the package, along with a
        rels item for its relationships if and only if it has any.
        N)r   Úpartnamer   ÚlenZ_relsÚrels_uriÚxml)r   r   Úpartr   r   r   r   /   s   
€ýzPackageWriter._write_partsc                 C   s   |   tj|j¡ dS )za
        Write the XML rels item for *pkg_rels* ('/_rels/.rels') to the
        package.
        N)r   r   r   r   )r   r   r   r   r   r   :   s   zPackageWriter._write_pkg_relsN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   r   r   r   r   r   r   r      s    



r   c                   @   sD   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdd	„ Z	ed
d„ ƒZ
dS )r   zò
    Service class that composes a content types item ([Content_Types].xml)
    based on a list of parts. Not meant to be instantiated directly, its
    single interface method is xml_for(), e.g.
    ``_ContentTypesItem.xml_for(parts)``.
    c                 C   s   t ƒ | _tƒ | _d S )N)r
   Ú	_defaultsÚdictÚ
_overrides©Úselfr   r   r   Ú__init__J   s   z_ContentTypesItem.__init__c                 C   s
   t | jƒS )zˆ
        Return XML form of this content types item, suitable for storage as
        ``[Content_Types].xml`` in an OPC package.
        )r   Ú_elementr(   r   r   r   r   N   s   
z_ContentTypesItem.blobc                 C   s<   | ƒ }t j|jd< t j|jd< |D ]
}| |j|j¡ q|S )z¾
        Return content types XML mapping each part in *parts* to the
        appropriate content type and suitable for storage as
        ``[Content_Types].xml`` in an OPC package.
        Zrelsr   )ÚCTZOPC_RELATIONSHIPSr%   ZXMLÚ_add_content_typer   Úcontent_type)Úclsr   r   r   r   r   r   r   V   s   z_ContentTypesItem.from_partsc                 C   s2   |j }| ¡ |ftv r|| j|< dS || j|< dS )z‰
        Add a content type for the part with *partname* and *content_type*,
        using a default or override as appropriate.
        N)ÚextÚlowerr   r%   r'   )r)   r   r.   r0   r   r   r   r-   d   s   z#_ContentTypesItem._add_content_typec                 C   sX   t  ¡ }t| j ¡ ƒD ]}| || j| ¡ qt| j ¡ ƒD ]}| || j| ¡ q|S )a[  
        Return XML form of this content types item, suitable for storage as
        ``[Content_Types].xml`` in an OPC package. Although the sequence of
        elements is not strictly significant, as an aid to testing and
        readability Default elements are sorted by extension and Override
        elements are sorted by partname.
        )r   ÚnewÚsortedr%   ÚkeysZadd_defaultr'   Zadd_override)r)   Z
_types_elmr0   r   r   r   r   r+   o   s   	z_ContentTypesItem._elementN)r    r!   r"   r#   r*   Úpropertyr   Úclassmethodr   r-   r+   r   r   r   r   r   C   s    

r   N)r#   Z
__future__r   Ú	constantsr   r,   Zoxmlr   r   Zpackurir   r   Zphys_pkgr	   Úsharedr
   Úspecr   Úobjectr   r   r   r   r   r   Ú<module>   s   1