
    ,h9"                        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  G d d      Z G d d      Zy)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                       e Zd ZdZ fdZd Zedd       ZddZddZ	dddZ
ed        Zdd	Zedd
       ZddZedd       ZddZed        ZddZedd       Z x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                *    t         t        |           y N)superr   __init__)self	__class__s    R/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/docx/opc/package.pyr   zOpcPackage.__init__   s    j$(*    c                     y)zEntry point for any post-unmarshaling processing.

        May be overridden by subclasses without forwarding call to super.
        N r   s    r   after_unmarshalzOpcPackage.after_unmarshal    s     	r   c                .    | j                   j                  S )zl|CoreProperties| object providing read/write access to the Dublin Core
        properties for this document.)_core_properties_partcore_propertiesr    s    r   r$   zOpcPackage.core_properties)   s     ))999r   c              #  J   K   	 d	 	 	 	 	 dfd |       D ]  }|  yw)zGenerate exactly one reference to each relationship in the package by
        performing a depth-first traversal of the rels graph.Nc              3     K   |g n|}| j                   j                         D ]I  }| |j                  r|j                  }||v r%|j	                  |       |} ||      D ]  }|  K y wr   relsvaluesis_externaltarget_partappend)sourcevisitedrelpart
new_source	walk_relss        r   r2   z'OpcPackage.iter_rels.<locals>.walk_rels3   s~      $ObG{{))+ 
	??7?t$!
$Z9 CI
s   A.A1r   )r-   zOpcPackage | Partr.   zlist[Part] | NonereturnIterator[_Relationship]r   )r   r/   r2   s     @r   	iter_relszOpcPackage.iter_rels/   sE     
 EI	%	0A	$	  T? 	CI	s    #c              #  >   K   g ffd	 |       D ]  }|  yw)zGenerate exactly one reference to each of the parts in the package by
        performing a depth-first traversal of the rels graph.c              3     K   | j                   j                         D ]I  }|j                  r|j                  }||v r!|j	                  |       | |} ||      D ]  }|  K y wr   r'   )r-   r.   r/   r0   r1   
walk_partss        r   r8   z)OpcPackage.iter_parts.<locals>.walk_partsJ   sq     {{))+ 
??7?t$
!
&z7; DJ
s   A(A+Nr   )r   r0   r8   s     @r   
iter_partszOpcPackage.iter_partsF   s,      (* 	 t$ 	DJ	s   c                >    | j                   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   reltypetargetrIdr*   s        r   load_relzOpcPackage.load_relZ   s     yy))'63LLr   c                @    | j                  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    s    r   main_document_partzOpcPackage.main_document_partd   s     ##B$6$677r   c                    | j                         D ch c]  }|j                   }}t        dt        |      dz         D ]  }||z  }||vst	        |      c S  yc c}w )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"
              N)r9   partnamerangelenr
   )r   templater0   	partnamesncandidate_partnames         r   next_partnamezOpcPackage.next_partnamen   se     04/@AtT]]A	Aq#i.1,- 	3A!)A!2122	3 Bs   Ac                t    t        j                  |      } |        }t        j                  ||t               |S )zGReturn an |OpcPackage| instance loaded with the contents of `pkg_file`.)r   	from_fileUnmarshaller	unmarshalr   )clspkg_file
pkg_readerpackages       r   openzOpcPackage.open|   s2     #,,X6
%z7K@r   c                8    | j                   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   r<   s     r   rA   zOpcPackage.part_related_by   s     yy**733r   c                4    t        | j                               S )zJReturn a list containing a reference to each of the parts in this package.)listr9   r    s    r   partszOpcPackage.parts   s     DOO%&&r   c                R    | j                   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   r0   r<   r/   s       r   	relate_tozOpcPackage.relate_to   s#     ii""7D1wwr   c                4    t        t        j                        S )ztReturn a reference to the |Relationships| instance holding the collection of
        relationships for this package.)r   r	   baseURIr    s    r   r(   zOpcPackage.rels   s     [0011r   c                    | j                   D ]  }|j                           t        j                  || j                  | j                          y)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   rU   r0   s      r   savezOpcPackage.save   s=    
 JJ 	"D!	"Hdii<r   c                    	 t        t        | j                  t        j                              S # t
        $ r: t        j                  |       }| j                  |t        j                         |cY S w xY w)z|CorePropertiesPart| object related to this package.

        Creates a default core properties part if one is not present (not common).
        )r   r   rA   rB   CORE_PROPERTIESKeyErrordefaultr`   )r   core_properties_parts     r   r#   z OpcPackage._core_properties_part   sa    	(*D,@,@ASAS,TUU 	(#5#=#=d#C NN/1C1CD''	(s   ,/ A A21A2)r3   r   )r3   r4   )r3   zIterator[Part])F)r<   strr=   z
Part | strr>   rl   r*   bool)rK   rl   r3   r
   )rU   str | IO[bytes]r3   r   )r<   rl   r3   r   )r3   z
list[Part])r0   r   r<   rl   )rU   rn   )r3   r   )__name__
__module____qualname____doc__r   r!   propertyr$   r5   r9   r?   rD   rO   classmethodrX   rA   r]   r`   r   r(   rf   r#   __classcell__)r   s   @r   r   r      s    + : :
.(M 8 83  4 ' ' 2 2
= 
( 
(r   r   c                  @    e Zd ZdZed        Zed        Zed        Zy)rR   zHHosts static methods for unmarshalling a package from a |PackageReader|.c                    t         j                  | ||      }t         j                  | ||       |j                         D ]  }|j	                           |j	                          y)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!   )rV   rW   part_factoryr]   r0   s        r   rS   zUnmarshaller.unmarshal   sW     --j'<P--j'5ILLN 	#D  "	#!r   c                \    i }| j                         D ]  \  }}}} ||||||      ||<    |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)rV   rW   rz   r]   rH   content_typer<   blobs           r   rx   zUnmarshaller._unmarshal_parts   sH     5?5K5K5M 	[1HlGT*8\7DRYZE(O	[r   c                    | j                         D ]j  \  }}|dk(  r|n||   }|j                  r|j                  n||j                     }|j	                  |j
                  ||j                  |j                         l y)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?   r<   r>   )rV   rW   r]   
source_urisrelr-   r=   s          r   ry   z%Unmarshaller._unmarshal_relationships   st    
 !+ 5 5 7 	NJ *c 1WuZ7HF(,(8(8T__eDDXDX>YFOODLL&$((D<L<LM	Nr   N)ro   rp   rq   rr   staticmethodrS   rx   ry   r   r   r   rR   rR      sA    R
" 
" 
 
 N Nr   rR   N) rr   
__future__r   typingr   r   r   r   docx.opc.constantsr   rB   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   <module>r      sN    > " 4 4 6 1 % 7 , , & $1"*^( ^(B%N %Nr   