o
    	'f                     @   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                           e Zd ZdZ fddZ  ZS )PhysPkgReaderz,Factory for physical package reader objects.c                    sJ   t |trtj|rt}nt|rt}ntd| t}t	t
| |S )NzPackage not found at '%s')
isinstancestrospathisdir_DirPkgReaderr   _ZipPkgReaderr   superr   __new__)clspkg_file
reader_cls	__class__ I/home/ubuntu/flask/venv/lib/python3.10/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   )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       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   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   sJ   t j| j|j}t|d}| }W d   |S 1 sw   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   
   |  tS )z7Return the `[Content_Types].xml` blob from the package.r/   r   r0   r   r   r   content_types_xml8      
z_DirPkgReader.content_types_xmlc                 C   ,   z	|  |j}W |S  ty   d}Y |S w )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&   
source_urirels_xmlr   r   r   rels_xml_for=      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   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.
        )rA   r+   r)   )r&   r,   r   r   r   r/   N   s   z_ZipPkgReader.blob_forc                 C      | j   dS )z;Close the zip archive, releasing any resources it is using.NrA   r1   r0   r   r   r   r1   U   s   z_ZipPkgReader.closec                 C   r2   )z;Return the `[Content_Types].xml` blob from the zip package.r3   r0   r   r   r   r4   Y   r5   z_ZipPkgReader.content_types_xmlc                 C   r6   )z]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   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   rA   rB   r   r   r   r#   k   s   z_ZipPkgWriter.__init__c                 C   rC   )zkClose the zip archive, flushing any pending physical writes and releasing any
        resources it's using.NrD   r0   r   r   r   r1   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)rA   writestrr)   )r&   r,   r.   r   r   r   writet   s   z_ZipPkgWriter.write)r   r   r   r   r#   r1   rI   r   r   r   r   r   r!   h   s
    r!   )r   r   zipfiler   r   r   docx.opc.exceptionsr   docx.opc.packurir   r   r    r   r   r!   r   r   r   r   <module>   s    $!