o
    ýÞh"  ã                   @  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r`ddlmZ d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)ÚSelf)ÚCoreProperties)ÚPart)Ú_Relationshipc                   @  s°   e Zd Z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.d/„Zed?d1d2„ƒZd3S )@Ú
OpcPackagez·Main 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                 C  s   dS )z…Entry point for any post-unmarshaling processing.

        May be overridden by subclasses without forwarding call to super.
        N© ©Úselfr   r   úP/var/www/html/premium_crap/venv/lib/python3.10/site-packages/docx/opc/package.pyÚ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 )zƒGenerate 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 ©N©ÚrelsÚvaluesÚis_externalÚtarget_partÚappend©r   r    ÚrelÚpartÚ
new_source©Ú	walk_relsr   r   r.   2   s   €
ÿ÷z'OpcPackage.iter_rels.<locals>.walk_relsr"   )r   r   r    r!   r   r   r   )r   r*   r   r-   r   Ú	iter_rels.   s   €ÿÿzOpcPackage.iter_relsúIterator[Part]c                 #  s*   � g f‡ fdd„	‰ ˆ | ƒD ]}|V  qdS )zƒGenerate 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   r2   I   s   €
ÿ÷z)OpcPackage.iter_parts.<locals>.walk_partsNr   )r   r+   r   r1   r   Ú
iter_partsE   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   r4   r6   r8   r&   r   r   r   Úload_relY   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_partc   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>u   s    z+OpcPackage.next_partname.<locals>.<setcomp>é   é   N)r3   ÚrangeÚlenr	   )r   r@   Ú	partnamesÚnÚcandidate_partnamer   r   r   Únext_partnamem   s   ÿþzOpcPackage.next_partnameÚpkg_fileústr | IO[bytes]r   c                 C  s"   t  |¡}| ƒ }t ||t¡ |S )zGReturn an |OpcPackage| instance loaded with the contents of `pkg_file`.)r   Ú	from_fileÚUnmarshallerÚ	unmarshalr
   )ÚclsrL   Ú
pkg_readerÚpackager   r   r   Úopen{   s   
zOpcPackage.openr   c                 C  s   | j  |¡S )zÍReturn 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   r4   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.)Úlistr3   r   r   r   r   Úparts‹   s   zOpcPackage.partsr+   c                 C  s   | j  ||¡}|jS )zãReturn 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_addr8   )r   r+   r4   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)rX   Úbefore_marshalr   Úwriter$   )r   rL   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ÚdefaultrZ   )r   Úcore_properties_partr   r   r   r   ¨   s   
ýz OpcPackage._core_properties_partN)r   r   )r   r   )r   r0   )F)r4   r5   r6   r7   r8   r5   r&   r9   )r@   r5   r   r	   )rL   rM   r   r   )r4   r5   r   r   )r   rV   )r+   r   r4   r5   )rL   rM   )r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r/   r3   r;   r?   rK   ÚclassmethodrT   r<   rX   rZ   r   r$   r^   r   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 )	rO   zHHosts static methods for unmarshalling a package from a |PackageReader|.c                 C  s>   t  | ||¡}t  | ||¡ | ¡ D ]}| ¡  q| ¡  dS )zÖConstruct 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)rO   Ú_unmarshal_partsÚ_unmarshal_relationshipsr%   r   )rR   rS   Úpart_factoryrX   r+   r   r   r   rP   ¹   s
   
zUnmarshaller.unmarshalc                 C  s2   i }|   ¡ D ]\}}}}||||||ƒ||< q|S )zÏReturn 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)rR   rS   rk   rX   rA   Úcontent_typer4   Úblobr   r   r   ri   Æ   s   zUnmarshaller._unmarshal_partsc                 C  sV   |   ¡ D ]$\}}|dkr|n|| }|jr|jn||j }| |j||j|j¡ qdS )z±Add 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;   r4   r8   )rR   rS   rX   Ú
source_uriÚsrelr   r6   r   r   r   rj   Ó   s
   ýz%Unmarshaller._unmarshal_relationshipsN)rc   rd   re   rf   ÚstaticmethodrP   ri   rj   r   r   r   r   rO   ¶   s    

rO   N)"rf   Ú
__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   Útyping_extensionsr   Údocx.opc.corepropsr   r   r   r   rO   r   r   r   r   Ú<module>   s&     