
    h%                         S r SSKJr  SSKJr  SSKJrJr  SSK	J
r
  SSKJr   " S S5      r " S	 S
5      r " S S5      r " S S5      r " S S5      rg)zQLow-level, read-only API to a serialized Open Packaging Convention (OPC) package.    )RELATIONSHIP_TARGET_MODE)	parse_xml)PACKAGE_URIPackURI)PhysPkgReader)CaseInsensitiveDictc                   |   ^  \ rS rSrSrU 4S jr\S 5       rS rS r	\S 5       r
\S 5       r\SS	 j5       rS
rU =r$ )PackageReader
   z~Provides access to the contents of a zip-format OPC package via its
:attr:`serialized_parts` and :attr:`pkg_srels` attributes.c                 B   > [         [        U ]  5         X l        X0l        g N)superr
   __init__
_pkg_srels_sparts)selfcontent_types	pkg_srelssparts	__class__s       D/var/www/html/env/lib/python3.13/site-packages/docx/opc/pkgreader.pyr   PackageReader.__init__   s    mT+-#    c                     [        U 5      n[        R                  UR                  5      n[        R                  U[        5      n[        R                  XU5      nUR                  5         [	        X#U5      $ )zEReturn a |PackageReader| instance loaded with contents of `pkg_file`.)	r   _ContentTypeMapfrom_xmlcontent_types_xmlr
   
_srels_forr   _load_serialized_partsclose)pkg_filephys_readerr   r   r   s        r   	from_filePackageReader.from_file   sf     $H-'001N1NO!,,[+F	55M
 	]v>>r   c              #      #    U R                    H3  nUR                  UR                  UR                  UR                  4v   M5     g7f)zmGenerate a 4-tuple `(partname, content_type, reltype, blob)` for each of the
serialized parts in the package.N)r   partnamecontent_typereltypeblob)r   ss     r   iter_spartsPackageReader.iter_sparts   s4      A::q~~qyy!&&AA s   AAc              #      #    U R                    H  n[        U4v   M     U R                   H&  nUR                   H  nUR                  U4v   M     M(     g7f)zUGenerate a 2-tuple `(source_uri, srel)` for each of the relationships in the
package.N)r   r   r   srelsr&   )r   srelsparts      r   
iter_srelsPackageReader.iter_srels%   sK      OOD%% $\\E~~t,, $ "s   AAc                     / n[         R                  X5      nU H(  u  pVpxX%   n	[        XYXvU5      n
UR                  U
5        M*     [	        U5      $ )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_SerializedPartappendtuple)r"   r   r   r   part_walkerr&   r)   r(   r.   r'   r0   s              r   r   $PackageReader._load_serialized_parts.   sV    
 #44[L.9*HG(2L#HG5QEMM%  /: V}r   c                 d    U R                  U5      n[        R                  UR                  U5      $ )znReturn |_SerializedRelationships| instance populated with relationships for
source identified by `source_uri`.)rels_xml_for_SerializedRelationshipsload_from_xmlbaseURI)r"   
source_urirels_xmls      r   r   PackageReader._srels_for;   s-     ++J7'55j6H6H(SSr   c              #   V  #    Uc  / nU H  nUR                   (       a  M  UR                  nXB;   a  M)  UR                  U5        UR                  n[        R                  X5      nU R                  U5      nXGXV4v   [        R                  XU5      nU H  u  pGpQXGXQ4v   M     M     g7f)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_externaltarget_partnamer6   r(   r
   r   blob_forr4   )	r"   r.   visited_partnamesr/   r&   r(   
part_srelsr)   next_walkers	            r   r4   PackageReader._walk_phys_partsB   s      $ "D++H,$$X.llG&11+HJ''1D777'88):K 3>.w66 3> s   B'B))r   r   r   )__name__
__module____qualname____firstlineno____doc__r   staticmethodr#   r+   r1   r   r   r4   __static_attributes____classcell__r   s   @r   r
   r
   
   sk    B
 	? 	?B- 
 
 T T 7 7r   r
   c                   N   ^  \ rS rSrSrU 4S jrS r\S 5       rS r	S r
SrU =r$ )	r   Z   zValue type providing dictionary semantics for looking up content type by part
name, e.g. ``content_type = cti['/ppt/presentation.xml']``.c                 f   > [         [        U ]  5         [        5       U l        [        5       U l        g r   )r   r   r   r   
_overrides	_defaultsr   r   s    r   r   _ContentTypeMap.__init__^   s$    ot-/-/,.r   c                    [        U[        5      (       d  Sn[        U[        U5      -  5      eXR                  ;   a  U R                  U   $ UR
                  U R                  ;   a  U R                  UR
                     $ Sn[        X!-  5      e)z6Return content type for part identified by `partname`.z4_ContentTypeMap key must be <type 'PackURI'>, got %sz8no content type for partname '%s' in [Content_Types].xml)
isinstancer   KeyErrortyperV   extrW   )r   r&   tmpls      r   __getitem___ContentTypeMap.__getitem__c   sv    (G,,ID4$x.011&??8,,<<4>>)>>(,,//It''r   c                    [        U 5      n[        5       nUR                   H)  nUR                  UR                  UR
                  5        M+     UR                   H)  nUR                  UR                  UR
                  5        M+     U$ )z[Return a new |_ContentTypeMap| instance populated with the contents of
`content_types_xml`.)	r   r   	overrides_add_overrider&   r'   defaults_add_default	extension)r   	types_elmct_mapods        r   r   _ContentTypeMap.from_xmlo   sl     /0	 "$$A  Q^^< %##AQ^^< $r   c                      X R                   U'   g)zVAdd the default mapping of `extension` to `content_type` to this content type
mapping.N)rW   )r   rg   r'   s      r   rf   _ContentTypeMap._add_default{   s     %1y!r   c                      X R                   U'   g)zUAdd the default mapping of `partname` to `content_type` to this content type
mapping.N)rV   )r   r&   r'   s      r   rd   _ContentTypeMap._add_override   s     %1!r   )rW   rV   )rJ   rK   rL   rM   rN   r   r`   rO   r   rf   rd   rP   rQ   rR   s   @r   r   r   Z   s6    C/

