U
    ?fÉUÚ>  ã                   @   s  d Z ddlmZmZ ddddddgZdd	lmZ dd
lmZ ddl	m
Z
mZmZ eƒ Zeeeƒ ƒZG dd„ deƒZG dd„ deƒZG dd„ dedefi ƒƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ ZG dd„ deƒZG dd„ deƒZdS )zi
Symbolic constant support, including collections and constants with text,
numeric, and bit flag values.
é    )ÚdivisionÚabsolute_importÚNamedConstantÚValueConstantÚFlagConstantÚNamesÚValuesÚFlags)Úpartial)Úcount)Úand_Úor_Úxorc                   @   sH   e Zd Z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 )Ú	_Constanta·  
    @ivar _index: A C{int} allocated from a shared counter in order to keep
        track of the order in which L{_Constant}s are instantiated.

    @ivar name: A C{str} giving the name of this constant; only set once the
        constant is initialized by L{_ConstantsContainer}.

    @ivar _container: The L{_ConstantsContainer} subclass this constant belongs
        to; C{None} until the constant is initialized by that subclass.
    c                 C   s   d | _ tƒ | _d S ©N)Ú
_containerÚ_constantOrderÚ_index©Úself© r   ú7/usr/lib/python3/dist-packages/constantly/_constants.pyÚ__init__#   s    z_Constant.__init__c                 C   s   d| j j| jf S )zq
        Return text identifying both which constant this is and which
        collection it belongs to.
        z<%s=%s>)r   Ú__name__Únamer   r   r   r   Ú__repr__(   s    z_Constant.__repr__c                 C   s(   t || jƒr| j|jkstS | j|jk S )aC  
        Implements C{<}.  Order is defined by instantiation order.

        @param other: An object.

        @return: C{NotImplemented} if C{other} is not a constant belonging to
            the same container as this constant, C{True} if this constant is
            defined before C{other}, otherwise C{False}.
        ©Ú
isinstanceÚ	__class__r   ÚNotImplementedr   ©r   Úotherr   r   r   Ú__lt__0   s    
ÿ
þz_Constant.__lt__c                 C   s0   t || jƒr| j|jkstS | |kp.| j|jk S )aP  
        Implements C{<=}.  Order is defined by instantiation order.

        @param other: An object.

        @return: C{NotImplemented} if C{other} is not a constant belonging to
            the same container as this constant, C{True} if this constant is
            defined before or equal to C{other}, otherwise C{False}.
        r   r    r   r   r   Ú__le__B   s    
ÿ
þz_Constant.__le__c                 C   s(   t || jƒr| j|jkstS | j|jkS )aB  
        Implements C{>}.  Order is defined by instantiation order.

        @param other: An object.

        @return: C{NotImplemented} if C{other} is not a constant belonging to
            the same container as this constant, C{True} if this constant is
            defined after C{other}, otherwise C{False}.
        r   r    r   r   r   Ú__gt__T   s    
ÿ
þz_Constant.__gt__c                 C   s0   t || jƒr| j|jkstS | |kp.| j|jkS )aO  
        Implements C{>=}.  Order is defined by instantiation order.

        @param other: An object.

        @return: C{NotImplemented} if C{other} is not a constant belonging to
            the same container as this constant, C{True} if this constant is
            defined after or equal to C{other}, otherwise C{False}.
        r   r    r   r   r   Ú__ge__f   s    
ÿ
þz_Constant.__ge__c                 C   s   || _ || _dS )ao  
        Complete the initialization of this L{_Constant}.

        @param container: The L{_ConstantsContainer} subclass this constant is
            part of.

        @param name: The name of this constant in its container.

        @param value: The value of this constant; not used, as named constants
            have no value apart from their identity.
        N)r   r   )r   Ú	containerr   Úvaluer   r   r   Ú_realizex   s    z_Constant._realizeN)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r#   r$   r%   r(   r   r   r   r   r      s   
