;it%dZddlmZddlmZddlmZmZddl m Z ddl m Z GddZ Gd d ZGd d ZGd dZGddZdS)zQLow-level, read-only API to a serialized Open Packaging Convention (OPC) package.)RELATIONSHIP_TARGET_MODE) parse_xml) PACKAGE_URIPackURI) PhysPkgReader)CaseInsensitiveDictceZdZdZfdZedZdZdZedZ edZ ed d Z xZ S) PackageReaderzProvides access to the contents of a zip-format OPC package via its :attr:`serialized_parts` and :attr:`pkg_srels` attributes.crtt|||_||_dSN)superr __init__ _pkg_srels_sparts)self content_types pkg_srelssparts __class__s FC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\docx/opc/pkgreader.pyrzPackageReader.__init__s1 mT""++---# c t|}t|j}t|t }t|||}|t |||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_filesw$H-- '001NOO !,,[+FF 55k9m\\]Iv>>>rc#VK|jD]}|j|j|j|jfVdS)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_spartssG B BA:q~qy!&A A A A A B Brc#rK|jD] }t|fV|jD]}|jD] }|j|fVdS)z]Generate a 2-tuple `(source_uri, srel)` for each of the relationships in the package.N)rrrsrelsr#)rsrelsparts r iter_srelszPackageReader.iter_srels#stO & &D% % % % %\ - -E  - -~t,,,,, - - -rcg}t||}|D]7\}}}}||} t|| |||} || 8t |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,sv #44[)LL .9 ! ! *HdGU(2L#HlGT5QQE MM% V}}rcl||}t|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_for9s0++J77'55j6H(SSSrNc#HK|g}|D]}|jr |j}||vr|||j}t||}||}||||fVt|||}|D]\}}}}||||fVdS)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  $ "  7 7D +H,,,  $ $X . . .lG&11+xHHJ''11DT7J7 7 7 7'88jRcddK2= 7 7.$w66666 7 7 7rr ) __name__ __module__ __qualname____doc__r staticmethodr!r(r-rrr/ __classcell__rs@rr r sBB ??\?BBB ---  \ TT\T 777\77777rr cJeZdZdZfdZdZedZdZdZ xZ S)rzValue type providing dictionary semantics for looking up content type by part name, e.g. ``content_type = cti['/ppt/presentation.xml']``.ctt|t|_t|_dSr )r rrr _overrides _defaultsrrs rrz_ContentTypeMap.__init__Zs= ot$$--///-//,..rct|ts!d}t|t|z||jvr |j|S|j|jvr|j|jSd}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___s(G,, 2ID4$x..011 1 t & &?8, , <4> ) )>(,/ /Ith'''rct|}t}|jD]"}||j|j#|jD]"}||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_xmlks/00  ""$ = =A  Q^ < < < <# = =A    Q^ < < < < rc||j|<dS)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|<dS)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@rrrVsCC///// ( ( (  \ 111 1111111rrceZdZdZfdZedZedZedZedZ edZ xZ S)r0zValue object for an OPC package part. Provides access to the partname, content type, blob, and serialized relationships for the part. ctt|||_||_||_||_||_dSr )r r0r _partname _content_type_reltype_blob_srels)rr#r$r%r&r*rs rrz_SerializedPart.__init__sG ot$$--///!)   rc|jSr )rcrs rr#z_SerializedPart.partnames ~rc|jSr )rdris rr$z_SerializedPart.content_types !!rc|jSr )rfris rr&z_SerializedPart.blobs zrc|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@rr0r0s X""X"XXXrr0ceZdZdZfdZedZedZedZedZ 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 |_ dSr ) r rrr_baseURIrId_rIdr%re target_mode _target_mode target_ref _target_ref)rr8rel_elmrs rrz _SerializedRelationship.__init__sU %t,,55777 K  #/"-rc,|jtjkS)z'True if target_mode is ``RTM.EXTERNAL``)rxRTMEXTERNALris rr<z#_SerializedRelationship.is_externals CL00rc|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.rIds yrc|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|jrd}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_partnames]   "0 S// !t/00 Y$+$8$X$XD !$$r) rBrCrDrErrpr<r%rurwryr=rGrHs@rrrrrs .....11X1XX !!X!   X %%X%%%%%rrrc>eZdZdZfdZdZedZxZS)r6zRead-only sequence of |_SerializedRelationship| instances corresponding to the relationships item XML passed to constructor.cdtt|g|_dSr )r r6rrgrMs rrz!_SerializedRelationships.__init__s+ &--66888 rc4|jS)z*Support iteration, e.g. 'for x in srels:'.)rg__iter__ris rrz!_SerializedRelationships.__iter__s{##%%%rct}|At|}|jD]*}|jt ||+|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_xmls_)**  $ //H#4 O O ##$;GW$M$MNNNN r) rBrCrDrErrrFr7rGrHs@rr6r6sl55&&&  \     rr6N)rEdocx.opc.constantsrr} docx.opc.oxmlrdocx.opc.packurirrdocx.opc.phys_pkgrdocx.opc.sharedrr rr0rrr6rrrsIWW>>>>>>######11111111++++++//////I7I7I7I7I7I7I7I7X)1)1)1)1)1)1)1)1X""""""""J<%<%<%<%<%<%<%<%~r