o
    ýÞhA#  ã                   @  s¼   d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ dd	lmZ erFdd
lmZ ddlmZ G dd„ dƒZG dd„ dƒZG dd„ deƒZdS )zAOpen Packaging Convention (OPC) objects related to package parts.é    )Úannotations)ÚTYPE_CHECKINGÚCallableÚTypeÚcast)Úserialize_part_xml)ÚPackURI)ÚRelationships)Úcls_method_fn©Ú	parse_xml)Úlazyproperty)ÚBaseOxmlElement)ÚPackagec                      sà   e Zd ZdZ		d7d8‡ fdd„Zdd„ Zdd„ Zed9dd„ƒZedd„ ƒZ	d:dd„Z
ed;dd„ƒZd<d=d#d$„Zed%d&„ ƒZed'd(„ ƒZejd>d)d(„ƒZd?d*d+„Zd<d@d,d-„Zed.d/„ ƒZed0d1„ ƒZdAd2d3„ZdBd5d6„Z‡  ZS )CÚPartz¦Base class for package parts.

    Provides common properties and methods, but intended to be subclassed in client code
    to implement specific part behaviors.
    NÚpartnamer   Úcontent_typeÚstrÚblobúbytes | NoneÚpackageúPackage | Nonec                   s*   t t| ƒ ¡  || _|| _|| _|| _d S ©N)Úsuperr   Ú__init__Ú	_partnameÚ_content_typeÚ_blobÚ_package)Úselfr   r   r   r   ©Ú	__class__© úM/var/www/html/premium_crap/venv/lib/python3.10/site-packages/docx/opc/part.pyr      s
   
zPart.__init__c                 C  ó   dS )z¬Entry point for post-unmarshaling processing, for example to parse the part
        XML.

        May be overridden by subclasses without forwarding call to super.
        Nr"   ©r   r"   r"   r#   Úafter_unmarshal)   ó   zPart.after_unmarshalc                 C  r$   )z»Entry point for pre-serialization processing, for example to finalize part
        naming if necessary.

        May be overridden by subclasses without forwarding call to super.
        Nr"   r%   r"   r"   r#   Úbefore_marshal3   r'   zPart.before_marshalÚreturnÚbytesc                 C  s
   | j pdS )z¸Contents of this package part as a sequence of bytes.

        May be text or binary. Intended to be overridden by subclasses. Default behavior
        is to return load blob.
        ó    )r   r%   r"   r"   r#   r   =   s   
z	Part.blobc                 C  ó   | j S )zContent type of this part.)r   r%   r"   r"   r#   r   F   ó   zPart.content_typeÚrIdc                 C  s   |   |¡dk r| j|= dS dS )z²Remove the relationship identified by `rId` if its reference count is less
        than 2.

        Relationships with a reference count of 0 are implicit relationships.
        é   N)Ú_rel_ref_countÚrels©r   r.   r"   r"   r#   Údrop_relK   s   ÿzPart.drop_relr   c                 C  s   | ||||ƒS r   r"   )Úclsr   r   r   r   r"   r"   r#   ÚloadT   s   z	Part.loadFÚreltypeÚtargetú
Part | strÚis_externalÚboolc                 C  s   | j  ||||¡S )až  Return newly added |_Relationship| instance of `reltype`.

        The new relationship relates the `target` part to this part with key `rId`.

        Target mode is set to ``RTM.EXTERNAL`` if `is_external` is |True|. Intended for
        use during load from a serialized package, where the rId is well-known. Other
        methods exist for adding a new relationship to a part when manipulating a part.
        )r1   Úadd_relationship)r   r6   r7   r.   r9   r"   r"   r#   Úload_relX   s   	zPart.load_relc                 C  r,   )z+|OpcPackage| instance this part belongs to.)r   r%   r"   r"   r#   r   c   r-   zPart.packagec                 C  r,   )zX|PackURI| instance holding partname of this part, e.g.
        '/ppt/slides/slide1.xml'.)r   r%   r"   r"   r#   r   h   s   zPart.partnamec                 C  s*   t |tƒsd}t|t|ƒj ƒ‚|| _d S )Nz.partname must be instance of PackURI, got '%s')Ú
isinstancer   Ú	TypeErrorÚtypeÚ__name__r   )r   r   Útmplr"   r"   r#   r   n   s   

c                 C  s   | j  |¡S )a%  Return part to which this part has a relationship of `reltype`.

        Raises |KeyError| if no such relationship is found and |ValueError| if more than
        one such relationship is found. Provides ability to resolve implicitly related
        part, such as Slide -> SlideLayout.
        )r1   Úpart_with_reltype)r   r6   r"   r"   r#   Úpart_related_byu   s   zPart.part_related_byc                 C  s2   |r| j  |tt|ƒ¡S | j  |tt|ƒ¡}|jS )zÀReturn rId key of relationship of `reltype` to `target`.

        The returned `rId` is from an existing relationship if there is one, otherwise a
        new relationship is created.
        )r1   Úget_or_add_ext_relr   r   Ú
get_or_addr   r.   )r   r7   r6   r9   Úrelr"   r"   r#   Ú	relate_to~   s   zPart.relate_toc                 C  s   | j jS )z³Dictionary mapping related parts by rId, so child objects can resolve
        explicit relationships present in the part XML, e.g. sldIdLst to a specific
        |Slide| instance.)r1   Úrelated_partsr%   r"   r"   r#   rH   Š   s   zPart.related_partsc                 C  s   t | jjƒ| _| jS )zA|Relationships| instance holding the relationships for this part.)r	   r   ÚbaseURIÚ_relsr%   r"   r"   r#   r1   ‘   s   z	Part.relsc                 C  s   | j | }|jS )zGReturn URL contained in target ref of relationship identified by `rId`.)r1   Ú
