a
    IDgx                     @   s~   d Z ddlZzddlmZmZ W n" eyB   ddlmZmZ Y n0 ddlmZmZ dZ	G dd dej
ZG dd	 d	eZdS )
a   
Simple case insensitive dictionaries.

The :class:`CaseInsensitiveDict` class is a dictionary whose string keys
are case insensitive. It works by automatically coercing string keys to
:class:`CaseInsensitiveKey` objects. Keys that are not strings are
supported as well, just without case insensitivity.

At its core this module works by normalizing strings to lowercase before
comparing or hashing them. It doesn't support proper case folding nor
does it support Unicode normalization, hence the word "simple".
    N)IterableMapping)
basestringunicode)CaseInsensitiveDictCaseInsensitiveKeyc                       s   e Zd ZdZd fdd	Zdd ZedddZd fd	d
	Zd fdd	Z	d fdd	Z
dddZ fddZ fddZ fddZ fddZ  ZS )r   a  
    Simple case insensitive dictionary implementation (that remembers insertion order).

    This class works by overriding methods that deal with dictionary keys to
    coerce string keys to :class:`CaseInsensitiveKey` objects before calling
    down to the regular dictionary handling methods. While intended to be
    complete this class has not been extensively tested yet.
    Nc                    s$   t t|   | j|fi | dS )z1Initialize a :class:`CaseInsensitiveDict` object.N)superr   __init__update)selfotherkw	__class__ c/mounts/lovelace/software/anaconda3/envs/paleomix/lib/python3.9/site-packages/humanfriendly/case.pyr	   0   s    zCaseInsensitiveDict.__init__c                 C   s   t |trt|}|S )a8  
        Coerce string keys to :class:`CaseInsensitiveKey` objects.

        :param key: The value to coerce (any type).
        :returns: If `key` is a string then a :class:`CaseInsensitiveKey`
                  object is returned, otherwise the value of `key` is
                  returned unmodified.
        )
isinstancer   r   r   keyr   r   r   
coerce_key7   s    	
zCaseInsensitiveDict.coerce_keyc                    s   |  fdd|D S )zYCreate a case insensitive dictionary with keys from `iterable` and values set to `value`.c                 3   s   | ]}| fV  qd S )Nr   ).0kvaluer   r   	<genexpr>G       z/CaseInsensitiveDict.fromkeys.<locals>.<genexpr>r   )clsiterabler   r   r   r   fromkeysD   s    zCaseInsensitiveDict.fromkeysc                    s   t t| | ||S )z"Get the value of an existing item.)r   r   getr   r   r   defaultr   r   r   r   I   s    zCaseInsensitiveDict.getc                    s   t t| | ||S )z2Remove an item from a case insensitive dictionary.)r   r   popr   r    r   r   r   r"   M   s    zCaseInsensitiveDict.popc                    s   t t| | ||S )z4Get the value of an existing item or add a new item.)r   r   
setdefaultr   r    r   r   r   r#   Q   s    zCaseInsensitiveDict.setdefaultc                 K   s   t |tr&| D ]\}}|| |< qnDt |trH|D ]\}}|| |< q4n"|durjd}t|j}t|| | D ]\}}|| |< qrdS )z4Update a case insensitive dictionary with new items.Nz'%s' object is not iterable)r   r   itemsr   type__name__	TypeError)r   r   r   r   r   msg	type_namer   r   r   r
   U   s    


zCaseInsensitiveDict.updatec                    s   t t| | |S )z>Check if a case insensitive dictionary contains the given key.)r   r   __contains__r   r   r   r   r   r*   h   s    z CaseInsensitiveDict.__contains__c                    s   t t| | |S )z0Delete an item in a case insensitive dictionary.)r   r   __delitem__r   r   r   r   r   r+   l   s    zCaseInsensitiveDict.__delitem__c                    s   t t| | |S )z:Get the value of an item in a case insensitive dictionary.)r   r   __getitem__r   r   r   r   r   r,   p   s    zCaseInsensitiveDict.__getitem__c                    s   t t| | ||S )z:Set the value of an item in a case insensitive dictionary.)r   r   __setitem__r   )r   r   r   r   r   r   r-   t   s    zCaseInsensitiveDict.__setitem__)N)N)N)N)N)N)r&   
__module____qualname____doc__r	   r   classmethodr   r   r"   r#   r
   r*   r+   r,   r-   __classcell__r   r   r   r   r   %   s   	
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   a  
    Simple case insensitive dictionary key implementation.

    The :class:`CaseInsensitiveKey` class provides an intentionally simple
    implementation of case insensitive strings to be used as dictionary keys.

    If you need features like Unicode normalization or proper case folding
    please consider using a more advanced implementation like the :pypi:`istr`
    package instead.
    c                 C   s(   t | |}| }||_t||_|S )z,Create a :class:`CaseInsensitiveKey` object.)r   __new__lower_normalizedhash_hash_value)r   r   obj
normalizedr   r   r   r3      s
    
zCaseInsensitiveKey.__new__c                 C   s   | j S )z,Get the hash value of the lowercased string.)r7   )r   r   r   r   __hash__   s    zCaseInsensitiveKey.__hash__c                 C   s6   t |tr| j|jkS t |tr.| j| kS tS dS )z!Compare two strings as lowercase.N)r   r   r5   r   r4   NotImplemented)r   r   r   r   r   __eq__   s
    

zCaseInsensitiveKey.__eq__N)r&   r.   r/   r0   r3   r:   r<   r   r   r   r   r   y   s   
r   )r0   collectionscollections.abcr   r   ImportErrorZhumanfriendly.compatr   r   __all__OrderedDictr   r   r   r   r   r   <module>   s   T