
    pj9"                        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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                       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d0d$            Z xZS )1
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                V    t          t          |                                            d S N)superr   __init__)self	__class__s    `/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/docx/opc/package.pyr   zOpcPackage.__init__   s%    j$((*****    c                    dS )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   returnr   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9r   Iterator[_Relationship]c              #  @   K   	 d	d
fd |           D ]}|V  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     K   |g n|}| j                                         D ]D}|V  |j        r|j        }||v r|                    |           |} ||          D ]}|V  Ed S r   relsvaluesis_externaltarget_partappend)r(   r*   relpart
new_source	walk_relss        r   r6   z'OpcPackage.iter_rels.<locals>.walk_rels3   s       $ObbG{))++ 
 
			? 7??t$$$!
$9Z99  CIIII
 
r   r   )r(   r)   r*   r+   r"   r&   r   )r   r3   r6   s     @r   	iter_relszOpcPackage.iter_rels/   s_      
 EI	 	 	 	 	 	 	  9T?? 	 	CIIII	 	r   Iterator[Part]c              #  >   K   g ffd	 |           D ]}|V  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     K   | j                                         D ]D}|j        r
|j        }||v r|                    |           |V  |} ||          D ]}|V  Ed S r   r-   )r(   r*   r3   r4   r5   
walk_partss        r   r;   z)OpcPackage.iter_parts.<locals>.walk_partsJ   s      {))++ 
 
? 7??t$$$


!
&Jz7;;  DJJJJ
 
r   Nr   )r   r4   r;   s     @r   
iter_partszOpcPackage.iter_partsF   sZ       (* 	 	 	 	 	 	 Jt$$ 	 	DJJJJ	 	r   Freltypestrtarget
Part | strrIdr0   boolc                <    | 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   r=   r?   rA   r0   s        r   load_relzOpcPackage.load_relZ   s      y))'63LLLr   c                @    |                      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777r   templater
   c                    d |                                  D             }t          dt          |          dz             D ]}||z  }||vrt          |          c S 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                    h | ]	}|j         
S r   )partname).0r4   s     r   	<setcomp>z+OpcPackage.next_partname.<locals>.<setcomp>v   s    AAAtT]AAAr         N)r<   rangelenr
   )r   rK   	partnamesncandidate_partnames        r   next_partnamezOpcPackage.next_partnamen   s     BAt/@/@AAA	q#i..1,-- 	3 	3A!)A!22122222 3	3 	3r   pkg_filestr | IO[bytes]c                    t          j        |          } |             }t                              ||t                     |S )zGReturn an |OpcPackage| instance loaded with the contents of `pkg_file`.)r   	from_fileUnmarshaller	unmarshalr   )clsrY   
pkg_readerpackages       r   openzOpcPackage.open|   s<     #,X66
#%%z7K@@@r   r   c                6    | 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   rG   zOpcPackage.part_related_by   s     y**7333r   
list[Part]c                D    t          |                                           S )zJReturn a list containing a reference to each of the parts in this package.)listr<   r    s    r   partszOpcPackage.parts   s     DOO%%&&&r   r4   c                F    | 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_addrA   )r   r4   r=   r3   s       r   	relate_tozOpcPackage.relate_to   s"     i""7D11w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111r   c                    | j         D ]}|                                 t          j        || j        | j                    dS )znSave this package to `pkg_file`.

        `pkg_file` can be either a file-path or a file-like object.
        N)rh   before_marshalr   writer.   )r   rY   r4   s      r   savezOpcPackage.save   sI    
 J 	" 	"D!!!!Hdi<<<<<r   r   c                    	 t          t          |                     t          j                            S # t
          $ r9 t          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   rG   rH   CORE_PROPERTIESKeyErrordefaultrk   )r   core_properties_parts     r   r$   z OpcPackage._core_properties_part   sy    	(*D,@,@AS,T,TUUU 	( 	( 	(#5#=d#C#C NN/1CDDD''''	(s   14 A A76A7)r"   r   )r"   r&   )r"   r8   )F)r=   r>   r?   r@   rA   r>   r0   rB   )rK   r>   r"   r
   )rY   rZ   r"   r   )r=   r>   r"   r   )r"   re   )r4   r   r=   r>   )rY   rZ   )r"   r   )__name__
__module____qualname____doc__r   r!   propertyr%   r7   r<   rE   rJ   rX   classmethodrb   rG   rh   rk   r   r.   rq   r$   __classcell__)r   s   @r   r   r      s        + + + + +   : : : X:
   .   (M M M M M 8 8 X83 3 3 3    [4 4 4 4 ' ' ' X'    2 2 \2
= = = = 
( 
( 
( X
( 
( 
( 
( 
(r   r   c                  T    e Zd ZdZed             Zed             Zed             ZdS )r]   zHHosts static methods for unmarshalling a package from a |PackageReader|.c                    t                               | ||          }t                               | ||           |                                D ]}|                                 |                                 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)r]   _unmarshal_parts_unmarshal_relationshipsr/   r!   )r`   ra   part_factoryrh   r4   s        r   r^   zUnmarshaller.unmarshal   sw     --j'<PP--j'5IIILLNN 	# 	#D  """"!!!!!r   c                f    i }|                                  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)r`   ra   r   rh   rN   content_typer=   blobs           r   r   zUnmarshaller._unmarshal_parts   sR     5?5K5K5M5M 	[ 	[1HlGT*l8\7DRYZZE(OOr   c                    |                                  D ]W\  }}|dk    r|n||         }|j        r|j        n||j                 }|                    |j        ||j        |j                   Xd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_srelsr0   
target_reftarget_partnamerE   r=   rA   )r`   ra   rh   
source_urisrelr(   r?   s          r   r   z%Unmarshaller._unmarshal_relationships   s    
 !+ 5 5 7 7 	N 	NJ *c 1 1WWuZ7HF(,(8YT__eDDX>YFOODL&$(D<LMMMM	N 	Nr   N)rw   rx   ry   rz   staticmethodr^   r   r   r   r   r   r]   r]      sn        RR
" 
" \
" 
 
 \
 N N \N N Nr   r]   N) rz   
__future__r   typingr   r   r   r   docx.opc.constantsr   rH   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      s   > > " " " " " " 4 4 4 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 1 1 1 1 1 1 1 1 % % % % % % 7 7 7 7 7 7 , , , , , , , , , , , , & & & & & & $ $ $ $ $ $ +111111""""""******^( ^( ^( ^( ^( ^( ^( ^(B%N %N %N %N %N %N %N %N %N %Nr   