
    hi"                        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          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)Self)CoreProperties)Part)_Relationshipc                      e Zd Z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d0d"Zed1d$            Zd%S )2
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                    dS )zEntry point for any post-unmarshaling processing.

        May be overridden by subclasses without forwarding call to super.
        N selfs    h/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/docx/opc/package.pyafter_unmarshalzOpcPackage.after_unmarshal   s	     	    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 Nrelsvaluesis_externaltarget_partappend)r$   r&   relpart
new_source	walk_relss        r   r3   z'OpcPackage.iter_rels.<locals>.walk_rels2   s       $ObbG{))++ 
 
			? 7??t$$$!
$9Z99  CIIII
 
r   r)   )r$   r%   r&   r'   r   r"   r   )r   r0   r3   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&   r0   r1   r2   
walk_partss        r   r8   z)OpcPackage.iter_parts.<locals>.walk_partsI   s      {))++ 
 
? 7??t$$$


!
&Jz7;;  DJJJJ
 
r   Nr   )r   r1   r8   s     @r   
iter_partszOpcPackage.iter_partsE   sZ       (* 	 	 	 	 	 	 Jt$$ 	 	DJJJJ	 	r   Freltypestrtarget
Part | strrIdr-   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<   r>   r-   s        r   load_relzOpcPackage.load_relY   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_partc   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).0r1   s     r   	<setcomp>z+OpcPackage.next_partname.<locals>.<setcomp>u   s    AAAtT]AAAr         N)r9   rangelenr
   )r   rH   	partnamesncandidate_partnames        r   next_partnamezOpcPackage.next_partnamem   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]r   c                    t          j        |          } |             }t                              ||t                     |S )zGReturn an |OpcPackage| instance loaded with the contents of `pkg_file`.)r   	from_fileUnmarshaller	unmarshalr   )clsrV   
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   rD   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.)listr9   r   s    r   partszOpcPackage.parts   s     DOO%%&&&r   r1   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_addr>   )r   r1   r:   r0   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)re   before_marshalr   writer+   )r   rV   r1   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   rD   rE   CORE_PROPERTIESKeyErrordefaultrh   )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N)r   r   )r   r"   )r   r5   )F)r:   r;   r<   r=   r>   r;   r-   r?   )rH   r;   r   r
   )rV   rW   r   r   )r:   r;   r   r   )r   rb   )r1   r   r:   r;   )rV   rW   )r   r   )__name__
__module____qualname____doc__r   propertyr!   r4   r9   rB   rG   rU   classmethodr_   rD   re   rh   r   r+   rn   r    r   r   r   r   r      sy           : : : 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 )rZ   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)rZ   _unmarshal_parts_unmarshal_relationshipsr,   r   )r]   r^   part_factoryre   r1   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]   r^   r~   re   rK   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_srelsr-   
target_reftarget_partnamerB   r:   r>   )r]   r^   re   
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)rt   ru   rv   rw   staticmethodr[   r|   r}   r   r   r   rZ   rZ      sn        RR
" 
" \
" 
 
 \
 N N \N N Nr   rZ   N)"rw   
__future__r   typingr   r   r   r   docx.opc.constantsr   rE   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   rZ   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""""""******[( [( [( [( [( [( [( [(|%N %N %N %N %N %N %N %N %N %Nr   