Xj"dZddlmZddlmZmZmZmZddlm Z ddl m Z m Z ddlmZddlmZddlmZdd lmZdd lmZdd lmZerdd lmZdd lmZddlmZddlmZGddZ GddZ!y)z8Objects that implement reading and writing OPC packages.) annotations)IO TYPE_CHECKINGIteratorcast)RELATIONSHIP_TYPE) PACKAGE_URIPackURI) PartFactory)CorePropertiesPart) PackageReader) PackageWriter) Relationships) lazyproperty)Self)CoreProperties)Part) _RelationshipceZdZdZdZeddZddZddZdddZ edZ ddZ e dd Z dd Zedd Zdd Zed ZddZeddZy) 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. cy)zEntry point for any post-unmarshaling processing. May be overridden by subclasses without forwarding call to super. Nselfs d/mnt/ssd/data/Dropbox/adrian/vault-secondbrain/venv/lib/python3.12/site-packages/docx/opc/package.pyafter_unmarshalzOpcPackage.after_unmarshals c.|jjS)zl|CoreProperties| object providing read/write access to the Dublin Core properties for this document.)_core_properties_partcore_propertiesrs rr zOpcPackage.core_properties(s))999rc#JK d dfd |D]}|yw)zGenerate exactly one reference to each relationship in the package by performing a depth-first traversal of the rels graph.Nc3K|gn|}|jjD]I}||jr|j}||vr%|j ||}||D]}|KywNrelsvalues is_external target_partappend)sourcevisitedrelpart new_source walk_relss rr/z'OpcPackage.iter_rels..walk_rels2s~$ObG{{))+  ??7?t$! $Z9CI sA.A1r#)r*zOpcPackage | Partr+zlist[Part] | NonereturnIterator[_Relationship]r)rr,r/s @r iter_relszOpcPackage.iter_rels.sE EI % 0A  $  T? CI s #c#>Kgffd |D]}|yw)zGenerate exactly one reference to each of the parts in the package by performing a depth-first traversal of the rels graph.c3K|jjD]I}|jr|j}||vr!|j |||}||D]}|Kywr#r$)r*r+r,r-r. walk_partss rr5z)OpcPackage.iter_parts..walk_partsIsq{{))+ ??7?t$ ! &z7;DJ sA(A+Nr)rr-r5s @r iter_partszOpcPackage.iter_partsEs,(* t$ DJ sc>|jj||||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)rreltypetargetrIdr's rload_relzOpcPackage.load_relYsyy))'63 LLrc@|jtjS)aReturn 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_byRTOFFICE_DOCUMENTrs rmain_document_partzOpcPackage.main_document_partcs##B$6$677rc|jDchc]}|j}}tdt|dzD]}||z}||vs t |cSycc}w)aReturn 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)r6partnamerangelenr )rtemplater- partnamesncandidate_partnames r next_partnamezOpcPackage.next_partnamemse04/@AtT]]A Aq#i.1,- 3A!)A !2122 3BsActtj|}|}tj||t|S)zGReturn an |OpcPackage| instance loaded with the contents of `pkg_file`.)r from_file Unmarshaller unmarshalr )clspkg_file pkg_readerpackages ropenzOpcPackage.open{s2#,,X6 %z7K@rc8|jj|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)rr9s rr>zOpcPackage.part_related_bys yy**733rc4t|jS)zJReturn a list containing a reference to each of the parts in this package.)listr6rs rpartszOpcPackage.partssDOO%&&rcR|jj||}|jS)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;)rr-r9r,s r relate_tozOpcPackage.relate_tos# ii""7D1wwrc4ttjS)ztReturn a reference to the |Relationships| instance holding the collection of relationships for this package.)rr baseURIrs rr%zOpcPackage.relss[0011rc|jD]}|jtj||j|jy)znSave this package to `pkg_file`. `pkg_file` can be either a file-path or a file-like object. N)rZbefore_marshalrwriter%)rrRr-s rsavezOpcPackage.saves= JJ "D    ! "Hdiir?CORE_PROPERTIESKeyErrordefaultr])rcore_properties_parts rrz OpcPackage._core_properties_partsa  (*D,@,@ASAS,TU U (#5#=#=d#C NN/1C1C D' ' (s,/AA21A2N)r0r)r0r1)r0zIterator[Part])F)r9strr:z Part | strr;rir'bool)rHrir0r )rRstr | IO[bytes]r0r)r9rir0r)r0z list[Part])r-rr9ri)rRrk)r0r )__name__ __module__ __qualname____doc__rpropertyr r2r6r<rArL classmethodrUr>rZr]rr%rcrrrrrrs  :: .(M88 34''22 = ( (rrc@eZdZdZedZedZedZy)rOzHHosts static methods for unmarshalling a package from a |PackageReader|.ctj|||}tj||||jD]}|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)rO_unmarshal_parts_unmarshal_relationshipsr&r)rSrT part_factoryrZr-s rrPzUnmarshaller.unmarshalsW--j'<P--j'5ILLN #D  " #!rc\i}|jD]\}}}}||||||||<|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)rSrTrvrZrE content_typer9blobs rrtzUnmarshaller._unmarshal_partssH5?5K5K5M [ 1HlGT*8\7DRYZE(O [ rc|jD]j\}}|dk(r|n||}|jr |jn||j}|j |j ||j |jly)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_reftarget_partnamer<r9r;)rSrTrZ source_urisrelr*r:s rruz%Unmarshaller._unmarshal_relationshipsst !+ 5 5 7 N J *c 1WuZ7HF(,(8(8T__eDDXDX>YF OODLL&$((DrsQ>"4461%7,,&$&1"*[([(|%N%Nr