a
    6Ra                     @   s   d dl Z d dlmZmZmZmZmZ d dlmZ d dl	m
Z
mZ d dlmZmZmZ G dd deZG dd	 d	eZd
d Zd dlmZ dd ZdddZdS )    N)StringSetNoneSetAliasSequence)Serialisable)REL_NS
PKG_REL_NS)Element
fromstringtostringc                   @   sN   e Zd ZdZd Ze Ze ZedZ	eddZ
eddZedZd	ddZdS )
Relationshipz'Represents many kinds of relationships.TargetT)Z
allow_noneIdNc                 C   s0   |durd t|}|| _|| _|| _|| _dS )z
        `type` can be used as a shorthand with the default relationships namespace
        otherwise the `Type` must be a fully qualified URL
        Nz{0}/{1})formatr   Typer   
TargetModer   )selfr   r   typer   r    r   >lib/python3.9/site-packages/openpyxl/packaging/relationship.py__init__$   s    zRelationship.__init__)NNNNN)__name__
__module____qualname____doc__tagnamer   r   r   r   targetr   r   idr   r   r   r   r   r      s   

     r   c                   @   sT   e Zd ZdZeedZdddZdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )RelationshipListRelationships)Zexpected_typer   c                 C   s
   || _ d S N)r   )r   r   r   r   r   r   >   s    zRelationshipList.__init__c                 C   s8   | j d d  }|| |js.dt||_|| _ d S )NrId{0})r   appendr   r   len)r   valuevaluesr   r   r   r#   B   s
    
zRelationshipList.appendc                 C   s
   t | jS r!   )r$   r   r   r   r   r   __len__J   s    zRelationshipList.__len__c                 C   s
   t | jS r!   )boolr   r'   r   r   r   __bool__N   s    zRelationshipList.__bool__c                 c   s    | j D ]}|j|kr|V  qdS )z
        Find relationships by content-type
        NB. these content-types namespaced objects and different to the MIME-types
        in the package manifest :-(
        N)r   r   )r   Zcontent_typerr   r   r   findR   s    

zRelationshipList.findc                 C   s0   | j D ]}|j|kr|  S qtd|d S )NzUnknown relationship: {0})r   r   KeyErrorr   )r   keyr+   r   r   r   __getitem__]   s    


zRelationshipList.__getitem__c                 C   sF   t dtd}t| jdD ](\}}|js2d||_||  q|S )Nr    )Zxmlns   r"   )r
   r	   	enumerater   r   r   r#   to_tree)r   treeidxrelr   r   r   r2   d   s    zRelationshipList.to_treeN)r   )r   r   r   r   r   r   r   r#   r(   r*   r,   r/   r2   r   r   r   r   r   7   s   

r   c                 C   s&   t | \}}t |dd|}|S )z
    Convert relative path to absolutes that can be loaded from a zip
    archive.
    The path to be passed in is that of containing object (workbook,
    worksheet, etc.)
    Z_relsz{0}.rels)	posixpathsplitjoinr   )pathfolderobjfilenamer   r   r   get_rels_pathn   s    r=   )warnc           
      C   s   |  |}t|}zt|}W n* tyJ   d|}t| t }Y n0 t|}t	|d }|j
D ]J}|jdkr|qjqj|jdr|jdd |_qjt||j}	t|	|_qj|S )zl
    Normalise dependency file paths to absolute ones

    Relative paths are relative to parent object
    z+{0} contains invalid dependency definitionsr   ZExternal/r0   N)readr   r   	from_tree	TypeErrorr   r>   r6   dirnamer7   r   r   r   
startswithr8   normpath)
archiver<   srcZnodeZrelsmsgr:   parentr+   Zpthr   r   r   get_dependents|   s$    




rJ   c           
      C   s   t ||gstd|dur&|| }n*zt||j}W n tyN   Y dS 0 |j}| |}t|}|	|}t
|}	zt| |	|_W n ty   g |_Y n0 |S )z4
    Get related object based on id or rel_type
    z.Either the id or the content type are requiredN)any
ValueErrornextr,   Zrel_typeStopIterationr   r@   r   rA   r=   rJ   depsr-   )
rF   rO   r   clsr5   r9   rG   r3   r;   Z	rels_pathr   r   r   get_rel   s$    


rQ   )NN)r6   Zopenpyxl.descriptorsr   r   r   r   r   Z!openpyxl.descriptors.serialisabler   Zopenpyxl.xml.constantsr   r	   Zopenpyxl.xml.functionsr
   r   r   r   r   r=   warningsr>   rJ   rQ   r   r   r   r   <module>   s    7