r   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )Ú_ConstantsContainerTypeza
    L{_ConstantsContainerType} is a metaclass for creating constants container
    classes.
    c                    sÂ   t t| ƒ | |||¡}t|ddƒ}|dkr.|S g }| ¡ D ]B\}}t||jƒr:|jdk	rjtd||j	f ƒ‚| 
|j||f¡ q:i }t|ƒD ],\}	}
}| |
|¡}| ||
|¡ |||
< qŠ||_|S )a‹  
        Create a new constants container class.

        If C{attributes} includes a value of C{None} for the C{"_constantType"}
        key, the new class will not be initialized as a constants container and
        it will behave as a normal class.

        @param name: The name of the container class.
        @type name: L{str}

        @param bases: A tuple of the base classes for the new container class.
        @type bases: L{tuple} of L{_ConstantsContainerType} instances

        @param attributes: The attributes of the new container class, including
            any constants it is to contain.
        @type attributes: L{dict}
        Ú_constantTypeNz0Cannot use %s as the value of an attribute on %s)Úsuperr,   Ú__new__ÚgetattrÚitemsr   r-   r   Ú
ValueErrorr   Úappendr   ÚsortedÚ_constantFactoryr(   Ú_enumerants)r   r   ÚbasesZ
attributesÚclsZconstantTypeÚ	constantsÚ
descriptorZ
enumerantsÚindexZ	enumerantr'   ©r   r   r   r/   Ž   s6    
   ÿ
 ÿÿ
z_ConstantsContainerType.__new__)r   r)   r*   r+   r/   Ú__classcell__r   r   r<   r   r,   ‰   s   r,   c                   @   s@   e Zd ZdZdZdd„ Zedd„ ƒZedd„ ƒZed	d
„ ƒZ	dS )Ú_ConstantsContaineraÕ  
    L{_ConstantsContainer} is a class with attributes used as symbolic
    constants.  It is up to subclasses to specify what kind of constants are
    allowed.

    @cvar _constantType: Specified by a L{_ConstantsContainer} subclass to
        specify the type of constants allowed by that subclass.

    @cvar _enumerants: A C{dict} mapping the names of constants (eg
        L{NamedConstant} instances) found in the class definition to those
        instances.
    Nc                 C   s   t d| jf ƒ‚dS )z›
        Classes representing constants containers are not intended to be
        instantiated.

        The class object itself is used directly.
        z%s may not be instantiated.N)Ú	TypeErrorr   )r8   r   r   r   r/   Ø   s    z_ConstantsContainer.__new__c                 C   s   t S )a  
        Construct the value for a new constant to add to this container.

        @param name: The name of the constant to create.

        @param descriptor: An instance of a L{_Constant} subclass (eg
            L{NamedConstant}) which is assigned to C{name}.

        @return: L{NamedConstant} instances have no value apart from identity,
            so return a meaningless dummy value.
        )Ú_unspecified)r8   r   r:   r   r   r   r5   â   s    z$_ConstantsContainer._constantFactoryc                 C   s    || j krt| |ƒS t|ƒ‚dS )a™  
        Retrieve a constant by its name or raise a C{ValueError} if there is no
        constant associated with that name.

        @param name: A C{str} giving the name of one of the constants defined
            by C{cls}.

        @raise ValueError: If C{name} is not the name of one of the constants
            defined by C{cls}.

        @return: The L{NamedConstant} associated with C{name}.
        N)r6   r0   r2   )r8   r   r   r   r   ÚlookupByNameò   s    

z _ConstantsContainer.lookupByNamec                 C   s   | j  ¡ }tt|dd„ dƒS )zû
        Iteration over a L{Names} subclass results in all of the constants it
        contains.

        @return: an iterator the elements of which are the L{NamedConstant}
            instances defined in the body of this L{Names} subclass.
        c                 S   s   | j S r   )r   )r:   r   r   r   Ú<lambda>  ó    z3_ConstantsContainer.iterconstants.<locals>.<lambda>)Úkey)r6   ÚvaluesÚiterr4   )r8   r9   r   r   r   Úiterconstants  s    	
