o
    Ö|ãa¨4  ã                   @   sÈ   d dl Z d dl mZ zd dlmZmZmZmZmZmZm	Z	m
Z
mZmZ e	dƒZW n ey3   dZ
Y nw dd„ ZG dd	„ d	eƒZeZd
d„ ZG dd„ dee ƒZG dd„ dee ƒZG dd„ deƒZdS )é    N)ÚReferenceType)
ÚIterableÚOptionalÚGenericÚDictÚListÚIteratorÚTypeVarÚTYPE_CHECKINGÚAnyÚCallableÚTFc                 C   s   | d ur| ƒ S d S ©N© )Úrefr   r   ú./usr/lib/python3/dist-packages/debian/_util.pyÚresolve_ref   s   r   c                       s\   e Zd ZdZddgZer‡ fdd„Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Z‡  ZS )Ú_CaseInsensitiveStringzCase insensitive string.
    Ú	str_lowerÚstr_origc                    s    t t| ƒ |¡ d| _d| _d S )NÚ )Úsuperr   Ú__init__r   r   )ÚselfÚs©Ú	__class__r   r   r      s   
z_CaseInsensitiveString.__init__c                 C   s    t  | |¡}||_| ¡ |_|S r   )ÚstrÚ__new__r   Úlowerr   )ÚclsÚstr_r   r   r   r   r   $   s   
z_CaseInsensitiveString.__new__c                 C   ó   | j S r   )r   ©r   r   r   r   Ú__str__,   ó   z_CaseInsensitiveString.__str__c                 C   ó
   t | jƒS r   )Úhashr   r#   r   r   r   Ú__hash__0   ó   
z_CaseInsensitiveString.__hash__c                 C   s&   z| j | ¡ kW S  ty   Y dS w )NF)r   r   ÚAttributeError©r   Úotherr   r   r   Ú__eq__4   s
   ÿz_CaseInsensitiveString.__eq__c                 C   s
   | |k S r   r   r+   r   r   r   Ú__ne__;   r)   z_CaseInsensitiveString.__ne__c                 C   r"   r   )r   r#   r   r   r   r   ?   r%   z_CaseInsensitiveString.lower)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r
   r   r   r$   r(   r-   r.   r   Ú__classcell__r   r   r   r   r      s    r   c                 C   s   |   ¡ S r   )r   ©Úxr   r   r   Údefault_field_sort_keyG   s   r7   c                   @   s~   e Zd ZdZdd„ Zedd„ ƒZejdd„ ƒZdd„ Zd	d
œdd„Z	d	d
œdd„Z
edd„ ƒZedd„ ƒZdd„ Zdd„ ZdS )ÚLinkedListNode)Ú_previous_nodeÚvalueÚ	next_nodeÚ__weakref__c                 C   s   d | _ d | _|| _d S r   )r9   r;   r:   ©r   r:   r   r   r   r   P   ó   
zLinkedListNode.__init__c                 C   r&   r   )r   r9   r#   r   r   r   Úprevious_nodeV   ó   
zLinkedListNode.previous_nodec                 C   s"   |d urt  |¡| _d S d | _d S r   )Úweakrefr   r9   ©r   Únoder   r   r   r?   [   s   "c                 C   s"   t  | j| j¡ d | _d | _| jS r   )r8   Ú
link_nodesr?   r;   r:   r#   r   r   r   Úremove`   s   zLinkedListNode.removeF)Úskip_currentc                c   ó,   � |r| j n| }|r|V  |j }|s
d S d S r   )r;   ©r   rF   rC   r   r   r   Ú	iter_nextg   ó   €þzLinkedListNode.iter_nextc                c   rG   r   )r?   rH   r   r   r   Úiter_previousp   rJ   zLinkedListNode.iter_previousc                 C   s   |r| |_ | r|| _d S d S r   ©r?   r;   rL   r   r   r   rD   y   s
   
