o
    Z2h9"                     @  s   d Z ddlmZ ddlmZmZmZmZ ddlm	Z
 ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ erZddlmZ ddlmZ ddlmZ G dd dZG dd dZdS )z8Objects that implement reading and writing OPC packages.    )annotations)IOTYPE_CHECKINGIteratorcast)RELATIONSHIP_TYPE)PACKAGE_URIPackURI)PartFactory)CorePropertiesPart)PackageReader)PackageWriter)Relationships)lazyproperty)CoreProperties)Part)_Relationshipc                      s   e Zd ZdZ fddZdd Zed4dd	Zd5ddZd6ddZ	d7d8ddZ
edd Zd9ddZed:d"d#Zd;d%d&Zed<d(d)Zd=d+d,Zed-d. Zd>d/d0Zed?d2d3Z  ZS )@
OpcPackagezMain API class for |python-opc|.

    A new instance is constructed by calling the :meth:`open` class method with a path
    to a package file or file-like object containing one.
    c                   s   t t|   d S N)superr   __init__self	__class__ a/home/skpark/git/infrasmart_work/infrasmart/venv/lib/python3.10/site-packages/docx/opc/package.pyr      s   zOpcPackage.__init__c                 C  s   dS )zEntry point for any post-unmarshaling processing.

        May be overridden by subclasses without forwarding call to super.
        Nr   r   r   r   r   after_unmarshal    s   zOpcPackage.after_unmarshalreturnr   c                 C  s   | j jS )zl|CoreProperties| object providing read/write access to the Dublin Core
        properties for this document.)_core_properties_partcore_propertiesr   r   r   r   r    )   s   zOpcPackage.core_propertiesIterator[_Relationship]c                 #  s,    	d
d fdd	  | D ]}|V  qdS )zGenerate exactly one reference to each relationship in the package by
        performing a depth-first traversal of the rels graph.NsourceOpcPackage | Partvisitedlist[Part] | Noner   r!   c                 3  sh    |d u rg n|}| j  D ]#}|V  |jrq|j}||v rq|| |} ||D ]}|V  q+qd S r   relsvaluesis_externaltarget_partappendr"   r$   relpart
new_source	walk_relsr   r   r1   3   s   
z'OpcPackage.iter_rels.<locals>.walk_relsr   )r"   r#   r$   r%   r   r!   r   )r   r-   r   r0   r   	iter_rels/   s   zOpcPackage.iter_relsIterator[Part]c                 #  s*    g f fdd	  | D ]}|V  qdS )zGenerate exactly one reference to each of the parts in the package by
        performing a depth-first traversal of the rels graph.c                 3  sX    | j  D ]#}|jrq|j}||v rq|| |V  |} ||D ]}|V  q#qd S r   r&   r,   
walk_partsr   r   r5   J   s   
z)OpcPackage.iter_parts.<locals>.walk_partsNr   )r   r.   r   r4   r   
iter_partsF   s
   zOpcPackage.iter_partsFreltypestrtarget
Part | strrIdr)   boolc                 C  s   | j ||||S )a}  Return newly added |_Relationship| instance of `reltype` between this part
        and `target` with key `rId`.

        Target mode is set to ``RTM.EXTERNAL`` if `is_external` is |True|. Intended for
        use during load from a serialized package, where the rId is well known. Other
        methods exist for adding a new relationship to the package during processing.
        )r'   add_relationship)r   r7   r9   r;   r)   r   r   r   load_relZ   s   zOpcPackage.load_relc                 C  s   |  tjS )a  Return a reference to the main document part for this package.

        Examples include a document part for a WordprocessingML package, a presentation
        part for a PresentationML package, or a workbook part for a SpreadsheetML
        package.
        )part_related_byRTOFFICE_DOCUMENTr   r   r   r   main_document_partd   s   zOpcPackage.main_document_parttemplater	   c                 C  sJ   dd |   D }tdt|d D ]}|| }||vr"t|  S qdS )a  Return a |PackURI| instance representing partname matching `template`.

        The returned part-name has the next available numeric suffix to distinguish it
        from other parts of its type. `template` is a printf (%)-style template string
        containing a single replacement item, a '%d' to be used to insert the integer
        portion of the partname. Example: "/word/header%d.xml"
        c                 S  s   h | ]}|j qS r   )partname).0r.   r   r   r   	<setcomp>v   s    z+OpcPackage.next_partname.<locals>.<setcomp>      N)r6   rangelenr	   )r   rC   	partnamesncandidate_partnamer   r   r   next_partnamen   s   zOpcPackage.next_partnamepkg_filestr | IO[bytes]c                 C  s"   t |}|  }t||t |S )zGReturn an |OpcPackage| instance loaded with the contents of `pkg_file`.)r   	from_fileUnmarshaller	unmarshalr
   )clsrO   
