a
    GBìjÁ  ã                   @   s’   d dl m Z  d dlmZ eeƒZddlm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 eefZG d
d„ ded�ZdS )é    )Úcopy)Úkwlisté   )Ú
Descriptor)ÚMetaSerialisable)ÚSequenceÚNestedSequenceÚMultiSequencePart)Ú
namespaced)Úsafe_string)ÚElementÚ	localnamec                   @   s‚   e Zd ZdZdZdZdZdZdZe	dd„ ƒZ
dZed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d„ ZdS )ÚSerialisablea  
    Objects can serialise to XML their attributes and child objects.
    The following class attributes are created by the metaclass at runtime:
    __attrs__ = attributes
    __nested__ = single-valued child treated as an attribute
    __elements__ = child elements
    Nr   c                 C   s   t ‚d S ©N)ÚNotImplementedError)Úself© r   ú`/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/openpyxl/descriptors/serialisable.pyÚtagname(   s    zSerialisable.tagnamec           
      C   s°  t |jƒ}| jD ]"\}}||v r|| ||< ||= qt|ƒD ]\}| d¡rR||= q<|tv rr|| |d| < ||= q<d|v r<| dd¡}|| ||< ||= q<|jr´d| jv r´|j|d< |D ]è}t	|ƒ}|tv rÔd| }t
| |dƒ}|du s¸t|tƒrôq¸t|dƒ�r| |¡}	n"t|jdƒ�r(|j |¡}	n|j}	t|tƒ�rD|	||< q¸t|tƒ�rl| |g ¡ ||  |	¡ q¸t|tƒ�r˜| |jg ¡ ||j  |	¡ q¸|	||< q¸| f i |¤ŽS )z(
        Create object from XML
        Ú{Ú_ú-Ú	attr_textNÚ	from_tree)ÚdictÚattribÚ__namespaced__ÚlistÚ
startswithÚKEYWORDSÚreplaceÚtextÚ	__attrs__r   ÚgetattrÚ
isinstanceÚpropertyÚhasattrr   Úexpected_typer   r   Ú
setdefaultÚappendr	   Ústore)
ÚclsÚnoder   ÚkeyÚnsÚnÚelÚtagÚdescÚobjr   r   r   r   .   sN    




zSerialisable.from_treec                    sš  |d u r| j }| d¡r$|dd … }t| |ˆƒ}t| dˆƒ‰t| ƒ}| jD ]"\}}||v rJ|| ||< ||= qJt||ƒ}d| jv r’tt| dƒƒ|_	| j
D ]ü‰ t| jˆ d ƒ}t| ˆ ƒ}	t|dƒrÐt|	dƒrÐ|j|	_t|	tƒ�rNt|tƒrþ|	sìq˜| ˆ |	ˆ¡g}
n8t|tƒ�r"| j|_| ˆ |	ˆ¡}
n‡ ‡fdd„|	D ƒ}
|
D ]}| |¡ �q:q˜ˆ | jv �rj| ˆ |	ˆ¡}n|	d u �rxq˜n
|	 ˆ ¡}|d ur˜| |¡ q˜|S )Nr   r   Ú	namespacer   c                 3   s   | ]}|  ˆ ˆ¡V  qd S r   )Úto_tree)Ú.0Úv©Z	child_tagr4   r   r   Ú	<genexpr>‘   ó    z'Serialisable.to_tree.<locals>.<genexpr>)r   r   r
   r#   r   r   r   r"   r   r!   Ú__elements__Ú	__class__r&   r4   r$   Ú	seq_typesr   r5   r   Úidx_baser)   Ú
__nested__)r   r   Úidxr4   Úattrsr-   r.   r0   r2   r3   Únodesr,   r   r8   r   r5   j   sL    







zSerialisable.to_treec                 c   s„   | j D ]x}t| |ƒ}| d¡r,|dd … }n4|dkr`d|v r`t| j|ƒ}t|ddƒr`| dd¡}|dkr|d ur|t|ƒfV  qd S )Nr   r   r   Z
hyphenatedFr   )r"   r#   r   r<   r    r   )r   ÚattrÚvaluer2   r   r   r   Ú__iter__    s    


zSerialisable.__iter__c                 C   sN   | j |j ksdS t| ƒt|ƒks$dS | jD ]}t| |ƒt||ƒkr* dS q*dS )NFT)r<   r   r;   r#   )r   Úotherr0   r   r   r   Ú__eq__­   s    
zSerialisable.__eq__c                 C   s
   | |k S r   r   )r   rF   r   r   r   Ú__ne__¸   s    zSerialisable.__ne__c                 C   sn   d  | j| jj¡}g }| j| j D ]2}t| |ƒ}t|tƒr>d }| 	d  |t
|ƒ¡¡ q"d |¡}d ||g¡S )Nz<{0}.{1} object>
Parameters:z{0}={1}z, Ú
)ÚformatÚ
__module__r<   Ú__name__r"   r;   r#   r$   r   r)   ÚreprÚjoin)r   ÚsÚargsÚkr7   r   r   r   Ú__repr__¼   s    þ


zSerialisable.__repr__c                 C   sH   g }| j | j D ]*}t| |ƒ}t|tƒr0t|ƒ}| |¡ qtt|ƒƒS r   )r"   r;   r#   r$   r   Útupler)   Úhash)r   ÚfieldsrC   Úvalr   r   r   Ú__hash__Ì   s    

zSerialisable.__hash__c                 C   s’   t | ƒt |ƒkrtdƒ‚i }| jD ]}t| |ƒp8t||ƒ||< q"| jD ]:}t| |ƒ}t||ƒ}|rt|rt|| ||< qF|pz|||< qF| jf i |¤ŽS )Nz+Cannot combine instances of different types)ÚtypeÚ	TypeErrorr"   r#   r;   r<   )r   rF   ÚvalsrC   r0   ÚaÚbr   r   r   Ú__add__×   s    



zSerialisable.__add__c                 C   sR   | j dd�}| j |¡}| jD ].}|| j| j vrtt| |ƒƒ}t|||ƒ q|S )NÚdummy)r   )	r5   r<   r   Ú__dict__r"   r;   r   r#   Úsetattr)r   ÚxmlÚcprQ   r7   r   r   r   Ú__copy__ç   s    
zSerialisable.__copy__)NNN)rL   rK   Ú__qualname__Ú__doc__r"   r?   r;   r   r>   r%   r   r4   Úclassmethodr   r5   rE   rG   rH   rR   rW   r]   rc   r   r   r   r   r      s&   

;
6r   )Ú	metaclassN)r   Úkeywordr   Ú	frozensetr   Ú r   r   Úsequencer   r   r	   r4   r
   Zopenpyxl.compatr   Zopenpyxl.xml.functionsr   r   r   rS   r=   r   r   r   r   r   Ú<module>   s   