
    h9"                        S r SSKJr  SSKJrJrJrJr  SSKJ	r
  SSKJrJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  \(       a  SSKJr  SSKJr  SSKJr   " S S5      r " S S5      rg)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                     ^  \ rS rSrSrU 4S jrS r\SS j5       rSS jr	SS jr
SSS jjr\S	 5       rSS
 jr\SS j5       rSS jr\SS j5       rSS jr\S 5       rSS jr\SS j5       rSrU =r$ )
OpcPackage   zMain 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                *   > [         [        U ]  5         g N)superr   __init__)self	__class__s    B/var/www/html/env/lib/python3.13/site-packages/docx/opc/package.pyr   OpcPackage.__init__   s    j$(*    c                    g)zuEntry point for any post-unmarshaling processing.

May be overridden by subclasses without forwarding call to super.
N r   s    r   after_unmarshalOpcPackage.after_unmarshal    s     	r   c                .    U R                   R                  $ )zd|CoreProperties| object providing read/write access to the Dublin Core
properties for this document.)_core_properties_partcore_propertiesr"   s    r   r'   OpcPackage.core_properties)   s     ))999r   c              #  T   ^#     S     SU4S jjjmT" U 5       H  nUv   M	     g7f)z{Generate exactly one reference to each relationship in the package by
performing a depth-first traversal of the rels graph.Nc              3     >#    Uc  / OUnU R                   R                  5        HS  nUv   UR                  (       a  M  UR                  nX1;   a  M-  UR	                  U5        UnT" XA5       H  nUv   M	     MU     g 7fr   relsvaluesis_externaltarget_partappend)sourcevisitedrelpart
new_source	walk_relss        r   r6   'OpcPackage.iter_rels.<locals>.walk_rels3   so      $ObG{{))+	???t$!
$Z9CI : ,s   A:A=r   )r1   zOpcPackage | Partr2   zlist[Part] | NonereturnIterator[_Relationship]r!   )r   r3   r6   s     @r   	iter_relsOpcPackage.iter_rels/   sD     
 EI	%	0A	$	 	  T?CI #s   %(c              #  F   ^#    / 4U4S jjmT" U 5       H  nUv   M	     g7f)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     >#    U R                   R                  5        HS  nUR                  (       a  M  UR                  nX1;   a  M)  UR	                  U5        Uv   UnT" XA5       H  nUv   M	     MU     g 7fr   r+   )r1   r2   r3   r4   r5   
walk_partss        r   r>   )OpcPackage.iter_parts.<locals>.walk_partsJ   sb     {{))+???t$
!
&z;DJ < ,s   A3A6Nr!   )r   r4   r>   s     @r   
iter_partsOpcPackage.iter_partsF   s&      (* 	 t$DJ %s   !c                :    U R                   R                  XX45      $ )aU  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_relOpcPackage.load_relZ   s     yy))'3LLr   c                @    U R                  [        R                  5      $ )zReturn 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_partOpcPackage.main_document_partd   s     ##B$6$677r   c                    U R                  5        Vs1 s H  o"R                  iM     nn[        S[        U5      S-   5       H  nX-  nXS;  d  M  [	        U5      s  $    gs  snf )ak  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)r@   partnamerangelenr
   )r   templater4   	partnamesncandidate_partnames         r   next_partnameOpcPackage.next_partnamen   s]     04/@A/@t]]/@	Aq#i.1,-A!)!2122 . Bs   A c                t    [         R                  " U5      nU " 5       n[        R                  X#[        5        U$ )zGReturn an |OpcPackage| instance loaded with the contents of `pkg_file`.)r   	from_fileUnmarshaller	unmarshalr   )clspkg_file
pkg_readerpackages       r   openOpcPackage.open|   s0     #,,X6
%zK@r   c                8    U R                   R                  U5      $ )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   rD   s     r   rJ   OpcPackage.part_related_by   s     yy**733r   c                4    [        U R                  5       5      $ )zJReturn a list containing a reference to each of the parts in this package.)listr@   r"   s    r   partsOpcPackage.parts   s     DOO%&&r   c                P    U R                   R                  X!5      nUR                  $ )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_addrF   )r   r4   rD   r3   s       r   	relate_toOpcPackage.relate_to   s!     ii""71wwr   c                4    [        [        R                  5      $ )zlReturn a reference to the |Relationships| instance holding the collection of
relationships for this package.)r   r	   baseURIr"   s    r   r,   OpcPackage.rels   s     [0011r   c                    U R                    H  nUR                  5         M     [        R                  " XR                  U R                   5        g)z^Save this package to `pkg_file`.

`pkg_file` can be either a file-path or a file-like object.
N)rj   before_marshalr   writer,   )r   r`   r4   s      r   saveOpcPackage.save   s6    
 JJD! Hii<r   c                     [        [        U R                  [        R                  5      5      $ ! [
         a;    [        R                  " U 5      nU R                  U[        R                  5        Us $ f = f)z|CorePropertiesPart| object related to this package.

Creates a default core properties part if one is not present (not common).
)r   r   rJ   rK   CORE_PROPERTIESKeyErrordefaultrn   )r   core_properties_parts     r   r&    OpcPackage._core_properties_part   sa    	(*D,@,@ASAS,TUU 	(#5#=#=d#C NN/1C1CD''	(s   ,/ AA43A4r!   )r8   r   )r8   r9   )r8   zIterator[Part])F)rD   strrE   z
Part | strrF   r~   r.   bool)rU   r~   r8   r
   )r`   str | IO[bytes]r8   r   )rD   r~   r8   r   )r8   z
list[Part])r4   r   rD   r~   )r`   r   )r8   r   )__name__
__module____qualname____firstlineno____doc__r   r#   propertyr'   r:   r@   rG   rM   rY   classmethodrc   rJ   rj   rn   r   r,   rv   r&   __static_attributes____classcell__)r   s   @r   r   r      s    + : :
.(M 8 83  4 ' ' 2 2
= 
( 
(r   r   c                  H    \ rS rSrSr\S 5       r\S 5       r\S 5       rSr	g)r]      zHHosts static methods for unmarshalling a package from a |PackageReader|.c                    [         R                  XU5      n[         R                  XU5        UR                  5        H  nUR	                  5         M     UR	                  5         g)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)r]   _unmarshal_parts_unmarshal_relationshipsr-   r#   )ra   rb   part_factoryrj   r4   s        r   r^   Unmarshaller.unmarshal   sN     --j<P--j5ILLND  " #!r   c                V    0 nU R                  5        H  u  pEpgU" XEXgU5      X4'   M     U$ )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)ra   rb   r   rj   rR   content_typerD   blobs           r   r   Unmarshaller._unmarshal_parts   s8     5?5K5K5M1HG*87RYZEO 6Nr   c                   U R                  5        Hm  u  p4US:X  a  UOX#   nUR                  (       a  UR                  OX$R                     nUR	                  UR
                  XdR                  UR                  5        Mo     g)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_partnamerG   rD   rF   )ra   rb   rj   
source_urisrelr1   rE   s          r   r   %Unmarshaller._unmarshal_relationships   se    
 !+ 5 5 7J *c 1Wu7HF(,(8(8T__eDXDX>YFOODLL&((D<L<LM !8r   r!   N)
r   r   r   r   r   staticmethodr^   r   r   r   r!   r   r   r]   r]      sA    R
" 
" 
 
 N Nr   r]   N) r   
__future__r   typingr   r   r   r   docx.opc.constantsr   rK   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   r]   r!   r   r   <module>r      sN    > " 4 4 6 1 % 7 , , & $1"*^( ^(B%N %Nr   