o
    US{'                     @   s   d Z ddlm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
eZG dd deZG dd deZG dd deZG dd deZdS )z^
Provides a low-level, read-only API to a serialized Open Packaging Convention
(OPC) package.
    )absolute_import   )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__ BD:\Projects\ConvertPro\env\Lib\site-packages\docx\opc\pkgreader.pyr      s   
zPackageReader.__init__c                 C   sB   t | }t|j}t|t}t|||}|  t|||S )zW
        Return 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 )z
        Generate 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 )zo
        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_srels1   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   Zpart_walkerr    r#   r"   r&   r!   r(   r   r   r   r   <   s   
z$PackageReader._load_serialized_partsc                 C   s   |  |}t|j|S )z
        Return |_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   M   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   Zblob_forr*   )	r   r&   Zvisited_partnamesr'   r    r"   Z
part_srelsr#   Znext_walkerr   r   r   r*   W   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   w   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|| )zH
        Return 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typer9   extr:   )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 )zu
        Return 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      s   

z_ContentTypeMap.from_xmlc                 C      || j |< dS )zp
        Add the default mapping of *extension* to *content_type* to this
        content type mapping.
        N)r:   )r   rE   r!   r   r   r   rD         z_ContentTypeMap._add_defaultc                 C   rH   )zo
        Add the default mapping of *partname* to *content_type* to this
        content type mapping.
        N)r9   )r   r    r!   r   r   r   rB      rI   z_ContentTypeMap._add_override)r3   r4   r5   r6   r   rA   r7   r   rD   rB   r8   r   r   r   r   r   r   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   )rJ   r;   r   r   r   r          z_SerializedPart.partnamec                 C   rO   r   )rK   r;   r   r   r   r!      rP   z_SerializedPart.content_typec                 C   rO   r   )rM   r;   r   r   r   r#      rP   z_SerializedPart.blobc                 C   rO   )z?
        The referring relationship type of this part.
        rL   r;   r   r   r   r"      s   z_SerializedPart.reltypec                 C   rO   r   )rN   r;   r   r   r   r&      rP   z_SerializedPart.srels)r3   r4   r5   r6   r   propertyr    r!   r#   r"   r&   r8   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   rS   r   _baseURIrId_rIdr"   rL   target_mode_target_mode
target_ref_target_ref)r   r0   rel_elmr   r   r   r      s   z _SerializedRelationship.__init__c                 C   s   | j tjkS )z9
        True if target_mode is ``RTM.EXTERNAL``
        )rX   RTMZEXTERNALr;   r   r   r   r1      s   z#_SerializedRelationship.is_externalc                 C   rO   )z.Relationship type, like ``RT.OFFICE_DOCUMENT``rQ   r;   r   r   r   r"      s   z_SerializedRelationship.reltypec                 C   rO   )z
        Relationship id, like 'rId9', corresponds to the ``Id`` attribute on
        the ``CT_Relationship`` element.
        )rV   r;   r   r   r   rU         z_SerializedRelationship.rIdc                 C   rO   )z
        String in ``TargetMode`` attribute of ``CT_Relationship`` element,
        one of ``RTM.INTERNAL`` or ``RTM.EXTERNAL``.
        )rX   r;   r   r   r   rW      r]   z#_SerializedRelationship.target_modec                 C   rO   )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.
        )rZ   r;   r   r   r   rY      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)r1   
ValueErrorhasattrr   Zfrom_rel_refrT   rY   r^   )r   msgr   r   r   r2      s   
z'_SerializedRelationship.target_partname)r3   r4   r5   r6   r   rR   r1   r"   rU   rW   rY   r2   r8   r   r   r   r   rS      s    




rS   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   rN   r;   r   r   r   r     s   
z!_SerializedRelationships.__init__c                 C   s
   | j  S )z)Support iteration, e.g. 'for x in srels:')rN   __iter__r;   r   r   r   rb     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   ZRelationship_lstrN   r,   rS   )r0   Zrels_item_xmlr&   Zrels_elmr[   r   r   r   r/     s   
z&_SerializedRelationships.load_from_xml)	r3   r4   r5   r6   r   rb   r7   r/   r8   r   r   r   r   r.     s    r.   N)r6   Z
__future__r   	constantsr   r\   Zoxmlr   Zpackurir   r   Zphys_pkgr   sharedr	   objectr
   r   r+   rS   r.   r   r   r   r   <module>   s   a5%E