o
    nګe                     @   s   d Z ddlmZmZ ddlmZmZmZ ddlZddl	Z	ddl
mZ ddgZe	eG dd deZe	eG d	d deeZdS )
z!
Mixin Classes for Attr-support.
    )ABCMetaabstractmethod)MappingMutableMappingSequenceN)mergeAttrMutableAttrc                   @   s\   e Zd ZdZedd Zedd Zdd Zdd	 Z	d
d Z
dd Zdd Zedd ZdS )r   a  
    A mixin class for a mapping that allows for attribute-style access
    of values.

    A key may be used as an attribute if:
     * It is a string
     * It matches /^[A-Za-z][A-Za-z0-9_]*$/ (i.e., a public attribute)
     * The key doesn't overlap with any class attributes (for Attr,
        those would be 'get', 'items', 'keys', 'values', 'mro', and
        'register').

    If a values which is accessed as an attribute is a Sequence-type
    (and is not a string/bytes), it will be converted to a
    _sequence_type with any mappings within it converted to Attrs.

    NOTE: This means that if _sequence_type is not None, then a
        sequence accessed as an attribute will be a different object
        than if accessed as an attribute than if it is accessed as an
        item.
    c                 C   s   dS )zv
        All required state for building a new instance with the same
        settings as the current object.
        N selfr
   r
   f/vol/project/2023/60021/g236002117/DeepSeek-Coder/venv/lib/python3.10/site-packages/attrdict/mixins.py_configuration&   s    zAttr._configurationc                 C   s   t d)a_  
        A standardized constructor used internally by Attr.

        mapping: A mapping of key-value pairs. It is HIGHLY recommended
            that you use this as the internal key-value pair mapping, as
            that will allow nested assignment (e.g., attr.foo.bar = baz)
        configuration: The return value of Attr._configuration
        zYou need to implement this)NotImplementedError)clsmappingconfigurationr
   r
   r   _constructor-   s   
zAttr._constructorc                 C   s,   || vrt dj| jj|d| | | S )z
        Dynamically access a key-value pair.

        key: A key associated with a value in the mapping.

        This differs from __getitem__, because it returns a new instance
        of an Attr (if the value is a Mapping object).
        z)'{cls} instance has no attribute '{name}'r   name)AttributeErrorformat	__class____name___buildr   keyr
   r
   r   __call__9   s   	zAttr.__call__c                 C   s6   || vs	|  |stdj| jj|d| | | S )z1
        Access an item as an attribute.
        z*'{cls}' instance has no attribute '{name}'r   )_valid_namer   r   r   r   r   r   r
   r
   r   __getattr__K   s   zAttr.__getattr__c                 C   s$   t |tstS | t| ||  S )z
        Add a mapping to this Attr, creating a new, merged Attr.

        other: A mapping.

        NOTE: Addition is not commutative. a + b != b + a.
        
isinstancer   NotImplementedr   r   r   r   otherr
   r
   r   __add__X      
zAttr.__add__c                 C   s$   t |tstS | t|| |  S )z
        Add this Attr to a mapping, creating a new, merged Attr.

        other: A mapping.

        NOTE: Addition is not commutative. a + b != b + a.
        r    r#   r
   r
   r   __radd__e   r&   zAttr.__radd__c                    sd   t |tr |  }|S t |tr0t |tjtjfs0t dd}|r0| fdd|D }|S )a  
        Conditionally convert an object to allow for recursive mapping
        access.

        obj: An object that was a key-value pair in the mapping. If obj
            is a mapping, self._constructor(obj, self._configuration())
            will be called. If obj is a non-string/bytes sequence, and
            self._sequence_type is not None, the obj will be converted
            to type _sequence_type and build will be called on its
            elements.
        _sequence_typeNc                 3   s    | ]}  |V  qd S )N)r   ).0elementr   r
   r   	<genexpr>   s    zAttr._build.<locals>.<genexpr>)	r!   r   r   r   r   sixstring_typesbinary_typegetattr)r   objsequence_typer
   r   r   r   r   s   

zAttr._buildc                 C   s$   t |tjotd|ot| | S )a}  
        Check whether a key is a valid attribute name.

        A key may be used as an attribute if:
         * It is a string
         * It matches /^[A-Za-z][A-Za-z0-9_]*$/ (i.e., a public attribute)
         * The key doesn't overlap with any class attributes (for Attr,
            those would be 'get', 'items', 'keys', 'values', 'mro', and
            'register').
        z^[A-Za-z][A-Za-z0-9_]*$)r!   r,   r-   rematchhasattr)r   r   r
   r
   r   r      s
   

zAttr._valid_nameN)r   
__module____qualname____doc__r   r   classmethodr   r   r   r%   r'   r   r   r
   r
   r
   r   r      s    

c                       sF   e Zd ZdZ fddZ fddZ fddZd fd	d
	Z  ZS )r	   z[
    A mixin class for a mapping that allows for attribute-style access
    of values.
    c                    s   t t| || dS )zo
        Add an attribute to the object, without attempting to add it as
        a key to the mapping.
        N)superr	   __setattr__r   r   valuer   r
   r   _setattr   s   zMutableAttr._setattrc                    sL   |  |r|| |< dS t| ddrtt| || dS tdj| jjd)zr
        Add an attribute.

        key: The name of the attribute
        value: The attributes contents
        _allow_invalid_attributesTz*'{cls}' does not allow attribute creation.r   N)	r   r/   r9   r	   r:   	TypeErrorr   r   r   r;   r=   r
   r   r:      s   
zMutableAttr.__setattr__c                    s   t t| | dS )zp
        Delete an attribute from the object, without attempting to
        remove it from the mapping.
        N)r9   r	   __delattr__r   r=   r
   r   _delattr   s   zMutableAttr._delattrFc                    sH   |  |r
| |= dS t| ddrtt| | dS tdj| jjd)zN
        Delete an attribute.

        key: The name of the attribute
        r?   Tz*'{cls}' does not allow attribute deletion.r@   N)	r   r/   r9   r	   rB   rA   r   r   r   )r   r   forcer=   r
   r   rB      s   

zMutableAttr.__delattr__)F)	r   r5   r6   r7   r>   r:   rC   rB   __classcell__r
   r
   r=   r   r	      s    )r7   abcr   r   collectionsr   r   r   r2   r,   attrdict.merger   __all__add_metaclassr   r	   r
   r
   r
   r   <module>   s     