target_ref)r   r.   rF   r"   r"   r#   rK   ˜   s   
zPart.target_refÚintc                 C  r$   )z¨Return the count of references in this part to the relationship identified by `rId`.

        Only an XML part can contain references, so this is 0 for `Part`.
        r   r"   r2   r"   r"   r#   r0   �   s   zPart._rel_ref_count)NN)r   r   r   r   r   r   r   r   )r)   r*   )r.   r   ©r   r   r   r   r   r*   r   r   )F)r6   r   r7   r8   r.   r   r9   r:   )r   r   )r6   r   r)   r   )r7   r8   r6   r   r9   r:   r)   r   )r.   r   r)   r   ©r.   r   r)   rL   )r@   Ú
__module__Ú__qualname__Ú__doc__r   r&   r(   Úpropertyr   r   r3   Úclassmethodr5   r<   r   r   ÚsetterrC   rG   rH   r   r1   rK   r0   Ú__classcell__r"   r"   r    r#   r      s:    
û



	


	


r   c                   @  sB   e Zd ZU dZded< i Zded< eZddd„Ze	ddd„ƒZ
dS )ÚPartFactoryaå  Provides a way for client code to specify a subclass of |Part| to be constructed
    by |Unmarshaller| based on its content type and/or a custom callable.

    Setting ``PartFactory.part_class_selector`` to a callable object will cause that
    object to be called with the parameters ``content_type, reltype``, once for each
    part in the package. If the callable returns an object, it is used as the class for
    that part. If it returns |None|, part class selection falls back to the content type
    map defined in ``PartFactory.part_type_for``. If no class is returned from either of
    these, the class contained in ``PartFactory.default_part_type`` is used to construct
    the part, which is by default ``opc.package.Part``.
    z.Callable[[str, str], Type[Part] | None] | NoneÚpart_class_selectorzdict[str, Type[Part]]Úpart_type_forr   r   r   r   r6   r   r*   r   r   c                 C  sD   d }| j d urt| dƒ}|||ƒ}|d u r|  |¡}| ||||¡S )NrW   )rW   r
   Ú_part_cls_forr5   )r4   r   r   r6   r   r   Ú	PartClassrW   r"   r"   r#   Ú__new__¶   s   



zPartFactory.__new__c                 C  s   || j v r
| j | S | jS )z’Return the custom part class registered for `content_type`, or the default
        part class if no custom class is registered for `content_type`.)rX   Údefault_part_type)r4   r   r"   r"   r#   rY   Æ   s   

zPartFactory._part_cls_forN)
r   r   r   r   r6   r   r   r*   r   r   )r   r   )r@   rO   rP   rQ   Ú__annotations__rX   r   r\   r[   rS   rY   r"   r"   r"   r#   rV   ¥   s   
 
rV   c                      s^   e Zd ZdZd‡ fd
d„Zedd„ ƒZedd„ ƒZeddd„ƒZ	edd„ ƒZ
ddd„Z‡  ZS )ÚXmlPartzøBase class for package parts containing an XML payload, which is most of them.

    Provides additional methods to the |Part| base class that take care of parsing and
    reserializing the XML payload and managing relationships to other parts.
    r   r   r   r   Úelementr   r   r   c                   s    t t| ƒj|||d� || _d S )N)r   )r   r^   r   Ú_element)r   r   r   r_   r   r    r"   r#   r   Ö   s   
zXmlPart.__init__c                 C  s
   t | jƒS r   )r   r`   r%   r"   r"   r#   r   Ü   s   
zXmlPart.blobc                 C  r,   )z&The root XML element of this XML part.)r`   r%   r"   r"   r#   r_   à   r-   zXmlPart.elementr   r*   c                 C  s   t |ƒ}| ||||ƒS r   r   )r4   r   r   r   r   r_   r"   r"   r#   r5   å   s   zXmlPart.loadc                 C  s   | S )zÒPart of the parent protocol, "children" of the document will not know the
        part that contains them so must ask their parent object.

        That chain of delegation ends here for child objects.
        r"   r%   r"   r"   r#   Úpartê   s   zXmlPart.partr.   r)   rL   c                   s(   t d| j d¡ƒ}t‡ fdd„|D ƒƒS )zbReturn the count of references in this part's XML to the relationship
        identified by `rId`.z	list[str]z//@r:idc                   s   g | ]}|ˆ kr|‘qS r"   r"   )Ú.0Ú_rId©r.   r"   r#   Ú
<listcomp>÷   s    z*XmlPart._rel_ref_count.<locals>.<listcomp>)r   r`   ÚxpathÚlen)r   r.   ÚrIdsr"   rd   r#   r0   ó   s   zXmlPart._rel_ref_count)r   r   r   r   r_   r   r   r   rM   rN   )r@   rO   rP   rQ   r   rR   r   r_   rS   r5   ra   r0   rU   r"   r"   r    r#   r^   Ï   s    


r^   N)rQ   Ú
__future__r   Útypingr   r   r   r   Údocx.opc.oxmlr   Údocx.opc.packurir   Údocx.opc.relr	   Údocx.opc.sharedr
   Údocx.oxml.parserr   Údocx.sharedr   Údocx.oxml.xmlchemyr   Údocx.packager   r   rV   r^   r"   r"   r"   r#   Ú<module>   s     *