o
    ?fU>                     @   s   d Z ddlmZmZ g d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      

z_Constant.__lt__c                 C   s0   t || jr| j|jkstS | |u 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      

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   r#   z_Constant.__gt__c                 C   s0   t || jr| j|jkstS | |u 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   r%   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u r|S g }| D ]!\}}t||jr>|jdur5td||j	f |
|j||f qi }t|D ]\}	}
}||
|}|||
| |||
< qE||_|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   bases
attributesclsconstantType	constants
descriptor
enumerantsindex	enumerantr)   r   r   r   r1      s0   


z_ConstantsContainerType.__new__)r   r+   r,   r-   r1   __classcell__r   r   rB   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 )z
        Classes representing constants containers are not intended to be
        instantiated.

        The class object itself is used directly.
        z%s may not be instantiated.)	TypeErrorr   )r;   r   r   r   r1      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)r;   r   r>   r   r   r   r7      s   z$_ConstantsContainer._constantFactoryc                 C   s   || j v r
t| |S t|)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}.
        )r8   r2   r4   )r;   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>  s    z3_ConstantsContainer.iterconstants.<locals>.<lambda>)key)r8   valuesiterr6   )r;   r=   r   r   r   iterconstants  s   
	z!_ConstantsContainer.iterconstants)
r   r+   r,   r-   r/   r1   classmethodr7   rG   rL   r   r   r   r   rD      s    


rD    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    r   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    r   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      t |  || _d S r   r   r   r)   r   r)   r   r   r   r   2     

zValueConstant.__init__N)r   r+   r,   r-   r   r   r   r   r   r   *  s    r   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|)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}.
        )rL   r)   r4   )r;   r)   constantr   r   r   lookupByValue?  s
   
zValues.lookupByValueN)r   r+   r,   r-   r   r/   rM   rT   r   r   r   r   r   8  s
    r   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)   rU   resultr   r   r   _flagOpS  s
   rZ   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   rO   r   rP   rQ   r   r   r   r   p  rR   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joinr6   r   r*   r)   rU   )r   r(   rU   r)   r   r   r   r   r*   u  s   

zFlagConstant._realizec                 C      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.
        )rZ   r   r    r   r   r   __or__     zFlagConstant.__or__c                 C   rc   )z
        Define C{&} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with only flags set in both instances set.
        )rZ   r   r    r   r   r   __and__  re   zFlagConstant.__and__c                 C   rc   )z
        Define C{^} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with only flags set on exactly one instance
        set.
        )rZ   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   r`   rL   r)   )r   rY   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   rG   ).0r   r   r   r   	<genexpr>  s    z(FlagConstant.__iter__.<locals>.<genexpr>)rU   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   rh   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}.
        )rm   r)   r   r   r   r   __nonzero__  s   
zFlagConstant.__nonzero__N)r   r+   r,   r-   rF   r   r*   rd   rf   rg   ri   rl   rn   ro   __bool__r   r   r   r   r   h  s    	r   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.
    r[   c                 C   s6   |j tu r| j}|  jdK  _|S |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}.
        r[   )r)   rF   _value)r;   r   r>   r)   r   r   r   r7     s   

zFlags._constantFactoryN)	r   r+   r,   r-   r   r/   rq   rM   r7   r   r   r   r   r	     s    r	   N)r-   
__future__r   r   __all__	functoolsr
   	itertoolsr   operatorr   r   r   objectrF   nextr   r   typer.   rD   r   r   r   r   rZ   r   r	   r   r   r   r   <module>   s$   q?M	m