
    Pqhu                    d    d Z ddlmZ ddlmZmZ ddlmZ  G d deedf         Z	 G d d      Z
y	)
zRelationship-related objects.    )annotations)AnyDict)CT_Relationshipsc                       e Zd ZdZd fdZ	 d	 	 	 	 	 	 	 	 	 ddZd Zd Zd Ze	d        Z
e	d        Zdd	Zd
 Ze	d        Z xZS )RelationshipszGCollection object for |_Relationship| instances, having list semantics.c                F    t         t        |           || _        i | _        y N)superr   __init___baseURI_target_parts_by_rId)selfbaseURI	__class__s     M/srv/www/ollama/chat-ollama/venv/lib/python3.12/site-packages/docx/opc/rel.pyr   zRelationships.__init__   s    mT+-46!    c                d    t        |||| j                  |      }|| |<   |s|| j                  |<   |S )z.Return a newly added |_Relationship| instance.)_Relationshipr   r   )r   reltypetargetrIdis_externalrels         r   add_relationshipzRelationships.add_relationship   s;     C&$--MS	-3D%%c*
r   c                l    | j                  ||      }|| j                  }| j                  |||      }|S )zlReturn relationship of `reltype` to `target_part`, newly added if not already
        present in collection.)_get_matching	_next_rIdr   )r   r   target_partr   r   s        r   
get_or_addzRelationships.get_or_add   s=       +6;..C''cBC
r   c                    | j                  ||d      }|!| j                  }| j                  |||d      }|j                  S )z{Return rId of external relationship of `reltype` to `target_ref`, newly added
        if not already present in collection.T)r   )r   r   r   r   )r   r   
target_refr   r   s        r   get_or_add_ext_relz Relationships.get_or_add_ext_rel%   sK       *$ G;..C''Sd'SCwwr   c                <    | j                  |      }|j                  S )zReturn 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   s      r   part_with_reltypezRelationships.part_with_reltype.   s     ##G,r   c                    | j                   S )z_Dict mapping rIds to target parts for all the internal relationships in the
        collection.)r   r   s    r   related_partszRelationships.related_parts4   s     (((r   c                    t        j                         }| j                         D ]>  }|j                  |j                  |j
                  |j                  |j                         @ |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   s      r   r.   zRelationships.xml:   sW     $'');;= 	TCSWWckk3>>3??S	T||r   c                R    d }| j                         D ]  } |||||      s|c S  y)zuReturn relationship of matching `reltype`, `target`, and `is_external` from
        collection, or None if not found.c                    | j                   |k7  ry| j                  |k7  ry| j                  r| j                  n| j                  }||k7  ryy)NFT)r   r   r"   r   )r   r   r   r   
rel_targets        r   matchesz,Relationships._get_matching.<locals>.matchesG   sC    {{g%+-+.??JV#r   N)r,   )r   r   r   r   r3   r   s         r   r   zRelationships._get_matchingC   s6    	 ;;= 	CsGV[9
	 r   c                    | j                         D cg c]  }|j                  |k(  s| }}t        |      dk(  rd}t        ||z        t        |      dkD  rd}t	        ||z        |d   S c c}w )zReturn 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.
        r   z*no relationship of type '%s' in collection   z1multiple relationships of type '%s' in collection)r,   r   lenKeyError
ValueError)r   r   r   matchingtmpls        r   r%   zRelationships._get_rel_of_typeV   sx     $(;;=KCCKK74JCKKx=A?D4'>**x=1FDTG^,,{ Ls
   A/A/c                X    t        dt        |       dz         D ]  }d|z  }|| vs|c S  y)zNext available rId in collection, starting from 'rId1' and making use of any
        gaps in numbering, e.g. 'rId2' for rIds ['rId1', 'rId3'].r5      zrId%dN)ranger6   )r   nrId_candidates      r   r   zRelationships._next_rIde   s9     q#d)a-( 	%A#aKMD($$	%r   )r   strF)
r   r@   r   z	str | Anyr   r@   r   boolreturnz'_Relationship')__name__
__module____qualname____doc__r   r   r    r#   r&   propertyr)   r.   r   r%   r   __classcell__r   s   @r   r   r   
   s    Q7 NS$-47FJ	 ) )
  & % %r   r   r   c                  x     e Zd ZdZdd	 fdZed        Zed        Zed        Zed        Z	ed
d       Z
 xZS )r   z&Value object for relationship to part.c                    t         t        |           || _        || _        || _        || _        t        |      | _        y r
   )	r   r   r   _rId_reltype_targetr   rB   _is_external)r   r   r   r   r   externalr   s         r   r   z_Relationship.__init__r   s8    mT+-	 Nr   c                    | j                   S r
   )rP   r(   s    r   r   z_Relationship.is_externalz   s       r   c                    | j                   S r
   )rN   r(   s    r   r   z_Relationship.reltype~   s    }}r   c                    | j                   S r
   )rM   r(   s    r   r   z_Relationship.rId   s    yyr   c                H    | j                   rt        d      | j                  S )NzOtarget_part property on _Relationship is undefined when target mode is External)rP   r8   rO   r(   s    r   r   z_Relationship.target_part   s)    4  ||r   c                    | j                   r| j                  S | j                  j                  j                  | j                        S r
   )rP   rO   partnamerelative_refr   r(   s    r   r"   z_Relationship.target_ref   s4    <<<<((55dmmDDr   rA   )r   r@   )rC   r@   )rD   rE   rF   rG   r   rH   r   r   r   r   r"   rI   rJ   s   @r   r   r   o   sq    0+ ! !       E Er   N)rG   
__future__r   typingr   r   docx.opc.oxmlr   r@   r   r    r   r   <module>r]      s6    # "  *b%Do-. b%J%E %Er   