a
    ]jt%                     @   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__ N/var/www/html/assistant/venv/lib/python3.9/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  qrq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_parts)N)__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   s^   t |tsd}t|t| || jv r2| j| S |j| jv rJ| j|j S d}t|| dS )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].xmlN)
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 q0|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   s   || 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   s    z_ContentTypeMap._add_defaultc                 C   s   || j |< dS )z]Add the default mapping of `partname` to `content_type` to this content type
        mapping.N)r7   )r   r   r   r   r   r   r@   |   s    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   s   | j S r	   )rF   r9   r   r   r   r      s    z_SerializedPart.partnamec                 C   s   | j S r	   )rG   r9   r   r   r   r      s    z_SerializedPart.content_typec                 C   s   | j S r	   )rI   r9   r   r   r   r!      s    z_SerializedPart.blobc                 C   s   | j S )z-The referring relationship type of this part.rH   r9   r   r   r   r       s    z_SerializedPart.reltypec                 C   s   | j S r	   )rJ   r9   r   r   r   r$      s    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
   rM   r   _baseURIrId_rIdr    rH   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``)rR   RTMZEXTERNALr9   r   r   r   r/      s    z#_SerializedRelationship.is_externalc                 C   s   | j S )z.Relationship type, like ``RT.OFFICE_DOCUMENT``rK   r9   r   r   r   r       s    z_SerializedRelationship.reltypec                 C   s   | j S )zmRelationship id, like 'rId9', corresponds to the ``Id`` attribute on the
        ``CT_Relationship`` element.)rP   r9   r   r   r   rO      s    z_SerializedRelationship.rIdc                 C   s   | j S )zwString in ``TargetMode`` attribute of ``CT_Relationship`` element, one of
        ``RTM.INTERNAL`` or ``RTM.EXTERNAL``.)rR   r9   r   r   r   rQ      s    z#_SerializedRelationship.target_modec                 C   s   | 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.)rT   r9   r   r   r   rS      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_refrN   rS   rW   )r   msgr   r   r   r0      s    
z'_SerializedRelationship.target_partname)r1   r2   r3   r4   r   rL   r/   r    rO   rQ   rS   r0   r6   r   r   r   r   rM      s   




rM   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   rJ   r9   r   r   r   r      s    z!_SerializedRelationships.__init__c                 C   s
   | j  S )z*Support iteration, e.g. 'for x in srels:'.)rJ   __iter__r9   r   r   r   r[      s    z!_SerializedRelationships.__iter__c                 C   s8   t  }|dur4t|}|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_lstrJ   r*   rM   )r.   Zrels_item_xmlr$   Zrels_elmrU   r   r   r   r-      s    
z&_SerializedRelationships.load_from_xml)	r1   r2   r3   r4   r   r[   r5   r-   r6   r   r   r   r   r,      s
   r,   N)r4   Zdocx.opc.constantsr   rV   Zdocx.opc.oxmlr   Zdocx.opc.packurir   r   Zdocx.opc.phys_pkgr   Zdocx.opc.sharedr   r   r   r)   rM   r,   r   r   r   r   <module>   s   L,%?