ÿz!_ConstantsContainer.iterconstants)
r   r)   r*   r+   r-   r/   Úclassmethodr5   rA   rG   r   r   r   r   r>   È   s   


r>   Ú c                   @   s   e Zd ZdZdS )r   a  
    L{NamedConstant} defines an attribute to be a named constant within a
    collection defined by a L{Names} subclass.

    L{NamedConstant} is only for use in the definition of L{Names}
    subclasses.  Do not instantiate L{NamedConstant} elsewhere and do not
    subclass it.
    N)r   r)   r*   r+   r   r   r   r   r     s   c                   @   s   e Zd ZdZeZdS )r   ze
    A L{Names} subclass contains constants which differ only in their names and
    identities.
    N)r   r)   r*   r+   r   r-   r   r   r   r   r   !  s   c                   @   s   e Zd ZdZdd„ ZdS )r   a  
    L{ValueConstant} defines an attribute to be a named constant within a
    collection defined by a L{Values} subclass.

    L{ValueConstant} is only for use in the definition of L{Values} subclasses.
    Do not instantiate L{ValueConstant} elsewhere and do not subclass it.
    c                 C   s   t  | ¡ || _d S r   ©r   r   r'   ©r   r'   r   r   r   r   2  s    
zValueConstant.__init__N)r   r)   r*   r+   r   r   r   r   r   r   *  s   c                   @   s    e Zd ZdZeZedd„ ƒZdS )r   za
    A L{Values} subclass contains constants which are associated with arbitrary
    values.
    c                 C   s,   |   ¡ D ]}|j|kr|  S qt|ƒ‚dS )a„  
        Retrieve a constant by its value or raise a C{ValueError} if there is
        no constant associated with that value.

        @param value: The value of one of the constants defined by C{cls}.

        @raise ValueError: If C{value} is not the value of one of the constants
            defined by C{cls}.

        @return: The L{ValueConstant} associated with C{value}.
        N)rG   r'   r2   )r8   r'   Zconstantr   r   r   ÚlookupByValue?  s    

