o
    P+ it%                     @   s   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 dZG d	d
 d
ZG dd dZG dd dZG d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                       sb   e Zd ZdZ fddZedd Zdd Zdd	 Zed
d Z	edd Z
edddZ  ZS )PackageReaderzProvides access to the contents of a zip-format OPC package via its
    :attr:`serialized_parts` and :attr:`pkg_srels` attributes.c                    s   t t|   || _|| _d S N)superr   __init__
_pkg_srels_sparts)selfcontent_types	pkg_srelssparts	__class__ ^/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/docx/opc/pkgreader.pyr      s   
zPackageReader.__init__c                 C   sB   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)Zpkg_filephys_readerr   r   r   r   r   r   	from_file   s   zPackageReader.from_filec                 c   s(    | j D ]}|j|j|j|jfV  q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   sr   r   r   iter_sparts   s   
zPackageReader.iter_spartsc                 c   s@    | j D ]}t|fV  q| jD ]}|jD ]}|j|fV  qq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spartr   r   r   
iter_srels#   s   


zPackageReader.iter_srelsc                 C   sL   g }t | |}|D ]\}}}}|| }	t||	|||}
||
 q
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_SerializedPartappendtuple)r   r   r   r   Zpart_walkerr   r!   r    r$   r   r&   r   r   r   r   ,   s   z$PackageReader._load_serialized_partsc                 C   s   |  |}t|j|S )zvReturn |_SerializedRelationships| instance populated with relationships for
        source identified by `source_uri`.)Zrels_xml_for_SerializedRelationshipsload_from_xmlbaseURI)r   Z
source_uriZrels_xmlr   r   r   r   9   s   
zPackageReader._srels_forNc           	      c   s    |du rg }|D ]?}|j rq	|j}||v rq	|| |j}t| |}| |}||||fV  t| ||}|D ]\}}}}||||fV  q:q	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_partnamer*   r    r   r   Zblob_forr(   )	r   r$   Zvisited_partnamesr%   r   r    Z
part_srelsr!   Znext_walkerr   r   r   r(   @   s&   

zPackageReader._walk_phys_partsr	   )__name__
__module____qualname____doc__r   staticmethodr   r#   r'   r   r   r(   __classcell__r   r   r   r   r   
   s    
		

r   c                       sD   e Zd ZdZ fddZdd Zedd Zdd	 Zd
d Z	  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                    s"   t t|   t | _t | _d S r	   )r
   r   r   r   
_overrides	_defaultsr   r   r   r   r   Z   s   z_ContentTypeMap.__init__c                 C   sZ   t |tsd}t|t| || jv r| j| S |j| jv r%| j|j S d}t|| )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typer7   extr8   )r   r   tmplr   r   r   __getitem___   s   


z_ContentTypeMap.__getitem__c                 C   sJ   t | }t }|jD ]
}||j|j q
|jD ]
}||j|j q|S )zcReturn a new |_ContentTypeMap| instance populated with the contents of
        `content_types_xml`.)	r   r   Z	overrides_add_overrider   r   defaults_add_default	extension)r   Z	types_elmZct_mapodr   r   r   r   k   s   

z_ContentTypeMap.from_xmlc                 C      || j |< dS )z^Add the default mapping of `extension` to `content_type` to this content type
        mapping.N)r8   )r   rC   r   r   r   r   rB   w      z_ContentTypeMap._add_defaultc                 C   rF   )z]Add the default mapping of `partname` to `content_type` to this content type
        mapping.N)r7   )r   r   r   r   r   r   r@   |   rG   z_ContentTypeMap._add_override)r1   r2   r3   r4   r   r?   r5   r   rB   r@   r6   r   r   r   r   r   V   s    
r   c                       s\   e Zd ZdZ fddZedd Zedd Zedd	 Zed
d Z	edd Z
  ZS )r)   zValue object for an OPC package part.

    Provides access to the partname, content type, blob, and serialized relationships
    for the part.
    c                    s0   t t|   || _|| _|| _|| _|| _d S r	   )r
   r)   r   	_partname_content_type_reltype_blob_srels)r   r   r   r    r!   r$   r   r   r   r      s   
