
    ,h&                     @   d Z ddlZddlZddlZddl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  G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z!y)zKAPI for reading/writing serialized Open Packaging Convention (OPC) package.    N)	Container	is_string)PackageNotFoundError)CONTENT_TYPE)CT_Typesserialize_part_xml)CONTENT_TYPES_URIPACKAGE_URIPackURI)CaseInsensitiveDict)default_content_types)lazypropertyc                   8    e Zd ZdZd Zd Zd Zd Zed        Z	y)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.
    c                     || _         y N	_pkg_fileselfpkg_files     U/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/pptx/opc/serialized.py__init__zPackageReader.__init__   	    !    c                     || j                   v S )zEReturn True when part identified by `pack_uri` is present in package._blob_readerr   pack_uris     r   __contains__zPackageReader.__contains__   s    4,,,,r   c                      | j                   |   S )z2Return bytes for part corresponding to `pack_uri`.r   r   s     r   __getitem__zPackageReader.__getitem__!   s      **r   c                 H    | 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   partnameblob_readeruris       r   rels_xml_forzPackageReader.rels_xml_for%   s0      ,,h.?.?S#&+#5{3?4?r   c                 @    t         j                  | j                        S )zC|_PhysPkgReader| subtype providing read access to the package file.)_PhysPkgReaderfactoryr   r   s    r   r   zPackageReader._blob_reader.   s     %%dnn55r   N)
__name__
__module____qualname____doc__r   r!   r#   r)   r   r    r   r   r   r      s0    "-+@ 6 6r   r   c                   >    e Zd ZdZd Zed        Zd Zd Zd Z	d Z
y)	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.
    c                 .    || _         || _        || _        y r   )r   	_pkg_rels_parts)r   r   pkg_relspartss       r   r   zPackageWriter.__init__?   s    !!r   c                 4     | |||      j                          y)a  Write 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   r8   r9   s       r   writezPackageWriter.writeD   s     	Hh&--/r   c                     t         j                  | j                        5 }| j                  |       | j	                  |       | j                  |       ddd       y# 1 sw Y   yxY w)z$Write physical package (.pptx file).N)_PhysPkgWriterr,   r   _write_content_types_stream_write_pkg_rels_write_partsr   phys_writers     r   r;   zPackageWriter._writeN   sU    ##DNN3 	+{,,[9  -k*	+ 	+ 	+s   4AA&c                 |    |j                  t        t        t        j	                  | j
                                     y)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)r=   r	   r   _ContentTypesItemxml_forr7   rC   s     r   r@   z)PackageWriter._write_content_types_streamU   s-     	088EF	
r   c                     | j                   D ]o  }|j                  |j                  |j                         |j                  s6|j                  |j                  j
                  |j                  j                         q y)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)r7   r=   r&   blob_relsr%   relsxml)r   rD   parts      r   rB   zPackageWriter._write_parts`   sZ    
 KK 	IDdmmTYY7zz!!$--"8"8$))--H	Ir   c                 l    |j                  t        j                  | j                  j                         y)zGWrite the XML rels item for *pkg_rels* ('/_rels/.rels') to the package.N)r=   r
   r%   r6   rL   rC   s     r   rA   zPackageWriter._write_pkg_relsj   s"    +..0B0BCr   N)r.   r/   r0   r1   r   classmethodr=   r;   r@   rB   rA   r2   r   r   r4   r4   4   s6    
 0 0+	
IDr   r4   c                   &    e Zd ZdZd Zed        Zy)r+   z/Base class for physical package reader objects.c                 D    t        dt        |       j                  z        )z%Must be implemented by each subclass.z%`%s` must implement `.__contains__()`)NotImplementedErrortyper.   )r   items     r   r!   z_PhysPkgReader.__contains__r   s!    !3d4j6I6II
 	
r   c                     t        |      st        |      S t        j                  j	                  |      rt        |      S t        j                  |      rt        |      S t        d|z        )zDReturn |_PhysPkgReader| subtype instance appropriage for `pkg_file`.zPackage not found at '%s')	r   _ZipPkgReaderospathisdir_DirPkgReaderzipfile
is_zipfiler   r<   r   s     r   r,   z_PhysPkgReader.factoryx   s^    
 " ** 77==" **h' **"#>#IJJr   N)r.   r/   r0   r1   r!   rO   r,   r2   r   r   r+   r+   o   s     9
 K Kr   r+   c                   "    e Zd ZdZd Zd Zd Zy)rZ   zImplements |PhysPkgReader| interface for OPC package extracted into directory.

    `path` is the path to a directory containing an expanded package.
    c                 L    t         j                  j                  |      | _        y r   )rW   rX   abspath_path)r   rX   s     r   r   z_DirPkgReader.__init__   s    WW__T*
r   c                     t         j                  j                  t        j                  | j
                  |j                              S zIReturn True when part identified by `pack_uri` is present in zip archive.)rW   rX   exists	posixpathjoinra   
membernamer   s     r   r!   z_DirPkgReader.__contains__   s*    ww~~innTZZ9L9LMNNr   c                    t         j                  j                  | j                  |j                        }	 t        |d      5 }|j                         cddd       S # 1 sw Y   yxY w# t        $ r t        d|z        w xY w)zFReturn bytes of file corresponding to `pack_uri` in package directory.rbNno member '%s' in package)	rW   rX   rf   ra   rg   openreadIOErrorKeyError)r   r    rX   fs       r   r#   z_DirPkgReader.__getitem__   sq    ww||DJJ(;(;<	CdD!  Qvvx      	C6ABB	Cs)   A( A	A( A%!A( %A( (B N)r.   r/   r0   r1   r   r!   r#   r2   r   r   rZ   rZ      s    
+OCr   rZ   c                   2    e Zd ZdZd Zd Zd Zed        Zy)rV   z@Implements |PhysPkgReader| interface for a zip-file OPC package.c                     || _         y r   r   r   s     r   r   z_ZipPkgReader.__init__   r   r   c                     || j                   v S rc   )_blobsr   s     r   r!   z_ZipPkgReader.__contains__   s    4;;&&r   c                 X    || j                   vrt        d|z        | j                   |   S )zReturn bytes for part corresponding to `pack_uri`.

        Raises |KeyError| if no matching member is present in zip archive.
        rj   )rs   rn   r   s     r   r#   z_ZipPkgReader.__getitem__   s0    
 4;;&6ABB{{8$$r   c                     t        j                  | j                  d      5 }|j                         D ci c]   }t	        d|z        |j                  |      " c}cddd       S c c}w # 1 sw Y   yxY w)z/dict mapping partname to package part binaries.rz/%sN)r[   ZipFiler   namelistr   rl   )r   znames      r   rs   z_ZipPkgReader._blobs   sd     __T^^S1 	RQDEJJLQDGEDL)166$<7Q	R 	RQ	R 	Rs   A*%A%A*%A**A3N)	r.   r/   r0   r1   r   r!   r#   r   rs   r2   r   r   rV   rV      s*    J"'% R Rr   rV   c                        e Zd ZdZed        Zy)r?   z/Base class for physical package writer objects.c                     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`.
        )_ZipPkgWriterr]   s     r   r,   z_PhysPkgWriter.factory   s     X&&r   N)r.   r/   r0   r1   rO   r,   r2   r   r   r?   r?      s    9' 'r   r?   c                   8    e Zd ZdZd Zd Zd Zd Zed        Z	y)r}   zMImplements |PhysPkgWriter| interface for a zip-file (.pptx file) OPC package.c                     || _         y r   r   r   s     r   r   z_ZipPkgWriter.__init__   r   r   c                     | S )zFEnable use as a context-manager. Opening zip for writing happens here.r2   r-   s    r   	__enter__z_ZipPkgWriter.__enter__   s    r   c                 8    | j                   j                          y)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   exc_type	exc_valueexc_tracebacks       r   __exit__z_ZipPkgWriter.__exit__   s     	