pkg_readerpackager   r   r   open|   s   
zOpcPackage.openr   c                 C  s   | j |S )zReturn part to which this package has a relationship of `reltype`.

        Raises |KeyError| if no such relationship is found and |ValueError| if more than
        one such relationship is found.
        )r'   part_with_reltype)r   r7   r   r   r   r?      s   zOpcPackage.part_related_by
list[Part]c                 C  s   t |  S )zJReturn a list containing a reference to each of the parts in this package.)listr6   r   r   r   r   parts   s   zOpcPackage.partsr.   c                 C  s   | j ||}|jS )zReturn rId key of new or existing relationship to `part`.

        If a relationship of `reltype` to `part` already exists, its rId is returned. Otherwise a
        new relationship is created and that rId is returned.
        )r'   
get_or_addr;   )r   r.   r7   r-   r   r   r   	relate_to   s   zOpcPackage.relate_toc                 C  s
   t tjS )ztReturn a reference to the |Relationships| instance holding the collection of
        relationships for this package.)r   r   baseURIr   r   r   r   r'      s   
zOpcPackage.relsc                 C  s*   | j D ]}|  qt|| j| j  dS )znSave this package to `pkg_file`.

        `pkg_file` can be either a file-path or a file-like object.
        N)r[   before_marshalr   writer'   )r   rO   r.   r   r   r   save   s   

zOpcPackage.saver   c                 C  sD   z
t t| tjW S  ty!   t| }| |tj | Y S w )z|CorePropertiesPart| object related to this package.

        Creates a default core properties part if one is not present (not common).
        )r   r   r?   r@   CORE_PROPERTIESKeyErrordefaultr]   )r   core_properties_partr   r   r   r      s   
z OpcPackage._core_properties_part)r   r   )r   r!   )r   r3   )F)r7   r8   r9   r:   r;   r8   r)   r<   )rC   r8   r   r	   )rO   rP   r   r   )r7   r8   r   r   )r   rY   )r.   r   r7   r8   )rO   rP   )r   r   )__name__
__module____qualname____doc__r   r   propertyr    r2   r6   r>   rB   rN   classmethodrW   r?   r[   r]   r   r'   ra   r   __classcell__r   r   r   r   r      s.    	




	

	

	r   c                   @  s4   e Zd ZdZedd Zedd Zedd ZdS )	rR   zHHosts static methods for unmarshalling a package from a |PackageReader|.c                 C  s>   t | ||}t | || | D ]}|  q|  dS )zConstruct graph of parts and realized relationships based on the contents of
        `pkg_reader`, delegating construction of each part to `part_factory`.

        Package relationships are added to `pkg`.
        N)rR   _unmarshal_parts_unmarshal_relationshipsr(   r   )rU   rV   part_factoryr[   r.   r   r   r   rS      s
   
zUnmarshaller.unmarshalc                 C  s2   i }|   D ]\}}}}||||||||< q|S )zReturn a dictionary of |Part| instances unmarshalled from `pkg_reader`, keyed
        by partname.

        Side-effect is that each part in `pkg_reader` is constructed using
        `part_factory`.
        )iter_sparts)rU   rV   ro   r[   rD   content_typer7   blobr   r   r   rm      s   zUnmarshaller._unmarshal_partsc                 C  sV   |   D ]$\}}|dkr|n|| }|jr|jn||j }||j||j|j qdS )zAdd a relationship to the source object corresponding to each of the
        relationships in `pkg_reader` with its target_part set to the actual target part
        in `parts`./N)
iter_srelsr)   
target_reftarget_partnamer>   r7   r;   )rU   rV   r[   
source_urisrelr"   r9   r   r   r   rn      s
   z%Unmarshaller._unmarshal_relationshipsN)rf   rg   rh   ri   staticmethodrS   rm   rn   r   r   r   r   rR      s    

rR   N) ri   
__future__r   typingr   r   r   r   docx.opc.constantsr   r@   docx.opc.packurir   r	   docx.opc.partr
   docx.opc.parts.corepropsr   docx.opc.pkgreaderr   docx.opc.pkgwriterr   docx.opc.relr   docx.sharedr   docx.opc.corepropsr   r   r   r   rR   r   r   r   r   <module>   s$     "