
    ,h%                         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      Z G d	 d
      Z G d d      Z G d d      Z G d d      Zy)zQLow-level, read-only API to a serialized Open Packaging Convention (OPC) package.    )RELATIONSHIP_TARGET_MODE)	parse_xml)PACKAGE_URIPackURI)PhysPkgReader)CaseInsensitiveDictc                   p     e Zd ZdZ fdZed        Zd Zd Zed        Z	ed        Z
ed	d       Z xZS )
PackageReaderzProvides access to the contents of a zip-format OPC package via its
    :attr:`serialized_parts` and :attr:`pkg_srels` attributes.c                 F    t         t        |           || _        || _        y N)superr
   __init__
_pkg_srels_sparts)selfcontent_types	pkg_srelssparts	__class__s       T/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/docx/opc/pkgreader.pyr   zPackageReader.__init__   s    mT+-#    c                     t        |       }t        j                  |j                        }t        j                  |t              }t        j                  |||      }|j                          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   s        r   	from_filezPackageReader.from_file   sj     $H-'001N1NO!,,[+F	55M
 	]Iv>>r   c              #      K   | j                   D ]2  }|j                  |j                  |j                  |j                  f 4 yw)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   ss     r   iter_spartszPackageReader.iter_sparts   s<       	BA::q~~qyy!&&AA	Bs   AAc              #      K   | j                   D ]  }t        |f  | j                  D ]#  }|j                  D ]  }|j                  |f  % yw)z]Generate a 2-tuple `(source_uri, srel)` for each of the relationships in the
        package.N)r   r   r   srelsr#   )r   srelsparts      r   
iter_srelszPackageReader.iter_srels%   s[      OO 	&D%%	&\\ 	-E -~~t,,-	-s   AAc                     g }t         j                  | |      }|D ],  \  }}}}||   }	t        ||	|||      }
|j                  |
       . 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,   s              r   r   z$PackageReader._load_serialized_parts.   sg    
 #44[)L.9 	!*HdGU(2L#HlGT5QEMM% 	! V}r   c                 d    | j                  |      }t        j                  |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_xmls      r   r   zPackageReader._srels_for;   s-     ++J7'55j6H6H(SSr   c              #   L  K   |g }|D ]  }|j                   r|j                  }||v r!|j                  |       |j                  }t        j                  | |      }| j                  |      }||||f t        j                  | ||      }|D ]  \  }}}}||||f   yw)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_partnamer1   r%   r
   r   blob_forr/   )	r    r*   visited_partnamesr+   r#   r%   
part_srelsr&   next_walkers	            r   r/   zPackageReader._walk_phys_partsB   s      $ " 	7D++H,,$$X.llG&11+xHJ''1DT7J77'88Z):K 3> 7.$w667	7s   B"B$r   )__name__
__module____qualname____doc__r   staticmethodr!   r(   r-   r   r   r/   __classcell__r   s   @r   r
   r
   
   sk    B
 	? 	?B- 
 
 T T 7 7r   r
   c                   D     e Zd ZdZ fdZd Zed        Zd Zd Z	 x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                 f    t         t        |           t               | _        t               | _        y r   )r   r   r   r   
_overrides	_defaultsr   r   s    r   r   z_ContentTypeMap.__init__^   s$    ot-/-/,.r   c                    t        |t              sd}t        |t        |      z        || j                  v r| j                  |   S |j
                  | j                  v r| j                  |j
                     S d}t        ||z        )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typerK   extrL   )r   r#   tmpls      r   __getitem__z_ContentTypeMap.__getitem__c   sw    (G,ID4$x.011t&??8,,<<4>>)>>(,,//Ith''r   c                    t        |       }t               }|j                  D ](  }|j                  |j                  |j
                         * |j                  D ](  }|j                  |j                  |j
                         * |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ds        r   r   z_ContentTypeMap.from_xmlo   sv     /0	 "$$ 	=A  Q^^<	=## 	=AQ^^<	=r   c                 "    || j                   |<   y)z^Add the default mapping of `extension` to `content_type` to this content type
        mapping.N)rL   )r   rZ   r$   s      r   rY   z_ContentTypeMap._add_default{   s     %1y!r   c                 "    || j                   |<   y)z]Add the default mapping of `partname` to `content_type` to this content type
        mapping.N)rK   )r   r#   r$   s      r   rW   z_ContentTypeMap._add_override   s     %1!r   )rB   rC   rD   rE   r   rT   rF   r   rY   rW   rG   rH   s   @r   r   r   Z   s1    C/