r   c                 P    | j                   j                  |j                  |       y)zHWrite `blob` to zip package with membername corresponding to `pack_uri`.N)r   writestrrg   )r   r    rI   s      r   r=   z_ZipPkgWriter.write   s    

H//6r   c                 b    t        j                  | j                  dt         j                        S )z$`ZipFile` instance open for writing.w)compression)r[   rw   r   ZIP_DEFLATEDr-   s    r   r   z_ZipPkgWriter._zipf   s!     t~~s@T@TUUr   N)
r.   r/   r0   r1   r   r   r   r=   r   r   r2   r   r   r}   r}      s/    W"7 V Vr   r}   c                   F    e Zd ZdZd Zed        Zed        Zed        Z	y)rF   zNComposes content-types "part" ([Content_Types].xml) for a collection of parts.c                     || _         y r   )r7   )r   r9   s     r   r   z_ContentTypesItem.__init__   s	    r   c                 &     | |      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)r<   r9   s     r   rG   z_ContentTypesItem.xml_for   s     5zr   c                    | j                   \  }}t        j                         }t        |j	                               D ]  \  }}|j                  ||        t        |j	                               D ]  \  }}|j                  ||        |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	overrides
_types_elmextcontent_typer&   s          r   r   z_ContentTypesItem._xml   s     #::)\\^
!'(8!9 	6C""35	6&,Y__->&? 	<"Hl##Hl;	< r   c                     t        t        j                  t        j                        }t	               }| j
                  D ]I  }|j                  |j                  }}|j                  }|j                         |ft        v r|||<   E|||<   K ||fS )zpair of dict (defaults, overrides) accounting for all parts.

        `defaults` is {ext: content_type} and overrides is {partname: content_type}.
        )rK   rL   )r   CTOPC_RELATIONSHIPSXMLdictr7   r&   r   r   lowerr   )r   r   r   rM   r&   r   r   s          r   r   z)_ContentTypesItem._defaults_and_overrides  s     'B,@,@bffMF	KK 	3D%)]]D4E4ElH,,C		\*.CC ,&2	(#	3 ""r   N)
r.   r/   r0   r1   r   rO   rG   r   r   r   r2   r   r   rF   rF      sF    X    $ # #r   rF   )"r1   rW   re   r[   pptx.compatr   r   pptx.exceptionsr   pptx.opc.constantsr   r   pptx.opc.oxmlr   r   pptx.opc.packurir	   r
   r   pptx.opc.sharedr   pptx.opc.specr   	pptx.utilr   r   objectr4   r+   rZ   rV   r?   r}   rF   r2   r   r   <module>r      s    R 	   , 0 1 6 D D / / "6I 6B8DF 8DvKY K6CN C.RN R4
'V 
'VN V83# 3#r   