Xjt%dZddlmZddlmZddlmZmZddl m Z ddl m Z GddZ Gd d ZGd d ZGd dZGddZy)zQLow-level, read-only API to a serialized Open Packaging Convention (OPC) package.)RELATIONSHIP_TARGET_MODE) parse_xml) PACKAGE_URIPackURI) PhysPkgReader)CaseInsensitiveDictcpeZdZdZfdZedZdZdZedZ edZ ed dZ xZ S) PackageReaderzProvides access to the contents of a zip-format OPC package via its :attr:`serialized_parts` and :attr:`pkg_srels` attributes.cFtt| ||_||_yN)superr __init__ _pkg_srels_sparts)self content_types pkg_srelssparts __class__s f/mnt/ssd/data/Dropbox/adrian/vault-secondbrain/venv/lib/python3.12/site-packages/docx/opc/pkgreader.pyrzPackageReader.__init__s mT+-# ct|}tj|j}tj |t }tj|||}|jt |||S)zEReturn a |PackageReader| instance loaded with contents of `pkg_file`.) r_ContentTypeMapfrom_xmlcontent_types_xmlr _srels_forr_load_serialized_partsclose)pkg_file phys_readerrrrs r from_filezPackageReader.from_filese$H- '001N1NO !,,[+F 55k9m\]Iv>>rc#K|jD]2}|j|j|j|jf4yw)zuGenerate a 4-tuple `(partname, content_type, reltype, blob)` for each of the serialized parts in the package.N)rpartname content_typereltypeblob)rss r iter_spartszPackageReader.iter_spartss< BA::q~~qyy!&&A A BsAAc#K|jD] }t|f|jD]#}|jD]}|j|f%yw)z]Generate a 2-tuple `(source_uri, srel)` for each of the relationships in the package.N)rrrsrelsr#)rsrelsparts r iter_srelszPackageReader.iter_srels#s[OO &D% % &\\ -E  -~~t,, - -sAAcg}tj||}|D],\}}}}||} t|| |||} |j| .t |S)zReturn a list of |_SerializedPart| instances corresponding to the parts in `phys_reader` accessible by walking the relationship graph starting with `pkg_srels`.)r _walk_phys_parts_SerializedPartappendtuple) r rrr part_walkerr#r&r%r*r$r,s rrz$PackageReader._load_serialized_parts,sg #44[)L .9 ! *HdGU(2L#HlGT5QE MM%  !V}rcd|j|}tj|j|S)zvReturn |_SerializedRelationships| instance populated with relationships for source identified by `source_uri`.) rels_xml_for_SerializedRelationships load_from_xmlbaseURI)r source_urirels_xmls rrzPackageReader._srels_for9s-++J7'55j6H6H(SSrc#LK|g}|D]}|jr|j}||vr!|j||j}tj ||}|j |}||||ftj|||}|D]\}}}}||||fyw)zGenerate a 4-tuple `(partname, blob, reltype, srels)` for each of the parts in `phys_reader` by walking the relationship graph rooted at srels.N) is_externaltarget_partnamer1r%r rblob_forr/) r r*visited_partnamesr+r#r% part_srelsr& next_walkers rr/zPackageReader._walk_phys_parts@s  $ "  7D++H,,  $ $X .llG&11+xHJ''1DT7J7 7'88jRcdK2= 7.$w66 7 7sB"B$r ) __name__ __module__ __qualname____doc__r staticmethodr!r(r-rrr/ __classcell__rs@rr r skB ??B -  TT 77rr cDeZdZdZfdZdZedZdZdZ xZ S)rzValue type providing dictionary semantics for looking up content type by part name, e.g. ``content_type = cti['/ppt/presentation.xml']``.cftt| t|_t|_yr )r rrr _overrides _defaultsrrs rrz_ContentTypeMap.__init__Zs$ ot-/-/,.rct|tsd}t|t|z||jvr|j|S|j |j vr|j |j Sd}t||z)z6Return content type for part identified by `partname`.z4_ContentTypeMap key must be , got %sz8no content type for partname '%s' in [Content_Types].xml) isinstancerKeyErrortyperKextrL)rr#tmpls r __getitem__z_ContentTypeMap.__getitem___sw(G,ID4$x.01 1 t &??8, , <<4>> )>>(,,/ /Ith''rc t|}t}|jD](}|j|j|j *|j D](}|j|j|j *|S)zcReturn a new |_ContentTypeMap| instance populated with the contents of `content_types_xml`.) rr overrides _add_overrider#r$defaults _add_default extension)r types_elmct_mapods rrz_ContentTypeMap.from_xmlksv/0  "$$ =A  Q^^ < =## =A    Q^^ < = rc"||j|<y)z^Add the default mapping of `extension` to `content_type` to this content type mapping.N)rL)rrZr$s rrYz_ContentTypeMap._add_defaultws%1y!rc"||j|<y)z]Add the default mapping of `partname` to `content_type` to this content type mapping.N)rK)rr#r$s rrWz_ContentTypeMap._add_override|s%1!r) rBrCrDrErrTrFrrYrWrGrHs@rrrVs1C/ (  1 1rrcreZdZdZfdZedZedZedZedZ edZ xZ S)r0zValue object for an OPC package part. Provides access to the partname, content type, blob, and serialized relationships for the part. cptt| ||_||_||_||_||_yr )r r0r _partname _content_type_reltype_blob_srels)rr#r$r%r&r*rs rrz_SerializedPart.__init__s4 ot-/!)   rc|jSr )rcrs rr#z_SerializedPart.partnames ~~rc|jSr )rdris rr$z_SerializedPart.content_types!!!rc|jSr )rfris rr&z_SerializedPart.blobs zzrc|jS)z-The referring relationship type of this part.reris rr%z_SerializedPart.reltype}}rc|jSr )rgris rr*z_SerializedPart.srelss {{r) rBrCrDrErpropertyr#r$r&r%r*rGrHs@rr0r0sq ""rr0ceZdZdZfdZedZedZedZedZ edZ edZ xZ S) _SerializedRelationshipzValue object representing a serialized relationship in an OPC package. Serialized, in this case, means any target part is referred to via its partname rather than a direct link to an in-memory |Part| object. ctt| ||_|j|_|j |_|j|_ |j|_ yr ) r rrr_baseURIrId_rIdr%re target_mode _target_mode target_ref _target_ref)rr8rel_elmrs rrz _SerializedRelationship.__init__sJ %t57 KK  #//"--rc<|jtjk(S)z'True if target_mode is ``RTM.EXTERNAL``)rxRTMEXTERNALris rr<z#_SerializedRelationship.is_externals  CLL00rc|jS)z.Relationship type, like ``RT.OFFICE_DOCUMENT``rmris rr%z_SerializedRelationship.reltypernrc|jS)zmRelationship id, like 'rId9', corresponds to the ``Id`` attribute on the ``CT_Relationship`` element.)rvris rruz_SerializedRelationship.rIdsyyrc|jS)zwString in ``TargetMode`` attribute of ``CT_Relationship`` element, one of ``RTM.INTERNAL`` or ``RTM.EXTERNAL``.)rxris rrwz#_SerializedRelationship.target_modes   rc|jS)zString in ``Target`` attribute of ``CT_Relationship`` element, a relative part reference for internal target mode or an arbitrary URI, e.g. an HTTP URL, for external target mode.)rzris rryz"_SerializedRelationship.target_refs rc|jr d}t|t|ds/tj|j |j |_|jS)z|PackURI| instance containing partname targeted by this relationship. Raises ``ValueError`` on reference if target_mode is ``'External'``. Use :attr:`target_mode` to check before referencing. zUtarget_partname attribute on Relationship is undefined where TargetMode == "External"_target_partname)r< ValueErrorhasattrr from_rel_refrtryr)rmsgs rr=z'_SerializedRelationship.target_partnamesW   0 S/ !t/0$+$8$8$XD !$$$r) rBrCrDrErrpr<r%rurwryr=rGrHs@rrrrrs .11 !!   %%rrrc8eZdZdZfdZdZedZxZS)r6zRead-only sequence of |_SerializedRelationship| instances corresponding to the relationships item XML passed to constructor.c8tt| g|_yr )r r6rrgrMs rrz!_SerializedRelationships.__init__s &68 rc6|jjS)z*Support iteration, e.g. 'for x in srels:'.)rg__iter__ris rrz!_SerializedRelationships.__iter__s{{##%%rct}|At|}|jD]'}|jj t ||)|S)zReturn |_SerializedRelationships| instance loaded with the relationships contained in `rels_item_xml`. Returns an empty collection if `rels_item_xml` is |None|. )r6rRelationship_lstrgr1rr)r8 rels_item_xmlr*rels_elmr{s rr7z&_SerializedRelationships.load_from_xmlsR)*  $ /H#44 O ##$;GW$MN O r) rBrCrDrErrrFr7rGrHs@rr6r6s&5&  rr6N)rEdocx.opc.constantsrr} docx.opc.oxmlrdocx.opc.packurirrdocx.opc.phys_pkgrdocx.opc.sharedrr rr0rrr6rrrsOW>#1+/I7I7X)1)1X""J<%<%~r