o
    	'f%                     @   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__ J/home/ubuntu/flask/venv/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)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   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`.)rels_xml_for_SerializedRelationshipsload_from_xmlbaseURI)r   
source_urirels_xmlr   r   r   r   ;   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   blob_forr)   )	r   r%   visited_partnamesr&   r   r!   
part_srelsr"   next_walkerr   r   r   r)   B   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   ^   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typer@   extrA   )r   r   tmplr   r   r   __getitem__c   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   	overrides_add_overrider   r    defaults_add_default	extension)r   	types_elmct_mapodr   r   r   r   o   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)rA   )r   rM   r    r   r   r   rL   {      z_ContentTypeMap._add_defaultc                 C   rR   )z]Add the default mapping of `partname` to `content_type` to this content type
        mapping.N)r@   )r   r   r    r   r   r   rJ      rS   z_ContentTypeMap._add_override)r:   r;   r<   r=   r   rH   r>   r   rL   rJ   r?   r   r   r   r   r   Z   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	   )rT   rB   r   r   r   r         z_SerializedPart.partnamec                 C   rY   r	   )rU   rB   r   r   r   r       rZ   z_SerializedPart.content_typec                 C   rY   r	   )rW   rB   r   r   r   r"      rZ   z_SerializedPart.blobc                 C   rY   )z-The referring relationship type of this part.rV   rB   r   r   r   r!         z_SerializedPart.reltypec                 C   rY   r	   )rX   rB   r   r   r   r%      rZ   z_SerializedPart.srels)r:   r;   r<   r=   r   propertyr   r    r"   r!   r%   r?   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
   r^   r   _baseURIrId_rIdr!   rV   target_mode_target_mode
target_ref_target_ref)r   r1   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``)rc   RTMEXTERNALrB   r   r   r   r4      s   z#_SerializedRelationship.is_externalc                 C   rY   )z.Relationship type, like ``RT.OFFICE_DOCUMENT``r[   rB   r   r   r   r!      r\   z_SerializedRelationship.reltypec                 C   rY   )zmRelationship id, like 'rId9', corresponds to the ``Id`` attribute on the
        ``CT_Relationship`` element.)ra   rB   r   r   r   r`         z_SerializedRelationship.rIdc                 C   rY   )zwString in ``TargetMode`` attribute of ``CT_Relationship`` element, one of
        ``RTM.INTERNAL`` or ``RTM.EXTERNAL``.)rc   rB   r   r   r   rb      ri   z#_SerializedRelationship.target_modec                 C   rY   )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.)re   rB   r   r   r   rd      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)r4   
ValueErrorhasattrr   from_rel_refr_   rd   rj   )r   msgr   r   r   r5      s   
z'_SerializedRelationship.target_partname)r:   r;   r<   r=   r   r]   r4   r!   r`   rb   rd   r5   r?   r   r   r   r   r^      s    




r^   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   rX   rB   r   r   r   r      s   
z!_SerializedRelationships.__init__c                 C   s
   | j  S )z*Support iteration, e.g. 'for x in srels:'.)rX   __iter__rB   r   r   r   ro      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   Relationship_lstrX   r+   r^   )r1   rels_item_xmlr%   rels_elmrf   r   r   r   r0      s   
z&_SerializedRelationships.load_from_xml)	r:   r;   r<   r=   r   ro   r>   r0   r?   r   r   r   r   r/      s    r/   N)r=   docx.opc.constantsr   rg   docx.opc.oxmlr   docx.opc.packurir   r   docx.opc.phys_pkgr   docx.opc.sharedr   r   r   r*   r^   r/   r   r   r   r   <module>   s    P,%?