
    hit%                         d Z ddlmZ ddlmZ ddlmZmZ ddl	m
Z
 ddlmZ  G d d          Z G d	 d
          Z G d d          Z G d d          Z G d d          ZdS )zQLow-level, read-only API to a serialized Open Packaging Convention (OPC) package.    )RELATIONSHIP_TARGET_MODE)	parse_xml)PACKAGE_URIPackURI)PhysPkgReader)CaseInsensitiveDictc                        e Zd ZdZ fdZed             Zd Zd Zed             Z	ed             Z
ed
d	            Z xZS )PackageReaderzProvides access to the contents of a zip-format OPC package via its
    :attr:`serialized_parts` and :attr:`pkg_srels` attributes.c                 r    t          t          |                                            || _        || _        d S N)superr
   __init__
_pkg_srels_sparts)selfcontent_types	pkg_srelssparts	__class__s       j/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/docx/opc/pkgreader.pyr   zPackageReader.__init__   s1    mT""++---#    c                     t          |           }t                              |j                  }t                              |t                    }t                              |||          }|                                 t	          |||          S )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_filezPackageReader.from_file   sw     $H--'001NOO!,,[+FF	55k9m\\]Iv>>>r   c              #   V   K   | j         D ]}|j        |j        |j        |j        fV  dS )zuGenerate 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_spartszPackageReader.iter_sparts   sG        	B 	BA:q~qy!&AAAAA	B 	Br   c              #   r   K   | j         D ]}t          |fV  | j        D ]}|j        D ]}|j        |fV  dS )z]Generate a 2-tuple `(source_uri, srel)` for each of the relationships in the
        package.N)r   r   r   srelsr#   )r   srelsparts      r   
iter_srelszPackageReader.iter_srels#   st       O 	& 	&D%%%%%\ 	- 	-E - -~t,,,,,-	- 	-r   c                     g }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_SerializedPartappendtuple)r    r   r   r   part_walkerr#   r&   r%   r*   r$   r,   s              r   r   z$PackageReader._load_serialized_parts,   sv    
 #44[)LL.9 	! 	!*HdGU(2L#HlGT5QQEMM%    V}}r   c                 l    |                      |          }t                              |j        |          S )zvReturn |_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   zPackageReader._srels_for9   s0     ++J77'55j6H(SSSr   Nc              #   H  K   |g }|D ]}|j         r
|j        }||v r|                    |           |j        }t                              | |          }|                     |          }||||fV  t                              | ||          }|D ]\  }}}}||||fV  dS )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_partnamer1   r%   r
   r   blob_forr/   )	r    r*   visited_partnamesr+   r#   r%   
part_srelsr&   next_walkers	            r   r/   zPackageReader._walk_phys_parts@   s       $ " 	7 	7D +H,,,$$X...lG&11+xHHJ''11DT7J7777'88jRcddK2= 7 7.$w666667	7 	7r   r   )__name__
__module____qualname____doc__r   staticmethodr!   r(   r-   r   r   r/   __classcell__r   s   @r   r
   r
   
   s        B B    
 ? ? \?B B B- - - 
 
 \
 T T \T 7 7 7 \7 7 7 7 7r   r
   c                   J     e Zd ZdZ fdZd Zed             Zd Zd Z	 xZ
S )r   zValue type providing dictionary semantics for looking up content type by part
    name, e.g. ``content_type = cti['/ppt/presentation.xml']``.c                     t          t          |                                            t                      | _        t                      | _        d S r   )r   r   r   r   
_overrides	_defaultsr   r   s    r   r   z_ContentTypeMap.__init__Z   s=    ot$$--///-//,..r   c                    t          |t                    s!d}t          |t          |          z            || j        v r| j        |         S |j        | j        v r| j        |j                 S d}t          ||z            )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typerK   extrL   )r   r#   tmpls      r   __getitem__z_ContentTypeMap.__getitem___   s    (G,, 	2ID4$x..0111t&&?8,,<4>))>(,//Ith'''r   c                     t          |           }t                      }|j        D ]"}|                    |j        |j                   #|j        D ]"}|                    |j        |j                   #|S )zcReturn 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   z_ContentTypeMap.from_xmlk   s     /00	 ""$ 	= 	=A  Q^<<<<# 	= 	=AQ^<<<<r   c                     || j         |<   dS )z^Add the default mapping of `extension` to `content_type` to this content type
        mapping.N)rL   )r   rZ   r$   s      r   rY   z_ContentTypeMap._add_defaultw   s     %1y!!!r   c                     || j         |<   dS )z]Add the default mapping of `partname` to `content_type` to this content type
        mapping.N)rK   )r   r#   r$   s      r   rW   z_ContentTypeMap._add_override|   s     %1!!!r   )rB   rC   rD   rE   r   rT   rF   r   rY   rW   rG   rH   s   @r   r   r   V   s        C C/ / / / /