ÿzLinkedListNode.link_nodesc                 C   s   t  | |¡ t  ||¡ d S r   )r8   rD   )Ú
first_nodeÚnew_nodeÚ	last_noder   r   r   Ú_insert_link�   s   zLinkedListNode._insert_linkc                 C   s*   | |ur	|| j usJ ‚t | j || ¡ d S r   )r?   r8   rP   ©r   rN   r   r   r   Úinsert_beforeŠ   ó   zLinkedListNode.insert_beforec                 C   s*   | |ur	|| j usJ ‚t | || j ¡ d S r   )r;   r8   rP   rQ   r   r   r   Úinsert_after�   rS   zLinkedListNode.insert_afterN)r/   r0   r1   r3   r   Úpropertyr?   ÚsetterrE   rI   rK   ÚstaticmethodrD   rP   rR   rT   r   r   r   r   r8   L   s$    

ÿ
ÿ	

r8   c                   @   s¢   e Zd ZdZdZd&dd„Zdd„ Zdd	„ Ze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d„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ ZdS )'Ú
LinkedListav  Specialized linked list implementation to support the deb822 parser needs

    We deliberately trade "encapsulation" for features needed by this library
    to facilitate their implementation.  Notably, we allow nodes to leak and assume
    well-behaved calls to remove_node - because that makes it easier to implement
    components like Deb822InvalidParagraphElement.
    ©Ú	head_nodeÚ	tail_nodeÚ_sizeNc                 C   s,   d | _ d | _d| _|d ur|  |¡ d S d S ©Nr   )rZ   r[   r\   Úextend)r   Úvaluesr   r   r   r       s   ÿzLinkedList.__init__c                 C   s
   | j d uS r   )rZ   r#   r   r   r   Ú__bool__¨   r)   zLinkedList.__bool__c                 C   r"   r   )r\   r#   r   r   r   Ú__len__¬   r%   zLinkedList.__len__c                 C   s   | j d ur	| j jS d S r   )r[   r:   r#   r   r   r   Útail°   s   zLinkedList.tailc                 C   s"   | j d u r	tdƒ‚|  | j ¡ d S )Nzpop from empty list)r[   Ú
IndexErrorÚremove_noder#   r   r   r   Úpopµ   s   
zLinkedList.popc                 c   s&   � | j }|d u r
d S | ¡ E d H  d S r   )rZ   rI   )r   rZ   r   r   r   Ú
iter_nodes»   s
   €zLinkedList.iter_nodesc                 c   s   � dd„ |   ¡ D ƒE d H  d S )Nc                 s   ó   � | ]}|j V  qd S r   ©r:   )Ú.0rC   r   r   r   Ú	<genexpr>Ä   ó   € z&LinkedList.__iter__.<locals>.<genexpr>)rf   r#   r   r   r   Ú__iter__Â   s   €zLinkedList.__iter__c                 c   s0   � | j }|d u r
d S dd„ | ¡ D ƒE d H  d S )Nc                 s   rg   r   rh   )ri   Únr   r   r   rj   Ë   rk   z*LinkedList.__reversed__.<locals>.<genexpr>)r[   rK   )r   r[   r   r   r   Ú__reversed__Æ   s
   €zLinkedList.__reversed__c                 C   sl   || j u r|j| _ | j d u rd | _n|| ju r"|j| _| jd us"J ‚| jdks)J ‚|  jd8  _| ¡  d S )Nr   é   )rZ   r;   r[   r?   r\   rE   rB   r   r   r   rd   Í   s   

€
zLinkedList.remove_nodec                 C   s"   | j d u r
|  |¡S |  || j ¡S r   )rZ   ÚappendrR   r=   r   r   r   Úinsert_at_headÜ   s   

zLinkedList.insert_at_headc                 C   sR   t |ƒ}| jd u r|| _|| _n| jd usJ ‚| j |¡ || _|  jd7  _|S )Nro   )r8   rZ   r[   rT   r\   )r   r:   rC   r   r   r   rp   â   s   
zLinkedList.appendc                 C   ó   |   t|ƒ|¡S r   )Úinsert_node_beforer8   ©r   r:   Úexisting_noder   r   r   rR   ð   ó   zLinkedList.insert_beforec                 C   rr   r   )Úinsert_node_afterr8   rt   r   r   r   rT   ô   rv   zLinkedList.insert_afterc                 C   óZ   | j d u r	tdƒ‚|jd us|jd urtdƒ‚| |¡ || j u r$|| _ |  jd7  _|S ©Nz,List is empty; node argument cannot be validz&New node must not already be inserted!ro   )rZ   Ú
ValueErrorr;   r?   rR   r\   ©r   rN   ru   r   r   r   rs   ø   ó   


