o
    ýÞhà  ã                   @  sn   d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 er"ddl
mZ G dd„ deedf ƒZG d	d„ dƒZd
S )zRelationship-related objects.é    )Úannotations)ÚTYPE_CHECKINGÚAnyÚDictÚcast)ÚCT_Relationships)ÚPartc                      sŽ   e Zd ZdZd&‡ fdd„Z	d'd(dd„Zd)dd„Zd*dd„Zd+dd„Ze	dd„ ƒZ
e	d,dd„ƒZ	d'd-d d!„Zd.d"d#„Ze	d,d$d%„ƒZ‡  ZS )/ÚRelationshipszGCollection object for |_Relationship| instances, having list semantics.ÚbaseURIÚstrc                   s   t t| ƒ ¡  || _i | _d S ©N)Úsuperr	   Ú__init__Ú_baseURIÚ_target_parts_by_rId)Úselfr
   ©Ú	__class__© úL/var/www/html/premium_crap/venv/lib/python3.10/site-packages/docx/opc/rel.pyr      s   
zRelationships.__init__FÚreltypeÚtargetú
Part | strÚrIdÚis_externalÚboolÚreturnú'_Relationship'c                 C  s,   t |||| j|ƒ}|| |< |s|| j|< |S )z.Return a newly added |_Relationship| instance.)Ú_Relationshipr   r   )r   r   r   r   r   Úrelr   r   r   Úadd_relationship   s
   
zRelationships.add_relationshipÚtarget_partr   r   c                 C  s,   |   ||¡}|du r| j}|  |||¡}|S )zlReturn relationship of `reltype` to `target_part`, newly added if not already
        present in collection.N)Ú_get_matchingÚ	_next_rIdr    )r   r   r!   r   r   r   r   r   Ú
get_or_add   s
   zRelationships.get_or_addÚ
target_refc                 C  s6   | j ||dd�}|du r| j}| j|||dd�}|jS )z{Return rId of external relationship of `reltype` to `target_ref`, newly added
        if not already present in collection.T)r   N)r"   r#   r    r   )r   r   r%   r   r   r   r   r   Úget_or_add_ext_rel(   s
   z Relationships.get_or_add_ext_relc                 C  s   |   |¡}|jS )zœReturn target part of rel with matching `reltype`, raising |KeyError| if not
        found and |ValueError| if more than one matching relationship is found.)Ú_get_rel_of_typer!   )r   r   r   r   r   r   Úpart_with_reltype1   s   
zRelationships.part_with_reltypec                 C  ó   | j S )z_Dict mapping rIds to target parts for all the internal relationships in the
        collection.)r   ©r   r   r   r   Úrelated_parts7   s   zRelationships.related_partsc                 C  s4   t  ¡ }|  ¡ D ]}| |j|j|j|j¡ q|jS )zoSerialize this relationship collection into XML suitable for storage as a
        .rels file in an OPC package.)	r   ÚnewÚvaluesÚadd_relr   r   r%   r   Úxml)r   Úrels_elmr   r   r   r   r/   =   s   zRelationships.xmlú_Relationship | Nonec                 C  s2   dd	d
„}|   ¡ D ]}|||||ƒr|  S q	dS )zuReturn relationship of matching `reltype`, `target`, and `is_external` from
        collection, or None if not found.r   r   r   r   r   r   r   r   c                 S  s6   | j |krdS | j|krdS | jr| jn| j}||kS )NF)r   r   r%   r!   )r   r   r   r   Ú
rel_targetr   r   r   ÚmatchesL   s   

z,Relationships._get_matching.<locals>.matchesN)r   r   r   r   r   r   r   r   )r-   )r   r   r   r   r3   r   r   r   r   r"   F   s   
ÿzRelationships._get_matchingc                   sV   ‡ fdd„|   ¡ D ƒ}t|ƒdkrd}t|ˆ  ƒ‚t|ƒdkr'd}t|ˆ  ƒ‚|d S )zØReturn single relationship of type `reltype` from the collection.

        Raises |KeyError| if no matching relationship is found. Raises |ValueError| if
        more than one matching relationship is found.
        c                   s   g | ]	}|j ˆ kr|‘qS r   ©r   )Ú.0r   r4   r   r   Ú
<listcomp>_   s    z2Relationships._get_rel_of_type.<locals>.<listcomp>r   z*no relationship of type '%s' in collectioné   z1multiple relationships of type '%s' in collection)r-   ÚlenÚKeyErrorÚ
ValueError)r   r   ÚmatchingÚtmplr   r4   r   r'   Y   s   zRelationships._get_rel_of_typec                 C  s4   t dt| ƒd ƒD ]}d| }|| vr|  S q	dS )zŽNext available rId in collection, starting from 'rId1' and making use of any
        gaps in numbering, e.g. 'rId2' for rIds ['rId1', 'rId3'].r7   é   zrId%dN)Úranger8   )r   ÚnÚrId_candidater   r   r   r#   h   s   ÿþzRelationships._next_rId)r
   r   ©F)
r   r   r   r   r   r   r   r   r   r   )r   r   r!   r   r   r   )r   r   r%   r   r   r   )r   r   r   r   ©r   r   )r   r   r   r   r   r   r   r1   )r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r$   r&   r(   Úpropertyr+   r/   r"   r'   r#   Ú__classcell__r   r   r   r   r	      s"    ÿ


	
	
	ÿ
r	   r   c                      sl   e Zd ZdZ	dd‡ fdd„Zeddd„ƒZeddd„ƒZeddd„ƒZeddd„ƒZ	eddd„ƒZ
‡  ZS )r   z&Value object for relationship to part.Fr   r   r   r   r   r
   Úexternalr   c                   s4   t t| ƒ ¡  || _|| _|| _|| _t|ƒ| _d S r   )	r   r   r   Ú_rIdÚ_reltypeÚ_targetr   r   Ú_is_external)r   r   r   r   r
   rI   r   r   r   r   u   s   z_Relationship.__init__r   c                 C  r)   r   )rM   r*   r   r   r   r      ó   z_Relationship.is_externalc                 C  r)   r   )rK   r*   r   r   r   r   ƒ   rN   z_Relationship.reltypec                 C  r)   r   )rJ   r*   r   r   r   r   ‡   rN   z_Relationship.rIdr   c                 C  s   | j rtdƒ‚td| jƒS )NzOtarget_part property on _Relationship is undefined when target mode is Externalr   )rM   r:   r   rL   r*   r   r   r   r!   ‹   s
   ÿz_Relationship.target_partc                 C  s,   | j r	tt| jƒS td| jƒ}|j | j¡S )Nr   )rM   r   r   rL   ÚpartnameÚrelative_refr   )r   r   r   r   r   r%   “   s   z_Relationship.target_refrA   )
r   r   r   r   r   r   r
   r   rI   r   )r   r   rB   )r   r   )rC   rD   rE   rF   r   rG   r   r   r   r!   r%   rH   r   r   r   r   r   r   s    ÿ
N)rF   Ú
__future__r   Útypingr   r   r   r   Údocx.opc.oxmlr   Údocx.opc.partr   r   r	   r   r   r   r   r   Ú<module>   s    e