B
    5d                 @   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
   3/tmp/pip-unpacked-wheel-akj3st3j/attrdict/mixins.py_configuration&   s    zAttr._configurationc             C   s   t ddS )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 thisN)NotImplementedError)clsmappingconfigurationr
   r
   r   _constructor-   s    
zAttr._constructorc             C   s,   || k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   || ks|  |s(tdj| jj|d| | | S )z1
        Access an item as an attribute.
        z*'{cls}' instance has no attribute '{name}')r   r   )_valid_namer   r   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   s    
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   r   )r   r   r
   r
   r   __radd__e   s    
zAttr.__radd__c                sb   t |tr |  }nBt |tr^t |tjtjfs^t dd}|r^| 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.
        Z_sequence_typeNc             3   s   | ]}  |V  qd S )N)r   ).0element)r   r
   r   	<genexpr>   s    zAttr._build.<locals>.<genexpr>)	r   r   r   r   r   sixstring_typesbinary_typegetattr)r   objZ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|| |< n4t| ddr4tt| || ntdj| jjddS )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(   r1   r	   r2   	TypeErrorr   r   r   )r   r   r3   )r   r
   r   r2      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)r1   r	   __delattr__)r   r   )r   r
   r   _delattr   s    zMutableAttr._delattrFc                sH   |  |r| |= n2t| ddr0tt| | ntdj| jjddS )zN
        Delete an attribute.

        key: The name of the attribute
        r5   Tz*'{cls}' does not allow attribute deletion.)r   N)	r   r(   r1   r	   r7   r6   r   r   r   )r   r   force)r   r
   r   r7      s    
zMutableAttr.__delattr__)F)	r   r-   r.   r/   r4   r2   r8   r7   __classcell__r
   r
   )r   r   r	      s
   )r/   abcr   r   collectionsr   r   r   r*   r%   Zattrdict.merger   __all__add_metaclassr   r	   r
   r
   r
   r   <module>   s    