o
    7 h,                     @  s>  d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
m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mZ dd	lmZ dd
lmZ ddlmZ er\ddlmZm Z  G dd de
e! Z"G dd dZ#G dd de
e Z$G dd de$Z%G dd de$Z&G dd dZ'G dd de'Z(G dd dZ)dS )zKAPI for reading/writing serialized Open Packaging Convention (OPC) package.    )annotationsN)IOTYPE_CHECKINGAny	ContainerSequence)PackageNotFoundError)CONTENT_TYPE)CT_Typesserialize_part_xml)CONTENT_TYPES_URIPACKAGE_URIPackURI)CaseInsensitiveDict)default_content_types)lazyproperty)Part_Relationshipsc                   @  sF   e Zd ZdZdddZdd
dZdddZdddZedddZ	dS )PackageReaderzProvides access to package-parts of an OPC package with dict semantics.

    The package may be in zip-format (a .pptx file) or expanded into a directory structure,
    perhaps by unzipping a .pptx file.
    pkg_filestr | IO[bytes]c                 C  
   || _ d S N	_pkg_fileselfr    r   [/var/www/html/figdemos/bartoux_crm/venv/lib/python3.10/site-packages/pptx/opc/serialized.py__init__      
zPackageReader.__init__pack_uriobjectreturnboolc                 C  
   || j v S )zEReturn True when part identified by `pack_uri` is present in package._blob_readerr   r!   r   r   r   __contains__       
zPackageReader.__contains__r   bytesc                 C  s
   | j | S )z2Return bytes for part corresponding to `pack_uri`.r&   r(   r   r   r   __getitem__$   r*   zPackageReader.__getitem__partnamebytes | Nonec                 C  s"   | j |j}}||v r|| S dS )zReturn optional rels item XML for `partname`.

        Returns `None` if no rels item is present for `partname`. `partname` is a |PackURI|
        instance.
        N)r'   rels_uri)r   r-   Zblob_readerurir   r   r   rels_xml_for(   s   zPackageReader.rels_xml_for_PhysPkgReaderc                 C  s   t | jS )zC|_PhysPkgReader| subtype providing read access to the package file.)r2   factoryr   r   r   r   r   r'   1   s   zPackageReader._blob_readerNr   r   r!   r"   r#   r$   r!   r   r#   r+   )r-   r   r#   r.   )r#   r2   )
__name__
__module____qualname____doc__r   r)   r,   r1   r   r'   r   r   r   r   r      s    



	r   c                   @  sP   e Zd ZdZddd	ZedddZdddZdddZdddZ	dddZ
dS )PackageWritera  Writes a zip-format OPC package to `pkg_file`.

    `pkg_file` can be either a path to a zip file (a string) or a file-like object. `pkg_rels` is
    the |_Relationships| object containing relationships for the package. `parts` is a sequence of
    |Part| subtype instance to be written to the package.

    Its single API classmethod is :meth:`write`. This class is not intended to be instantiated.
    r   r   pkg_relsr   partsSequence[Part]c                 C  s   || _ || _|| _d S r   )r   	_pkg_rels_parts)r   r   r=   r>   r   r   r   r   A   s   
zPackageWriter.__init__r#   Nonec                 C  s   | |||   dS )zWrite a physical package (.pptx file) to `pkg_file`.

        The serialized package contains `pkg_rels` and `parts`, a content-types stream based on
        the content type of each part, and a .rels file for each part that has relationships.
        N)_write)clsr   r=   r>   r   r   r   writeF   s   	zPackageWriter.writec                 C  sP   t | j}| | | | | | W d   dS 1 s!w   Y  dS )z$Write physical package (.pptx file).N)_PhysPkgWriterr3   r   _write_content_types_stream_write_pkg_rels_write_partsr   phys_writerr   r   r   rC   Q   s
   

"zPackageWriter._writerK   rF   c                 C  s   | ttt| j dS )zWrite `[Content_Types].xml` part to the physical package.

        This part must contain an appropriate content type lookup target for each part in the
        package.
        N)rE   r   r   _ContentTypesItemxml_forrA   rJ   r   r   r   rG   X   s   z)PackageWriter._write_content_types_streamc                 C  s:   | j D ]}||j|j |jr||jj|jj qdS )zWrite blob of each part in `parts` to the package.

        A rels item for each part is also written when the part has relationships.
        N)rA   rE   r-   blob_relsr/   relsxml)r   rK   partr   r   r   rI   c   s   
