o
    f                     @   s   d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZmZ G dd	 d	eZG d
d de
Zdd Zdd ZdddZdS )    N)warn)StringAliasSequence)Serialisable)ElementList)REL_NS
PKG_REL_NS)Element
fromstringc                   @   sX   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)
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   X/home/ubuntu/webapp/venv/lib/python3.10/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                       sH   e Zd ZdZeZ fddZdd Zdd Zdd	 Z	 fd
dZ
  ZS )RelationshipListRelationshipsc                    s*   t  | |jsdt|  |_d S d S )NrId)superappendr   len)r   value	__class__r   r   r#   ;   s   zRelationshipList.appendc                 c   s     | 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   content_typerr   r   r   findA   s   
zRelationshipList.findc                 C   s*   | D ]}|j |kr|  S qtd|)NzUnknown relationship: {0})r   KeyErrorr   )r   keyr)   r   r   r   getL   s
   
zRelationshipList.getc                 C   s   dd | D S )z,Return a dictionary of relations keyed by idc                 S   s   i | ]}|j |qS r   )r   ).0r)   r   r   r   
<dictcomp>U   s    z,RelationshipList.to_dict.<locals>.<dictcomp>r   )r   r   r   r   to_dictS   s   zRelationshipList.to_dictc                    s   t   }|dt |S )Nxmlns)r"   to_treesetr	   )r   treer&   r   r   r2   X   s   
zRelationshipList.to_tree)r   r   r   r   r   expected_typer#   r*   r-   r0   r2   __classcell__r   r   r&   r   r   5   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.)
    _relsz{0}.rels)	posixpathsplitjoinr   )pathfolderobjfilenamer   r   r   get_rels_path^   s   r?   c           
      C   s   |  |}t|}zt|}W n ty%   d|}t| t }Y nw t|}t	|d }|D ]$}|j
dkr<q4|jdrK|jdd |_q4t||j}	t|	|_q4|S )zl
    Normalise dependency file paths to absolute ones

    Relative paths are relative to parent object
    z+{0} contains invalid dependency definitionsr   External/   N)readr   r   	from_tree	TypeErrorr   r   r8   dirnamer9   r   r   
startswithr:   normpath)
archiver>   srcnoderelsmsgr<   parentr)   pthr   r   r   get_dependentsj   s&   




rP   c           
      C   s   t ||gs
td|dur||}nz
t||j}W n
 ty(   Y dS w |j}| |}t	|}|
|}t|}	z	t| |	|_W |S  tyT   g |_Y |S w )z4
    Get related object based on id or rel_type
    z.Either the id or the content type are requiredN)any
ValueErrorr-   nextr*   rel_typeStopIterationr   rC   r   rD   r?   rP   depsr+   )
rI   rV   r   clsrelr;   rJ   r4   r=   	rels_pathr   r   r   get_rel   s*   

rZ   )NN)r8   warningsr   openpyxl.descriptorsr   r   r   !openpyxl.descriptors.serialisabler   openpyxl.descriptors.containerr   openpyxl.xml.constantsr   r	   openpyxl.xml.functionsr
   r   r   r   r?   rP   rZ   r   r   r   r   <module>   s    )