o
    	Ð'f	  ã                   @   st   d 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	ƒZG d
d„ dƒZdS )zªProvides low-level, write-only API to serialized (OPC) package.

OPC stands for Open Packaging Convention. This is e, essentially an implementation of
OpcPackage.save().
é    )Ú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)Úpkg_fileÚpkg_relsÚpartsÚphys_writer© r   úJ/home/ubuntu/flask/venv/lib/python3.10/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   ÚlenÚ_relsÚrels_uriÚxml)r   r   Úpartr   r   r   r   (   s   
€ýzPackageWriter._write_partsc                 C   s   |   tj|j¡ dS )zGWrite the XML rels item for `pkg_rels` ('/_rels/.rels') to the package.N)r   r   r   r   )r   r   r   r   r   r   1   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 ƒ | _i | _d S )N)r   Ú	_defaultsÚ
_overrides©Úselfr   r   r   Ú__init__?   s   
z_ContentTypesItem.__init__c                 C   s
   t | jƒS )zvReturn XML form of this content types item, suitable for storage as
        ``[Content_Types].xml`` in an OPC package.)r   Ú_elementr'   r   r   r   r   C   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.Úrelsr   )ÚCTÚOPC_RELATIONSHIPSr%   ÚXMLÚ_add_content_typer   Úcontent_type)Úclsr   r   r   r   r   r   r   I   s   z_ContentTypesItem.from_partsc                 C   s2   |j }| ¡ |ftv r|| j|< dS || j|< dS )zwAdd 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   r0   r2   r   r   r   r/   U   s   z#_ContentTypesItem._add_content_typec                 C   sX   t  ¡ }t| j ¡ ƒD ]}| || j| ¡ qt| j ¡ ƒD ]}| || j| ¡ q|S )aS  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%   ÚkeysÚadd_defaultr&   Úadd_override)r(   Ú
_types_elmr2   r   r   r   r   r*   ^   s   	z_ContentTypesItem._elementN)r    r!   r"   r#   r)   Úpropertyr   Úclassmethodr   r/   r*   r   r   r   r   r   7   s    

	r   N)r#   Údocx.opc.constantsr   r,   Údocx.opc.oxmlr   r   Údocx.opc.packurir   r   Údocx.opc.phys_pkgr   Údocx.opc.sharedr   Údocx.opc.specr	   r
   r   r   r   r   r   Ú<module>   s    (