
    j                         d 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  G d
 d          Z G d d          ZdS )z8Objects that implement reading and writing OPC packages.    )RELATIONSHIP_TYPE)PACKAGE_URIPackURI)PartFactory)CorePropertiesPart)PackageReader)PackageWriter)Relationships)lazypropertyc                        e Zd ZdZ fdZd Zed             Zd Zd Z	ddZ
ed	             Zd
 Zed             Zd Zed             Zd Zed             Zd Ze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                 V    t          t          |                                            d S N)superr   __init__)self	__class__s    Z/lsinfo/ai/hellotax_ai/base_platform/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   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   c              #   <   K   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.Nc              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)sourcevisitedrelpart
new_source	walk_relss        r   r*   z'OpcPackage.iter_rels.<locals>.walk_rels*   s      #ObbG{))++ 
 
			? 7??t$$$!
$9Z99  CIIII
 
r   r   r   )r   r'   r*   s     @r   	iter_relszOpcPackage.iter_rels&   sS      	 	 	 	 	 	 9T?? 	 	CIIII	 	r   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&   r'   r(   r)   
walk_partss        r   r.   z)OpcPackage.iter_parts.<locals>.walk_parts?   s      {))++ 
 
? 7??t$$$


!
&Jz7;;  DJJJJ
 
r   Nr   )r   r(   r.   s     @r   
iter_partszOpcPackage.iter_parts;   sZ       (* 	 	 	 	 	 	 Jt$$ 	 	DJJJJ	 	r   Fc                 <    | 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_relO   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_partY   s     ##B$6777r   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).0r(   s     r   	<setcomp>z+OpcPackage.next_partname.<locals>.<setcomp>k   s    AAAtT]AAAr         N)r/   rangelenr   )r   template	partnamesncandidate_partnames        r   next_partnamezOpcPackage.next_partnamec   s     BAt/@/@AAA	q#i..1,-- 	3 	3A!)A!22122222 3	3 	3r   c                     t          j        |          } |             }t                              ||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.openq   s<     #,X66
#%%z7K@@@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   r2   s     r   r7   zOpcPackage.part_related_byy   s     y**7333r   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   c                 F    | j                             ||          }|j        S )zReturn rId key of relationship to `part`, from the existing relationship if
        there is one, otherwise a newly created one.)r    
get_or_addr4   )r   r(   r2   r'   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 )zvSave this package to `pkg_file`, where `file` can be either a path to a file
        (a string) or a file-like object.N)rV   before_marshalr	   writer    )r   rN   r(   s      r   savezOpcPackage.save   sI     J 	" 	"D!!!!Hdi<<<<<r   c                     	 |                      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).
        )r7   r8   CORE_PROPERTIESKeyErrorr   defaultrY   )r   core_properties_parts     r   r   z OpcPackage._core_properties_part   sn    	(''(:;;; 	( 	( 	(#5#=d#C#C NN/1CDDD''''	(s   ! A A$#A$)F)__name__
__module____qualname____doc__r   r   propertyr   r+   r/   r5   r:   rH   classmethodrQ   r7   rV   rY   r   r    r_   r   __classcell__)r   s   @r   r   r      se        + + + + +   : : X:
  *  (M M M M 8 8 X83 3 3   [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 )rK   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)rK   _unmarshal_parts_unmarshal_relationshipsr!   r   )rO   rP   part_factoryrV   r(   s        r   rL   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)rO   rP   rp   rV   r=   content_typer2   blobs           r   rn   zUnmarshaller._unmarshal_parts   sV     5?5K5K5M5M 	 	1HlGT*l,w E(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_partnamer5   r2   r4   )rO   rP   rV   
source_urisrelr%   r3   s          r   ro   z%Unmarshaller._unmarshal_relationships   s    
 !+ 5 5 7 7 	N 	NJ *c 1 1WWuZ7HF#'#3Tt?S9T  OODL&$(D<LMMMM	N 	Nr   N)re   rf   rg   rh   staticmethodrL   rn   ro   r   r   r   rK   rK      sn        RR
" 
" \
"   \ 	N 	N \	N 	N 	Nr   rK   N)rh   docx.opc.constantsr   r8   docx.opc.packurir   r   docx.opc.partr   docx.opc.parts.corepropsr   docx.opc.pkgreaderr   docx.opc.pkgwriterr	   docx.opc.relr
   docx.opc.sharedr   r   rK   r   r   r   <module>r      s   > > 6 6 6 6 6 6 1 1 1 1 1 1 1 1 % % % % % % 7 7 7 7 7 7 , , , , , , , , , , , , & & & & & & ( ( ( ( ( (W( W( W( W( W( W( W( W(t)N )N )N )N )N )N )N )N )N )Nr   