
    Pqh                         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y)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                 *    t         t        |           y N)superr   __init__)self	__class__s    Q/srv/www/ollama/chat-ollama/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              #   <   K   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   r*   z'OpcPackage.iter_rels.<locals>.walk_rels*   s|     #ObG{{))+ 
	??7?t$!
$Z9 CI
s   A.A1r   r   )r   r'   r*   s     @r   	iter_relszOpcPackage.iter_rels&   s&     	 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'   r(   r)   
walk_partss        r   r.   z)OpcPackage.iter_parts.<locals>.walk_parts?   sq     {{))+ 
??7?t$
!
&z7; DJ
s   A(A+Nr   )r   r(   r.   s     @r   
iter_partszOpcPackage.iter_parts;   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_relO   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_partY   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)r/   partnamerangelenr   )r   templater(   	partnamesncandidate_partnames         r   next_partnamezOpcPackage.next_partnamec   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.openq   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   r2   s     r   r7   zOpcPackage.part_related_byy   s     yy**733r   c                 4    t        | j                               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                 R    | j                   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#     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)zvSave this package to `pkg_file`, where `file` can be either a path to a file
        (a string) or a file-like object.N)rS   before_marshalr	   writer    )r   rK   r(   s      r   savezOpcPackage.save   s=     JJ 	"D!	"Hdii<r   c                     	 | 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).
        )r7   r8   CORE_PROPERTIESKeyErrorr   defaultrV   )r   core_properties_parts     r   r   z OpcPackage._core_properties_part   sY    	(''(:(:;; 	(#5#=#=d#C NN/1C1CD''	(s   ! A A$#A$)F)__name__
__module____qualname____doc__r   r   propertyr   r+   r/   r5   r:   rE   classmethodrN   r7   rS   rV   r   r    r\   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)rH   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)rH   _unmarshal_parts_unmarshal_relationshipsr!   r   )rL   rM   part_factoryrS   r(   s        r   rI   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)rL   rM   rm   rS   r>   content_typer2   blobs           r   rk   zUnmarshaller._unmarshal_parts   sJ     5?5K5K5M 	1HlGT*,wE(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_partnamer5   r2   r4   )rL   rM   rS   
source_urisrelr%   r3   s          r   rl   z%Unmarshaller._unmarshal_relationships   sy    
 !+ 5 5 7 	NJ *c 1WuZ7HF#'#3#3t?S?S9T  OODLL&$((D<L<LM	Nr   N)rb   rc   rd   re   staticmethodrI   rk   rl   r   r   r   rH   rH      sA    R
" 
"   	N 	Nr   rH   N)re   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   rH   r   r   r   <module>r      s9    > 6 1 % 7 , , & (W( W(t)N )Nr   