zPackageWriter._write_partsc                 C  s   | tj| jj dS )zGWrite the XML rels item for `pkg_rels` ('/_rels/.rels') to the package.N)rE   r   r/   r@   rQ   rJ   r   r   r   rH   m   s   zPackageWriter._write_pkg_relsN)r   r   r=   r   r>   r?   )r   r   r=   r   r>   r?   r#   rB   )r#   rB   )rK   rF   r#   rB   )r8   r9   r:   r;   r   classmethodrE   rC   rG   rI   rH   r   r   r   r   r<   7   s    
	




r<   c                   @  s2   e Zd ZdZdddZdddZedddZdS )r2   z/Base class for physical package reader objects.itemr"   r#   r$   c                 C  s   t dt| j )z%Must be implemented by each subclass.z%`%s` must implement `.__contains__()`NotImplementedErrortyper8   )r   rT   r   r   r   r)   u   s   z_PhysPkgReader.__contains__r!   r   r+   c                 C     t dt| j d)z*Blob for part corresponding to `pack_uri`.`z"` must implement `.__contains__()`rU   r(   r   r   r   r,   {      z_PhysPkgReader.__getitem__r   r   c                 C  sD   t |ts	t|S tj|rt|S t|rt|S t	d| )zDReturn |_PhysPkgReader| subtype instance appropriage for `pkg_file`.zPackage not found at '%s')

isinstancestr_ZipPkgReaderospathisdir_DirPkgReaderzipfile
is_zipfiler   rD   r   r   r   r   r3      s   

z_PhysPkgReader.factoryN)rT   r"   r#   r$   r7   )r   r   r#   r2   )r8   r9   r:   r;   r)   r,   rS   r3   r   r   r   r   r2   r   s    

r2   c                   @  s.   e Zd ZdZdddZdd
dZdddZdS )ra   zImplements |PhysPkgReader| interface for OPC package extracted into directory.

    `path` is the path to a directory containing an expanded package.
    r_   r\   c                 C  s   t j|| _d S r   )r^   r_   abspath_path)r   r_   r   r   r   r      s   z_DirPkgReader.__init__r!   r"   r#   r$   c                 C  s&   t |tsdS tjt| j|jS )IReturn True when part identified by `pack_uri` is present in zip archive.F)	r[   r   r^   r_   exists	posixpathjoinrf   
membernamer(   r   r   r   r)      s   
z_DirPkgReader.__contains__r   r+   c                 C  sh   t j| j|j}zt|d}| W  d   W S 1 sw   Y  W dS  ty3   td| w )zFReturn bytes of file corresponding to `pack_uri` in package directory.rbNno member '%s' in package)	r^   r_   rj   rf   rk   openreadIOErrorKeyError)r   r!   r_   fr   r   r   r,      s   (z_DirPkgReader.__getitem__N)r_   r\   r6   r7   )r8   r9   r:   r;   r   r)   r,   r   r   r   r   ra      s
    

ra   c                   @  s<   e Zd ZdZdddZdd
dZdddZedddZdS )r]   z@Implements |PhysPkgReader| interface for a zip-file OPC package.r   r   c                 C  r   r   r   r   r   r   r   r      r    z_ZipPkgReader.__init__r!   r"   r#   r$   c                 C  r%   )rg   )_blobsr(   r   r   r   r)      r*   z_ZipPkgReader.__contains__r   r+   c                 C  s    || j vrtd| | j | S )zReturn bytes for part corresponding to `pack_uri`.

        Raises |KeyError| if no matching member is present in zip archive.
        rm   )rs   rq   r(   r   r   r   r,      s   

z_ZipPkgReader.__getitem__dict[PackURI, bytes]c                   sH   t | jd  fdd  D W  d   S 1 sw   Y  dS )z/dict mapping partname to package part binaries.rc                   s    i | ]}t d |  |qS )z/%s)r   ro   ).0namezr   r   
<dictcomp>   s     z(_ZipPkgReader._blobs.<locals>.<dictcomp>N)rb   ZipFiler   namelistr4   r   rx   r   rs      s   $z_ZipPkgReader._blobsNr5   r6   r7   )r#   rt   )	r8   r9   r:   r;   r   r)   r,   r   rs   r   r   r   r   r]      s    


	r]   c                   @  s(   e Zd ZdZedddZdddZdS )rF   z/Base class for physical package writer objects.r   r   r#   _ZipPkgWriterc                 C  s   t |S )zReturn |_PhysPkgWriter| subtype instance appropriage for `pkg_file`.

        Currently the only subtype is `_ZipPkgWriter`, but a `_DirPkgWriter` could be implemented
        or even a `_StreamPkgWriter`.
        )r}   rd   r   r   r   r3      s   z_PhysPkgWriter.factoryr!   r   rN   r+   rB   c                 C  rX   )zDWrite `blob` to package with membername corresponding to `pack_uri`.rY   z` must implement `.write()`rU   r   r!   rN   r   r   r   rE      rZ   z_PhysPkgWriter.writeN)r   r   r#   r}   r!   r   rN   r+   r#   rB   )r8   r9   r:   r;   rS   r3   rE   r   r   r   r   rF      s
    rF   c                   @  sF   e Zd ZdZdddZdddZdddZdddZedddZ	dS )r}   zMImplements |PhysPkgWriter| interface for a zip-file (.pptx file) OPC package.r   r   c                 C  r   r   r   r   r   r   r   r      r    z_ZipPkgWriter.__init__r#   c                 C  s   | S )zFEnable use as a context-manager. Opening zip for writing happens here.r   r4   r   r   r   	__enter__   s   z_ZipPkgWriter.__enter__exc	list[Any]rB   c                 G  s   | j   dS )zClose the zip archive on exit from context.

        Closing flushes any pending physical writes and releasing any resources it's using.
        N)_zipfclose)r   r   r   r   r   __exit__   s   z_ZipPkgWriter.__exit__r!   r   rN   r+   c                 C  s   | j |j| dS )zHWrite `blob` to zip package with membername corresponding to `pack_uri`.N)r   writestrrk   r~   r   r   r   rE      s   z_ZipPkgWriter.writezipfile.ZipFilec                 C  s   t j| jdt jddS )z$`ZipFile` instance open for writing.wF)compressionstrict_timestamps)rb   r{   r   ZIP_DEFLATEDr4   r   r   r   r      s   z_ZipPkgWriter._zipfNr5   )r#   r}   )r   r   r#   rB   r   )r#   r   )