( 
( 
( 	 	 \	1 1 1
1 1 1 1 1 1 1r   r   c                        e Zd ZdZ fdZed             Zed             Zed             Zed             Z	ed             Z
 xZS )r0   zValue object for an OPC package part.

    Provides access to the partname, content type, blob, and serialized relationships
    for the part.
    c                     t          t          |                                            || _        || _        || _        || _        || _        d S r   )r   r0   r   	_partname_content_type_reltype_blob_srels)r   r#   r$   r%   r&   r*   r   s         r   r   z_SerializedPart.__init__   sG    ot$$--///!)
r   c                     | j         S r   )rc   r   s    r   r#   z_SerializedPart.partname   s
    ~r   c                     | j         S r   )rd   ri   s    r   r$   z_SerializedPart.content_type   s    !!r   c                     | j         S r   )rf   ri   s    r   r&   z_SerializedPart.blob   s
    zr   c                     | j         S )z-The referring relationship type of this part.re   ri   s    r   r%   z_SerializedPart.reltype        }r   c                     | j         S r   )rg   ri   s    r   r*   z_SerializedPart.srels   s
    {r   )rB   rC   rD   rE   r   propertyr#   r$   r&   r%   r*   rG   rH   s   @r   r0   r0      s                X " " X"   X   X   X    r   r0   c                        e Zd ZdZ fdZed             Zed             Zed             Zed             Z	ed             Z
ed             Z xZ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.
    c                     t          t          |                                            || _        |j        | _        |j        | _        |j        | _	        |j
        | _        d S r   )r   rr   r   _baseURIrId_rIdr%   re   target_mode_target_mode
target_ref_target_ref)r   r8   rel_elmr   s      r   r   z _SerializedRelationship.__init__   sU    %t,,55777K	#/"-r   c                 ,    | j         t          j        k    S )z'True if target_mode is ``RTM.EXTERNAL``)rx   RTMEXTERNALri   s    r   r<   z#_SerializedRelationship.is_external   s      CL00r   c                     | j         S )z.Relationship type, like ``RT.OFFICE_DOCUMENT``rm   ri   s    r   r%   z_SerializedRelationship.reltype   rn   r   c                     | j         S )zmRelationship id, like 'rId9', corresponds to the ``Id`` attribute on the
        ``CT_Relationship`` element.)rv   ri   s    r   ru   z_SerializedRelationship.rId   s     yr   c                     | j         S )zwString in ``TargetMode`` attribute of ``CT_Relationship`` element, one of
        ``RTM.INTERNAL`` or ``RTM.EXTERNAL``.)rx   ri   s    r   rw   z#_SerializedRelationship.target_mode   s       r   c                     | j         S )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.)rz   ri   s    r   ry   z"_SerializedRelationship.target_ref   s    
 r   c                     | j         rd}t          |          t          | d          s$t          j        | j        | j                  | _        | j        S )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<   
ValueErrorhasattrr   from_rel_refrt   ry   r   )r   msgs     r   r=   z'_SerializedRelationship.target_partname   s]      	"0  S//!t/00 	Y$+$8$X$XD!$$r   )rB   rC   rD   rE   r   rp   r<   r%   ru   rw   ry   r=   rG   rH   s   @r   rr   rr      s         . . . . . 1 1 X1   X   X
 ! ! X!
     X  % % X% % % % %r   rr   c                   >     e Zd ZdZ fdZd Zed             Z xZS )r6   zRead-only sequence of |_SerializedRelationship| instances corresponding to the
    relationships item XML passed to constructor.c                 d    t          t          |                                            g | _        d S r   )r   r6   r   rg   rM   s    r   r   z!_SerializedRelationships.__init__   s+    &--66888r   c                 4    | j                                         S )z*Support iteration, e.g. 'for x in srels:'.)rg   __iter__ri   s    r   r   z!_SerializedRelationships.__iter__   s    {##%%%r   c                     t                      }|At          |          }|j        D ]*}|j                            t          | |                     +|S )zReturn |_SerializedRelationships| instance loaded with the relationships
        contained in `rels_item_xml`.

        Returns an empty collection if `rels_item_xml` is |None|.
        )r6   r   Relationship_lstrg   r1   rr   )r8   rels_item_xmlr*   rels_elmr{   s        r   r7   z&_SerializedRelationships.load_from_xml   s_     )**$ //H#4 O O##$;GW$M$MNNNNr   )	rB   rC   rD   rE   r   r   rF   r7   rG   rH   s   @r   r6   r6      sl        5 5    & & &   \    r   r6   N)rE   docx.opc.constantsr   r}   docx.opc.oxmlr   docx.opc.packurir   r   docx.opc.phys_pkgr   docx.opc.sharedr   r
   r   r0   rr   r6    r   r   <module>r      sI   W W > > > > > > # # # # # # 1 1 1 1 1 1 1 1 + + + + + + / / / / / /I7 I7 I7 I7 I7 I7 I7 I7X)1 )1 )1 )1 )1 )1 )1 )1X" " " " " " " "J<% <% <% <% <% <% <% <%~         r   