( 	 	1
1r   r   c                   r     e Zd ZdZ fdZed        Zed        Zed        Zed        Z	ed        Z
 xZS )r0   zValue object for an OPC package part.

    Provides access to the partname, content type, blob, and serialized relationships
    for the part.
    c                 p    t         t        |           || _        || _        || _        || _        || _        y r   )r   r0   r   	_partname_content_type_reltype_blob_srels)r   r#   r$   r%   r&   r*   r   s         r   r   z_SerializedPart.__init__   s4    ot-/!)
r   c                     | j                   S r   )rc   r   s    r   r#   z_SerializedPart.partname   s    ~~r   c                     | j                   S r   )rd   ri   s    r   r$   z_SerializedPart.content_type   s    !!!r   c                     | j                   S r   )rf   ri   s    r   r&   z_SerializedPart.blob   s    zzr   c                     | j                   S )z-The referring relationship type of this part.re   ri   s    r   r%   z_SerializedPart.reltype        }}r   c                     | j                   S r   )rg   ri   s    r   r*   z_SerializedPart.srels   s    {{r   )rB   rC   rD   rE   r   propertyr#   r$   r&   r%   r*   rG   rH   s   @r   r0   r0      sq       " "      r   r0   c                        e Zd ZdZ fdZed        Zed        Zed        Zed        Z	ed        Z
ed        Z x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                     t         t        |           || _        |j                  | _        |j                  | _        |j                  | _	        |j                  | _        y r   )r   rr   r   _baseURIrId_rIdr%   re   target_mode_target_mode
target_ref_target_ref)r   r8   rel_elmr   s      r   r   z _SerializedRelationship.__init__   sJ    %t57KK	#//"--r   c                 <    | j                   t        j                  k(  S )z'True if target_mode is ``RTM.EXTERNAL``)rx   RTMEXTERNALri   s    r   r<   z#_SerializedRelationship.is_external   s       CLL00r   c                     | j                   S )z.Relationship type, like ``RT.OFFICE_DOCUMENT``rm   ri   s    r   r%   z_SerializedRelationship.reltype   rn   r   c                     | j                   S )zmRelationship id, like 'rId9', corresponds to the ``Id`` attribute on the
        ``CT_Relationship`` element.)rv   ri   s    r   ru   z_SerializedRelationship.rId   s     yyr   c                     | j                   S )zwString in ``TargetMode`` attribute of ``CT_Relationship`` element, one of
        ``RTM.INTERNAL`` or ``RTM.EXTERNAL``.)rx   ri   s    r   rw   z#_SerializedRelationship.target_mode   s        r   c                     | 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.)rz   ri   s    r   ry   z"_SerializedRelationship.target_ref   s    
 r   c                     | j                   rd}t        |      t        | d      s/t        j                  | 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   from_rel_refrt   ry   r   )r   msgs     r   r=   z'_SerializedRelationship.target_partname   sW     0  S/!t/0$+$8$8$XD!$$$r   )rB   rC   rD   rE   r   rp   r<   r%   ru   rw   ry   r=   rG   rH   s   @r   rr   rr      s    . 1 1    
 ! !
     % %r   rr   c                   8     e Zd ZdZ fdZd Zed        Z xZS )r6   zRead-only sequence of |_SerializedRelationship| instances corresponding to the
    relationships item XML passed to constructor.c                 8    t         t        |           g | _        y r   )r   r6   r   rg   rM   s    r   r   z!_SerializedRelationships.__init__   s    &68r   c                 6    | j                   j                         S )z*Support iteration, e.g. 'for x in srels:'.)rg   __iter__ri   s    r   r   z!_SerializedRelationships.__iter__   s    {{##%%r   c                     t               }|At        |      }|j                  D ]'  }|j                  j	                  t        | |             ) |S )zReturn |_SerializedRelationships| instance loaded with the relationships
        contained in `rels_item_xml`.

        Returns an empty collection if `rels_item_xml` is |None|.
        )r6   r   Relationship_lstrg   r1   rr   )r8   rels_item_xmlr*   rels_elmr{   s        r   r7   z&_SerializedRelationships.load_from_xml   sR     )*$ /H#44 O##$;GW$MNOr   )	rB   rC   rD   rE   r   r   rF   r7   rG   rH   s   @r   r6   r6      s&    5&  r   r6   N)rE   docx.opc.constantsr   r}   docx.opc.oxmlr   docx.opc.packurir   r   docx.opc.phys_pkgr   docx.opc.sharedr   r
   r   r0   rr   r6    r   r   <module>r      sO    W > # 1 + /M7 M7`)1 )1X" "J<% <%~ r   