zLinkedList.insert_node_beforec                 C   rx   ry   )r[   rz   r;   r?   rT   r\   r{   r   r   r   rw     r|   zLinkedList.insert_node_afterc                 C   ó   |D ]}|   |¡ qd S r   )rp   )r   r_   Úvr   r   r   r^     ó   ÿzLinkedList.extendc                 C   s   d | _ d | _d| _d S r]   rY   r#   r   r   r   Úclear  r>   zLinkedList.clearr   )r/   r0   r1   r2   r3   r   r`   ra   rU   rb   re   rf   rl   rn   rd   rq   rp   rR   rT   rs   rw   r^   r€   r   r   r   r   rX   •   s*    

rX   c                   @   s~   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
eZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
OrderedSetzÅA set-like object that preserves order when iterating over it

    We use this to keep track of keys in Deb822Dict, because it's much faster
    to look up if a key is in a set than in a list.
    Nc                 C   s2   i | _ tƒ | _|d u rg }|D ]}|  |¡ qd S r   )Ú_OrderedSet__tablerX   Ú_OrderedSet__orderÚadd©r   ÚiterableÚitemr   r   r   r   #  s   ÿzOrderedSet.__init__c                 C   sF   || vr!| j  |¡}z|| j|< W d S  ty    | j  |¡ ‚ w d S r   )rƒ   rp   r‚   Ú	Exceptionrd   ©r   r‡   rC   r   r   r   r„   1  s   þøzOrderedSet.addc                 C   s"   | j | }| j |= | j |¡ d S r   )r‚   rƒ   rd   r‰   r   r   r   rE   ?  s   
zOrderedSet.removec                 C   r&   r   )Úiterrƒ   r#   r   r   r   rl   G  r@   zOrderedSet.__iter__c                 C   s   t t| jƒƒS r   )rŠ   Úreversedrƒ   r#   r   r   r   rn   L  s   zOrderedSet.__reversed__c                 C   r&   r   )Úlenrƒ   r#   r   r   r   ra   Q  r)   zOrderedSet.__len__c                 C   s
   || j v S r   )r‚   ©r   r‡   r   r   r   Ú__contains__U  s   
zOrderedSet.__contains__c                 C   r}   r   )r„   r…   r   r   r   r^   ^  r   zOrderedSet.extendc                 C   ó   |   || jj¡ dS )z2Re-order the given item so it is "last" in the setN)Ú_reorderrƒ   rp   r�   r   r   r   Ú
order_lastd  ó   zOrderedSet.order_lastc                 C   r�   )z3Re-order the given item so it is "first" in the setN)r�   rƒ   rq   r�   r   r   r   Úorder_firsti  r’   zOrderedSet.order_firstc                    ó4   ||krt dƒ‚ˆj| ‰ ˆ |‡ ‡fdd„¡ dS )zTRe-order the given item so appears directly after the reference item in the sequenceú*Cannot re-order an item relative to itselfc                    ó   ˆj  | ˆ ¡S r   )rƒ   rR   r5   ©Úreference_noder   r   r   Ú<lambda>t  ó    z)OrderedSet.order_before.<locals>.<lambda>N©rz   r‚   r�   ©r   r‡   Úreference_itemr   r—   r   Úorder_beforen  ó   
zOrderedSet.order_beforec                    r”   )zURe-order the given item so appears directly before the reference item in the sequencer•   c                    r–   r   )rƒ   rT   r5   r—   r   r   r™   |  rš   z(OrderedSet.order_after.<locals>.<lambda>Nr›   rœ   r   r—   r   Úorder_afterv  rŸ   zOrderedSet.order_afterc                 C   s.   | j | }| j |¡ ||jƒ}|| j |< d S r   )r‚   rƒ   rd   r:   )r   r‡   Ú
reinserterrC   rN   r   r   r   r�   ~  s   

zOrderedSet._reorderr   )r/   r0   r1   r2   r   r„   rE   rl   rn   ra   rŽ   rp   r^   r‘   r“   rž   r    r�   r   r   r   r   r�     s     
r�   )rA   r   Útypingr   r   r   r   r   r   r	   r
   r   r   r   ÚImportErrorr   r   r   Ú_strIr7   r8   rX   Úobjectr�   r   r   r   r   Ú<module>   s     0ÿ-I 