o
    |a4                     @   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 )
OrderedSetzA 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 