a
    ]øj¥  ã                   @   sˆ   d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 G dd„ dƒZ
G dd	„ d	ƒZG d
d„ de
ƒZG dd„ de
ƒZG dd„ deƒZdS )zMProvides a general interface to a `physical` OPC package, such as a zip file.é    N)ÚZIP_DEFLATEDÚZipFileÚ
is_zipfile)ÚPackageNotFoundError)ÚCONTENT_TYPES_URIc                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚPhysPkgReaderz,Factory for physical package reader objects.c                    sL   t |tƒr8tj |¡rt}q<t|ƒr*t}q<td| ƒ‚nt}t	t
| ƒ |¡S )NzPackage not found at '%s')Ú
isinstanceÚstrÚosÚpathÚisdirÚ_DirPkgReaderr   Ú_ZipPkgReaderr   Úsuperr   Ú__new__)ÚclsÚpkg_fileZ
reader_cls©Ú	__class__© úM/var/www/html/assistant/venv/lib/python3.9/site-packages/docx/opc/phys_pkg.pyr      s    
zPhysPkgReader.__new__©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r   r   r   r   r   
   s   r   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚPhysPkgWriterz,Factory for physical package writer objects.c                    s   t t| ƒ t¡S )N)r   r   r   Ú_ZipPkgWriter)r   r   r   r   r   r      s    zPhysPkgWriter.__new__r   r   r   r   r   r      s   r   c                       sD   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zedd	„ ƒZd
d„ Z	‡  Z
S )r   zWImplements |PhysPkgReader| interface for an OPC package extracted into a
    directory.c                    s    t t| ƒ ¡  tj |¡| _dS )zA`path` is the path to a directory containing an expanded package.N)r   r   Ú__init__r
   r   ÚabspathÚ_path)Úselfr   r   r   r   r   '   s    z_DirPkgReader.__init__c                 C   sH   t j | j|j¡}t|dƒ}| ¡ }W d  ƒ n1 s:0    Y  |S )zIReturn contents of file corresponding to `pack_uri` in package directory.ÚrbN)r
   r   Újoinr!   Ú
membernameÚopenÚread)r"   Úpack_urir   ÚfÚblobr   r   r   Úblob_for,   s    &z_DirPkgReader.blob_forc                 C   s   dS )z|Provides interface consistency with |ZipFileSystem|, but does nothing, a
        directory file system doesn't need closing.Nr   ©r"   r   r   r   Úclose3   s    z_DirPkgReader.closec                 C   s
   |   t¡S )z7Return the `[Content_Types].xml` blob from the package.©r+   r   r,   r   r   r   Úcontent_types_xml8   s    z_DirPkgReader.content_types_xmlc                 C   s,   z|   |j¡}W n ty&   d}Y n0 |S )z`Return rels item XML for source with `source_uri`, or None if the item has no
        rels item.N)r+   Úrels_uriÚIOError©r"   Z
source_uriZrels_xmlr   r   r   Úrels_xml_for=   s
    
z_DirPkgReader.rels_xml_for©r   r   r   r   r   r+   r-   Úpropertyr/   r3   r   r   r   r   r   r   #   s   
r   c                       sD   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zedd	„ ƒZd
d„ Z	‡  Z
S )r   z@Implements |PhysPkgReader| interface for a zip file OPC package.c                    s   t t| ƒ ¡  t|dƒ| _d S )NÚr)r   r   r   r   Ú_zipf©r"   r   r   r   r   r   J   s    z_ZipPkgReader.__init__c                 C   s   | j  |j¡S )zReturn blob corresponding to `pack_uri`.

        Raises |ValueError| if no matching member is present in zip archive.
        )r7   r'   r%   )r"   r(   r   r   r   r+   N   s    z_ZipPkgReader.blob_forc                 C   s   | j  ¡  dS )z;Close the zip archive, releasing any resources it is using.N©r7   r-   r,   r   r   r   r-   U   s    z_ZipPkgReader.closec                 C   s
   |   t¡S )z;Return the `[Content_Types].xml` blob from the zip package.r.   r,   r   r   r   r/   Y   s    z_ZipPkgReader.content_types_xmlc                 C   s,   z|   |j¡}W n ty&   d}Y n0 |S )z]Return rels item XML for source with `source_uri` or None if no rels item is
        present.N)r+   r0   ÚKeyErrorr2   r   r   r   r3   ^   s
    
z_ZipPkgReader.rels_xml_forr4   r   r   r   r   r   G   s   
r   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )r   z@Implements |PhysPkgWriter| interface for a zip file OPC package.c                    s"   t t| ƒ ¡  t|dtd| _d S )NÚw)Úcompression)r   r   r   r   r   r7   r8   r   r   r   r   k   s    z_ZipPkgWriter.__init__c                 C   s   | j  ¡  dS )zkClose the zip archive, flushing any pending physical writes and releasing any
        resources it's using.Nr9   r,   r   r   r   r-   o   s    z_ZipPkgWriter.closec                 C   s   | j  |j|¡ dS )zYWrite `blob` to this zip package with the membername corresponding to
        `pack_uri`.N)r7   Úwritestrr%   )r"   r(   r*   r   r   r   Úwritet   s    z_ZipPkgWriter.write)r   r   r   r   r   r-   r>   r   r   r   r   r   r   h   s   r   )r   r
   Úzipfiler   r   r   Zdocx.opc.exceptionsr   Zdocx.opc.packurir   r   r   r   r   r   r   r   r   r   Ú<module>   s   $!