;i"dZddlmZddlmZmZmZmZddlm Z ddl m Z m Z ddlmZddlmZddlmZdd lmZdd lmZdd lmZerdd lmZdd lmZddlmZddlmZGddZ GddZ!dS)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) _RelationshipceZdZdZdZed&dZd'dZd(d Zd)d*dZ edZ d+dZ e d,dZ d-dZed.dZd/d Zed!Zd0d"Zed1d$Zd%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. cdS)zEntry point for any post-unmarshaling processing. May be overridden by subclasses without forwarding call to super. Nselfs DC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\docx/opc/package.pyafter_unmarshalzOpcPackage.after_unmarshals  returnrc|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)99rIterator[_Relationship]c#@K d d fd |D]}|VdS) zGenerate exactly one reference to each relationship in the package by performing a depth-first traversal of the rels graph.NsourceOpcPackage | Partvisitedlist[Part] | Nonerr"c3K|gn|}|jD]D}|V|jr|j}||vr|||}||D]}|VEdSNrelsvalues is_external target_partappend)r$r&relpart new_source walk_relss rr3z'OpcPackage.iter_rels..walk_rels2s$ObbG{))++   ?7??t$$$! $9Z99CIIII  rr))r$r%r&r'rr"r)rr0r3s @r iter_relszOpcPackage.iter_rels.s_ EI        9T??  CIIII  rIterator[Part]c#>Kgffd |D]}|VdS)zGenerate exactly one reference to each of the parts in the package by performing a depth-first traversal of the rels graph.c3K|jD]D}|jr |j}||vr|||V|}||D]}|VEdSr)r*)r$r&r0r1r2 walk_partss rr8z)OpcPackage.iter_parts..walk_partsIs{))++  ?7??t$$$ ! &Jz7;;DJJJJ  rNr)rr1r8s @r iter_partszOpcPackage.iter_partsEsZ(*      Jt$$  DJJJJ  rFreltypestrtarget Part | strrIdr-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)rr:r<r>r-s rload_relzOpcPackage.load_relYs y))'63 LLLrc@|tjS)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$6777rtemplater cd|D}tdt|dzD]}||z}||vrt|cSdS)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" ch|] }|j Sr)partname).0r1s r z+OpcPackage.next_partname..usAAAtT]AAArN)r9rangelenr )rrH partnamesncandidate_partnames r next_partnamezOpcPackage.next_partnamemsBAt/@/@AAA q#i..1,-- 3 3A!)A !221222223 3 3rpkg_filestr | IO[bytes]rctj|}|}t||t|S)zGReturn an |OpcPackage| instance loaded with the contents of `pkg_file`.)r from_file Unmarshaller unmarshalr )clsrV pkg_readerpackages ropenzOpcPackage.open{s<#,X66 #%%z7K@@@rrc6|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)rr:s rrDzOpcPackage.part_related_bys y**7333r list[Part]cDt|S)zJReturn a list containing a reference to each of the parts in this package.)listr9rs rpartszOpcPackage.partssDOO%%&&&rr1cF|j||}|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>)rr1r:r0s r relate_tozOpcPackage.relate_tos" i""7D11wrc4ttjS)ztReturn a reference to the |Relationships| instance holding the collection of relationships for this package.)rr baseURIrs rr+zOpcPackage.relss[0111rc|jD]}|tj||j|jdS)znSave this package to `pkg_file`. `pkg_file` can be either a file-path or a file-like object. N)rebefore_marshalrwriter+)rrVr1s rsavezOpcPackage.savesI J " "D    ! ! ! !Hdi<<<<r;r-r?)rHr;rr )rVrWrr)r:r;rr)rrb)r1rr:r;)rVrW)rr )__name__ __module__ __qualname____doc__rpropertyr!r4r9rBrGrU classmethodr_rDrerhrr+rnr rrrrrsy    :::X: .(MMMMM88X8 3 3 3 3[4444'''X'22\2 ==== ( ( (X ( ( (rrcTeZdZdZedZedZedZdS)rZzHHosts static methods for unmarshalling a package from a |PackageReader|.ct|||}t||||D]}||dS)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_factoryrer1s rr[zUnmarshaller.unmarshalsw--j'<PP--j'5IIILLNN # #D  " " " "!!!!!rcfi}|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~rerK content_typer:blobs rr|zUnmarshaller._unmarshal_partssR5?5K5K5M5M [ [ 1HlGT*l8\7DRYZZE(OO rc|D]W\}}|dkr|n||}|jr|jn ||j}||j||j|jXdS)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_partnamerBr:r>)r]r^re source_urisrelr$r<s rr}z%Unmarshaller._unmarshal_relationshipss !+ 5 5 7 7 N N J *c 1 1WWuZ7HF(,(8YT__eDDX>YF OODL&$(Drs>>""""""44444444444466666611111111%%%%%%777777,,,,,,,,,,,,&&&&&&$$$$$$+&&&&&&111111""""""******[([([([([([([([(|%N%N%N%N%N%N%N%N%N%Nr