B
    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                   s    e Zd ZdZ fddZ  ZS )PhysPkgReaderz6
    Factory for physical package reader objects.
    c                sJ   t |r6tj|rt}q:t|r(t}q:td| nt}tt	| 
|S )NzPackage not found at '%s')r   ospathisdir_DirPkgReaderr   _ZipPkgReaderr   superr
   __new__)clspkg_fileZ
reader_cls)	__class__ C/var/www/html/venv/lib/python3.7/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 )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   )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   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   s4   t j| j|j}t|d}| }W dQ R X |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   s
   |  tS )zI
        Return the `[Content_Types].xml` blob from the package.
        )r*   r	   )r!   r   r   r   content_types_xmlL   s    z_DirPkgReader.content_types_xmlc             C   s.   y|  |j}W n tk
r(   d}Y nX |S )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!   
source_urirels_xmlr   r   r   rels_xml_forS   s
    
z_DirPkgReader.rels_xml_for)r   r   r   r   r   r*   r+   propertyr,   r1   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   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.
        )r4   r&   r$   )r!   r'   r   r   r   r*   g   s    z_ZipPkgReader.blob_forc             C   s   | j   dS )zM
        Close the zip archive, releasing any resources it is using.
        N)r4   r+   )r!   r   r   r   r+   n   s    z_ZipPkgReader.closec             C   s
   |  tS )zM
        Return the `[Content_Types].xml` blob from the zip package.
        )r*   r	   )r!   r   r   r   r,   t   s    z_ZipPkgReader.content_types_xmlc             C   s.   y|  |j}W n tk
r(   d}Y nX |S )zo
        Return rels item XML for source with *source_uri* or None if no rels
        item is present.
        N)r*   r-   KeyError)r!   r/   r0   r   r   r   r1   {   s
    
z_ZipPkgReader.rels_xml_for)r   r   r   r   r   r*   r+   r2   r,   r1   r   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   r4   )r!   r   )r   r   r   r      s    z_ZipPkgWriter.__init__c             C   s   | j   dS )z}
        Close the zip archive, flushing any pending physical writes and
        releasing any resources it's using.
        N)r4   r+   )r!   r   r   r   r+      s    z_ZipPkgWriter.closec             C   s   | j |j| dS )zk
        Write *blob* to this zip package with the membername corresponding to
        *pack_uri*.
        N)r4   writestrr$   )r!   r'   r)   r   r   r   write   s    z_ZipPkgWriter.write)r   r   r   r   r   r+   r9   r   r   r   )r   r   r      s   r   )r   
__future__r   r   zipfiler   r   r   compatr   
exceptionsr   Zpackurir	   objectr
   r   r   r   r   r   r   r   r   <module>   s   0(