zValues.lookupByValueN)r   r)   r*   r+   r   r-   rH   rL   r   r   r   r   r   8  s   c                 C   s6   | |j |j ƒ}| |j|jƒ}tƒ }| |j||¡ |S )a‹  
    Implement a binary operator for a L{FlagConstant} instance.

    @param op: A two-argument callable implementing the binary operation.  For
        example, C{operator.or_}.

    @param left: The left-hand L{FlagConstant} instance.
    @param right: The right-hand L{FlagConstant} instance.

    @return: A new L{FlagConstant} instance representing the result of the
        operation.
    )r'   Únamesr   r(   r   )ÚopÚleftÚrightr'   rM   Úresultr   r   r   Ú_flagOpS  s
    rR   c                   @   s`   e Zd ZdZef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eZdS )r   a  
    L{FlagConstant} defines an attribute to be a flag constant within a
    collection defined by a L{Flags} subclass.

    L{FlagConstant} is only for use in the definition of L{Flags} subclasses.
    Do not instantiate L{FlagConstant} elsewhere and do not subclass it.
    c                 C   s   t  | ¡ || _d S r   rJ   rK   r   r   r   r   p  s    
zFlagConstant.__init__c                 C   sd   t |tƒr|}t|gƒ}n*t|ƒdkr.|\}ndd t|ƒ¡ d }t | |||¡ || _|| _	dS )aR  
        Complete the initialization of this L{FlagConstant}.

        This implementation differs from other C{_realize} implementations in
        that a L{FlagConstant} may have several names which apply to it, due to
        flags being combined with various operators.

        @param container: The L{Flags} subclass this constant is part of.

        @param names: When a single-flag value is being initialized, a C{str}
            giving the name of that flag.  This is the case which happens when
            a L{Flags} subclass is being initialized and L{FlagConstant}
            instances from its body are being realized.  Otherwise, a C{set} of
            C{str} giving names of all the flags set on this L{FlagConstant}
            instance.  This is the case when two flags are combined using C{|},
            for example.
        é   ú{ú,ú}N)
r   ÚstrÚsetÚlenÚjoinr4   r   r(   r'   rM   )r   r&   rM   r'   r   r   r   r   r(   u  s    
zFlagConstant._realizec                 C   s   t t| |ƒS )zš
        Define C{|} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with all flags set in either instance set.
        )rR   r   r    r   r   r   Ú__or__“  s    zFlagConstant.__or__c                 C   s   t t| |ƒS )zš
        Define C{&} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with only flags set in both instances set.
        )rR   r   r    r   r   r   Ú__and__›  s    zFlagConstant.__and__c                 C   s   t t| |ƒS )z¨
        Define C{^} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with only flags set on exactly one instance
        set.
        )rR   r   r    r   r   r   Ú__xor__£  s    zFlagConstant.__xor__c                 C   sD   t ƒ }| | jtƒ d¡ | j ¡ D ]}|j| j@ dkr"||O }q"|S )z™
        Define C{~} on a L{FlagConstant} instance to create a new
        L{FlagConstant} instance with all flags not set on this instance set.
        r   )r   r(   r   rX   rG   r'   )r   rQ   Úflagr   r   r   Ú
__invert__¬  s    
zFlagConstant.__invert__c                    s   ‡ fdd„ˆ j D ƒS )zI
        @return: An iterator of flags set on this instance set.
        c                 3   s   | ]}ˆ j  |¡V  qd S r   )r   rA   )Ú.0r   r   r   r   Ú	<genexpr>½  s     z(FlagConstant.__iter__.<locals>.<genexpr>)rM   r   r   r   r   Ú__iter__¹  s    zFlagConstant.__iter__c                 C   s   t || @ ƒS )z¹
        @param flag: The flag to test for membership in this instance
            set.

        @return: C{True} if C{flag} is in this instance set, else
            C{False}.
        )Úbool)r   r^   r   r   r   Ú__contains__À  s    	zFlagConstant.__contains__c                 C   s
   t | jƒS )zL
        @return: C{False} if this flag's value is 0, else C{True}.
        )rc   r'   r   r   r   r   Ú__nonzero__Ì  s    zFlagConstant.__nonzero__N)r   r)   r*   r+   r@   r   r(   r[   r\   r]   r_   rb   rd   re   Ú__bool__r   r   r   r   r   h  s   	c                   @   s$   e Zd ZdZeZdZedd„ ƒZdS )r	   zµ
    A L{Flags} subclass contains constants which can be combined using the
    common bitwise operators (C{|}, C{&}, etc) similar to a I{bitvector} from a
    language like C.
    rS   c                 C   s4   |j tkr | j}|  jdK  _n|j }|d> | _|S )a
  
        For L{FlagConstant} instances with no explicitly defined value, assign
        the next power of two as its value.

        @param name: The name of the constant to create.

        @param descriptor: An instance of a L{FlagConstant} which is assigned
            to C{name}.

        @return: Either the value passed to the C{descriptor} constructor, or
            the next power of 2 value which will be assigned to C{descriptor},
            relative to the value of the last defined L{FlagConstant}.
        rS   )r'   r@   Ú_value)r8   r   r:   r'   r   r   r   r5   ß  s    

zFlags._constantFactoryN)	r   r)   r*   r+   r   r-   rg   rH   r5   r   r   r   r   r	   Õ  s
   N)r+   Z
__future__r   r   Ú__all__Ú	functoolsr
   Ú	itertoolsr   Úoperatorr   r   r   Úobjectr@   Únextr   r   Útyper,   r>   r   r   r   r   rR   r   r	   r   r   r   r   Ú<module>   s.       þq?M	m