o
    S                     @   s   d Z ddlmZ ddl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
 d
eZG dd deZG dd deZG dd deZG dd deZdS )zO
Provides a general interface to a *physical* OPC package, such as a zip file.
    )absolute_importN)ZipFile
is_zipfileZIP_DEFLATED   )	is_string)PackageNotFoundError)CONTENT_TYPES_URIc                           e Zd ZdZ fddZ  ZS )PhysPkgReaderz6
    Factory for physical package reader objects.
    c                    sH   t |rtj|rt}nt|rt}ntd| t}tt	| 
|S )NzPackage not found at '%s')r   ospathisdir_DirPkgReaderr   _ZipPkgReaderr   superr   __new__)clspkg_fileZ
reader_cls	__class__ AD:\Projects\ConvertPro\env\Lib\site-packages\docx\opc\phys_pkg.pyr      s   zPhysPkgReader.__new____name__
__module____qualname____doc__r   __classcell__r   r   r   r   r          r   c                       r
   )PhysPkgWriterz6
    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    '   r   r    c                       D   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   za
    Implements |PhysPkgReader| interface for an OPC package extracted into a
    directory.
    c                    s    t t|   tj|| _dS )zS
        *path* is the path to a directory containing an expanded package.
        N)r   r   __init__r   r   abspath_path)selfr   r   r   r   r#   4   s   z_DirPkgReader.__init__c                 C   sJ   t j| j|j}t|d}| }W d   |S 1 sw   Y  |S )zc
        Return 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   closeE   s   z_DirPkgReader.closec                 C   
   |  tS )zI
        Return the `[Content_Types].xml` blob from the package.
        r/   r	   r0   r   r   r   content_types_xmlL      
z_DirPkgReader.content_types_xmlc                 C   ,   z	|  |j}W |S  ty   d}Y |S w )zr
        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_forS      z_DirPkgReader.rels_xml_forr   r   r   r   r#   r/   r1   propertyr4   r:   r   r   r   r   r   r   /   s    

r   c                       r"   )r   zJ
    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#   c   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.
        )r?   r+   r)   )r&   r,   r   r   r   r/   g      z_ZipPkgReader.blob_forc                 C      | j   dS )zM
        Close the zip archive, releasing any resources it is using.
        Nr?   r1   r0   r   r   r   r1   n   s   z_ZipPkgReader.closec                 C   r2   )zM
        Return the `[Content_Types].xml` blob from the zip package.
        r3   r0   r   r   r   r4   t   r5   z_ZipPkgReader.content_types_xmlc                 C   r6   )zo
        Return rels item XML for source with *source_uri* or None if no rels
        item is present.
        N)r/   r7   KeyErrorr9   r   r   r   r:   {   r;   z_ZipPkgReader.rels_xml_forr<   r   r   r   r   r   _   s    
r   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )r!   zJ
    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   r?   r@   r   r   r   r#      s   z_ZipPkgWriter.__init__c                 C   rB   )z}
        Close the zip archive, flushing any pending physical writes and
        releasing any resources it's using.
        NrC   r0   r   r   r   r1      rA   z_ZipPkgWriter.closec                 C   s   | j |j| dS )zk
        Write *blob* to this zip package with the membername corresponding to
        *pack_uri*.
        N)r?   writestrr)   )r&   r,   r.   r   r   r   write   s   z_ZipPkgWriter.write)r   r   r   r   r#   r1   rH   r   r   r   r   r   r!      s
    r!   )r   Z
__future__r   r   zipfiler   r   r   compatr   
exceptionsr   Zpackurir	   objectr   r    r   r   r!   r   r   r   r   <module>   s   0(