z_SerializedPart.__init__c                 C      | j S r	   )rH   r9   r   r   r   r         z_SerializedPart.partnamec                 C   rM   r	   )rI   r9   r   r   r   r      rN   z_SerializedPart.content_typec                 C   rM   r	   )rK   r9   r   r   r   r!      rN   z_SerializedPart.blobc                 C   rM   )z-The referring relationship type of this part.rJ   r9   r   r   r   r          z_SerializedPart.reltypec                 C   rM   r	   )rL   r9   r   r   r   r$      rN   z_SerializedPart.srels)r1   r2   r3   r4   r   propertyr   r   r!   r    r$   r6   r   r   r   r   r)      s    



r)   c                       sh   e Zd ZdZ fddZedd Zedd Zedd	 Zed
d Z	edd Z
edd Z  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                    s8   t t|   || _|j| _|j| _|j| _	|j
| _d S r	   )r
   rR   r   _baseURIrId_rIdr    rJ   target_mode_target_mode
target_ref_target_ref)r   r.   rel_elmr   r   r   r      s   z _SerializedRelationship.__init__c                 C   s   | j tjkS )z'True if target_mode is ``RTM.EXTERNAL``)rW   RTMZEXTERNALr9   r   r   r   r/      s   z#_SerializedRelationship.is_externalc                 C   rM   )z.Relationship type, like ``RT.OFFICE_DOCUMENT``rO   r9   r   r   r   r       rP   z_SerializedRelationship.reltypec                 C   rM   )zmRelationship id, like 'rId9', corresponds to the ``Id`` attribute on the
        ``CT_Relationship`` element.)rU   r9   r   r   r   rT         z_SerializedRelationship.rIdc                 C   rM   )zwString in ``TargetMode`` attribute of ``CT_Relationship`` element, one of
        ``RTM.INTERNAL`` or ``RTM.EXTERNAL``.)rW   r9   r   r   r   rV      r\   z#_SerializedRelationship.target_modec                 C   rM   )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.)rY   r9   r   r   r   rX      s   z"_SerializedRelationship.target_refc                 C   s4   | j r	d}t|t| dst| 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   Zfrom_rel_refrS   rX   r]   )r   msgr   r   r   r0      s   
z'_SerializedRelationship.target_partname)r1   r2   r3   r4   r   rQ   r/   r    rT   rV   rX   r0   r6   r   r   r   r   rR      s    




rR   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )r,   zRead-only sequence of |_SerializedRelationship| instances corresponding to the
    relationships item XML passed to constructor.c                    s   t t|   g | _d S r	   )r
   r,   r   rL   r9   r   r   r   r      s   
z!_SerializedRelationships.__init__c                 C   s
   | j  S )z*Support iteration, e.g. 'for x in srels:'.)rL   __iter__r9   r   r   r   ra      s   
z!_SerializedRelationships.__iter__c                 C   s8   t  }|durt|}|jD ]}|jt| | q|S )zReturn |_SerializedRelationships| instance loaded with the relationships
        contained in `rels_item_xml`.

        Returns an empty collection if `rels_item_xml` is |None|.
        N)r,   r   ZRelationship_lstrL   r*   rR   )r.   Zrels_item_xmlr$   Zrels_elmrZ   r   r   r   r-      s   
z&_SerializedRelationships.load_from_xml)	r1   r2   r3   r4   r   ra   r5   r-   r6   r   r   r   r   r,      s    r,   N)r4   Zdocx.opc.constantsr   r[   Zdocx.opc.oxmlr   Zdocx.opc.packurir   r   Zdocx.opc.phys_pkgr   Zdocx.opc.sharedr   r   r   r)   rR   r,   r   r   r   r   <module>   s    L,%?