r8   r9   r:   r;   r   r   r   rE   r   r   r   r   r   r   r}      s    



r}   c                   @  sD   e Zd ZdZdddZeddd	Zedd
dZedddZ	dS )rL   zNComposes content-types "part" ([Content_Types].xml) for a collection of parts.r>   r?   c                 C  r   r   )rA   )r   r>   r   r   r   r      r    z_ContentTypesItem.__init__r#   r
   c                 C  s
   | |j S )zReturn content-types XML mapping each part in `parts` to a content-type.

        The resulting XML is suitable for storage as `[Content_Types].xml` in an OPC package.
        )_xml)rD   r>   r   r   r   rM      s   
z_ContentTypesItem.xml_forc                 C  sZ   | j \}}t }t| D ]
\}}||| qt| D ]
\}}||| q |S )a  lxml.etree._Element containing the content-types item.

        This XML object is suitable for serialization to the `[Content_Types].xml` item for 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.
        )_defaults_and_overridesr
   newsorteditemsadd_defaultadd_override)r   defaults	overridesZ
_types_elmextcontent_typer-   r   r   r   r     s   
	z_ContentTypesItem._xml)tuple[dict[str, str], dict[PackURI, str]]c                 C  s^   t tjtjd}i }| jD ]}|j|j}}|j}| |ft	v r&|||< q|||< q||fS )zpair of dict (defaults, overrides) accounting for all parts.

        `defaults` is {ext: content_type} and overrides is {partname: content_type}.
        )rP   rQ   )
r   CTOPC_RELATIONSHIPSXMLrA   r-   r   r   lowerr   )r   r   r   rR   r-   r   r   r   r   r   r     s   


z)_ContentTypesItem._defaults_and_overridesN)r>   r?   )r>   r?   r#   r
   )r#   r
   )r#   r   )
r8   r9   r:   r;   r   rS   rM   r   r   r   r   r   r   r   rL      s    
rL   )*r;   
__future__r   r^   ri   rb   typingr   r   r   r   r   pptx.excr   pptx.opc.constantsr	   r   pptx.opc.oxmlr
   r   pptx.opc.packurir   r   r   pptx.opc.sharedr   Zpptx.opc.specr   	pptx.utilr   pptx.opc.packager   r   r+   r   r<   r2   ra   r]   rF   r}   rL   r   r   r   r   <module>   s.    !;!