
    ju                    x    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
d	S )
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.baseURIstrc                r    t          t          |                                            || _        i | _        d S N)superr   __init___baseURI_target_parts_by_rId)selfr	   	__class__s     V/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/docx/opc/rel.pyr   zRelationships.__init__   s4    mT""++---46!!!    Freltypetarget	str | AnyrIdis_externalboolreturn'_Relationship'c                X    t          |||| j        |          }|| |<   |s
|| j        |<   |S )z.Return a newly added |_Relationship| instance.)_Relationshipr   r   )r   r   r   r   r   rels         r   add_relationshipzRelationships.add_relationship   s>     C&$-MMS	 	4-3D%c*
r   c                r    |                      ||          }|| 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   sA       +66;.C''cBBC
r   c                    |                      ||d          }| | 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%   sM       *$ GG;.C''Sd'SSCwr   c                :    |                      |          }|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                    }|                                 D ].}|                    |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   r3   zRelationships.xml:   sX     $'));;== 	T 	TCSWck3>3?SSSS|r   c                ^    d }|                                  D ]} |||||          r|c S dS )zuReturn relationship of matching `reltype`, `target`, and `is_external` from
        collection, or None if not found.c                t    | j         |k    rdS | j        |k    rdS | j        r| j        n| j        }||k    rdS dS )NFT)r   r   r'   r$   )r   r   r   r   
rel_targets        r   matchesz,Relationships._get_matching.<locals>.matchesG   sO    {g%%u+--u+.?OJV##u4r   N)r1   )r   r   r   r   r8   r   s         r   r"   zRelationships._get_matchingC   sU    	 	 	 ;;== 	 	CwsGV[99 


tr   c                    fd|                                  D             }t          |          dk    rd}t          |z            t          |          dk    rd}t          |z            |d         S )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.
        c                *    g | ]}|j         k    |S  )r   ).0r   r   s     r   
<listcomp>z2Relationships._get_rel_of_type.<locals>.<listcomp>\   s%    KKKCCK74J4JC4J4J4Jr   r   z*no relationship of type '%s' in collection   z1multiple relationships of type '%s' in collection)r1   lenKeyError
ValueError)r   r   matchingtmpls    `  r   r*   zRelationships._get_rel_of_typeV   s     LKKK4;;==KKKx==A?D4'>***x==1FDTG^,,,{r   c                f    t          dt          |           dz             D ]}d|z  }|| vr|c S dS )zNext available rId in collection, starting from 'rId1' and making use of any
        gaps in numbering, e.g. 'rId2' for rIds ['rId1', 'rId3'].r>      zrId%dN)ranger?   )r   nrId_candidates      r   r#   zRelationships._next_rIde   sV     q#d))a-(( 	% 	%A#aKMD(($$$$ )	% 	%r   )r	   r
   F)
r   r
   r   r   r   r
   r   r   r   r   )__name__
__module____qualname____doc__r   r    r%   r(   r+   propertyr.   r3   r"   r*   r#   __classcell__r   s   @r   r   r   
   s       QQ7 7 7 7 7 7 NS           ) ) X)
   X   &   % % X% % % % %r   r   r   c                       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.Fr   r
   c                    t          t          |                                            || _        || _        || _        || _        t          |          | _        d S r   )	r   r   r   _rId_reltype_targetr   r   _is_external)r   r   r   r   r	   externalr   s         r   r   z_Relationship.__init__r   sO    mT""++---	 NNr   c                    | j         S r   )rV   r-   s    r   r   z_Relationship.is_externalz   s      r   c                    | j         S r   )rT   r-   s    r   r   z_Relationship.reltype~   s
    }r   c                    | j         S r   )rS   r-   s    r   r   z_Relationship.rId   s
    yr   c                <    | j         rt          d          | j        S )NzOtarget_part property on _Relationship is undefined when target mode is External)rV   rA   rU   r-   s    r   r$   z_Relationship.target_part   s.     	4   |r   r   c                f    | j         r| j        S | j        j                            | j                  S r   )rV   rU   partnamerelative_refr   r-   s    r   r'   z_Relationship.target_ref   s1     	E<<(55dmDDDr   rI   )r   r
   )r   r
   )rJ   rK   rL   rM   r   rN   r   r   r   r$   r'   rO   rP   s   @r   r   r   o   s        00+ + + + + + + ! ! X!   X   X   X E E E XE E E E Er   N)rM   
__future__r   typingr   r   docx.opc.oxmlr   r
   r   r   r;   r   r   <module>rb      s    # # " " " " " "         * * * * * *b% b% b% b% b%Do-. b% b% b%J%E %E %E %E %E %E %E %E %E %Er   