( 	 	1
1 1r   r   c                   |   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       rS	rU =r$ )
r5      zValue object for an OPC package part.

Provides access to the partname, content type, blob, and serialized relationships
for the part.
c                 f   > [         [        U ]  5         Xl        X l        X0l        X@l        XPl        g r   )r   r5   r   	_partname_content_type_reltype_blob_srels)r   r&   r'   r(   r)   r.   r   s         r   r   _SerializedPart.__init__   s*    ot-/!)
r   c                     U R                   $ r   )rt   r   s    r   r&   _SerializedPart.partname   s    ~~r   c                     U R                   $ r   )ru   r{   s    r   r'   _SerializedPart.content_type   s    !!!r   c                     U R                   $ r   )rw   r{   s    r   r)   _SerializedPart.blob   s    zzr   c                     U R                   $ )z-The referring relationship type of this part.rv   r{   s    r   r(   _SerializedPart.reltype        }}r   c                     U R                   $ r   rx   r{   s    r   r.   _SerializedPart.srels   s    {{r   )rw   ru   rt   rv   rx   )rJ   rK   rL   rM   rN   r   propertyr&   r'   r)   r(   r.   rP   rQ   rR   s   @r   r5   r5      sq       " "      r   r5   c                      ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       rS
rU =r$ )_SerializedRelationship   zValue 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.
c                    > [         [        U ]  5         Xl        UR                  U l        UR                  U l        UR                  U l	        UR                  U l        g r   )r   r   r   _baseURIrId_rIdr(   rv   target_mode_target_mode
target_ref_target_ref)r   r>   rel_elmr   s      r   r    _SerializedRelationship.__init__   sH    %t57KK	#//"--r   c                 <    U R                   [        R                  :H  $ )z'True if target_mode is ``RTM.EXTERNAL``)r   RTMEXTERNALr{   s    r   rC   #_SerializedRelationship.is_external   s       CLL00r   c                     U R                   $ )z.Relationship type, like ``RT.OFFICE_DOCUMENT``r   r{   s    r   r(   _SerializedRelationship.reltype   r   r   c                     U R                   $ )zeRelationship id, like 'rId9', corresponds to the ``Id`` attribute on the
``CT_Relationship`` element.)r   r{   s    r   r   _SerializedRelationship.rId   s     yyr   c                     U R                   $ )zoString in ``TargetMode`` attribute of ``CT_Relationship`` element, one of
``RTM.INTERNAL`` or ``RTM.EXTERNAL``.)r   r{   s    r   r   #_SerializedRelationship.target_mode   s        r   c                     U R                   $ )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.)r   r{   s    r   r   "_SerializedRelationship.target_ref   s    
 r   c                     U R                   (       a  Sn[        U5      e[        U S5      (       d0  [        R                  " U R
                  U R                  5      U l        U R                  $ )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)rC   
ValueErrorhasattrr   from_rel_refr   r   r   )r   msgs     r   rD   '_SerializedRelationship.target_partname   sZ     0  S/!t/00$+$8$8$XD!$$$r   )r   r   rv   r   r   r   )rJ   rK   rL   rM   rN   r   r   rC   r(   r   r   r   rD   rP   rQ   rR   s   @r   r   r      s    . 1 1    
 ! !
     % %r   r   c                   B   ^  \ rS rSrSrU 4S jrS r\S 5       rSr	U =r
$ )r<      z|Read-only sequence of |_SerializedRelationship| instances corresponding to the
relationships item XML passed to constructor.c                 8   > [         [        U ]  5         / U l        g r   )r   r<   r   rx   rX   s    r   r   !_SerializedRelationships.__init__   s    &68r   c                 6    U R                   R                  5       $ )z*Support iteration, e.g. 'for x in srels:'.)rx   __iter__r{   s    r   r   !_SerializedRelationships.__iter__   s    {{##%%r   c                     [        5       nUbB  [        U5      nUR                   H'  nUR                  R	                  [        X5      5        M)     U$ )zReturn |_SerializedRelationships| instance loaded with the relationships
contained in `rels_item_xml`.

Returns an empty collection if `rels_item_xml` is |None|.
)r<   r   Relationship_lstrx   r6   r   )r>   rels_item_xmlr.   rels_elmr   s        r   r=   &_SerializedRelationships.load_from_xml   sI     )*$ /H#44##$;G$MN 5r   r   )rJ   rK   rL   rM   rN   r   r   rO   r=   rP   rQ   rR   s   @r   r<   r<      s&    5&  r   r<   N)rN   docx.opc.constantsr   r   docx.opc.oxmlr   docx.opc.packurir   r   docx.opc.phys_pkgr   docx.opc.sharedr   r
   r   r5   r   r<    r   r   <module>r      sO    W > # 1 + /M7 M7`)1 )1X" "J<% <%~ r   