o
    ýÞht%  ã                   @   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 )ÚPackageReaderz‚Provides 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__© úR/var/www/html/premium_crap/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 )z°Return 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   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 )z—Generate 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)   @   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   z�Value 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Útyper@   ÚextrA   )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   Ú	overridesÚ_add_overrider   r    ÚdefaultsÚ_add_defaultÚ	extension)r   Ú	types_elmÚ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)rA   )r   rM   r    r   r   r   rL   w   ó   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   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*   z“Value 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 )Ú_SerializedRelationshipzÝValue 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   )zÂString 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/   z€Read-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 )zºReturn |_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    L,%?