o
    >h>                     @   s   d Z ddlmZmZ 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)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   u/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-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__.   
   
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__@   
   
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__R   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__d   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   _realizev   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   r+      s0   


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 )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   )r5   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)r5   r   r8   r   r   r   r1      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}.
        )r2   r,   r.   )r5   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   )r8   r   r   r   <lambda>  s    z3_ConstantsContainer.iterconstants.<locals>.<lambda>)key)r2   valuesiterr0   )r5   r7   r   r   r   iterconstants  s   
	z!_ConstantsContainer.iterconstants)
r   r%   r&   r'   r)   r+   classmethodr1   rA   rF   r   r   r   r   r>      s    


r>    c                   @   s   e Zd ZdZdS )NamedConstanta  
    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   rI     s    rI   c                   @   s   e Zd ZdZeZdS )Namesze
    A L{Names} subclass contains constants which differ only in their names and
    identities.
    N)r   r%   r&   r'   rI   r)   r   r   r   r   rJ     s    rJ   c                   @   s   e Zd ZdZdd ZdS )ValueConstanta  
    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   0     

zValueConstant.__init__N)r   r%   r&   r'   r   r   r   r   r   rK   (  s    rK   c                   @   s    e Zd ZdZeZedd ZdS )Valuesza
    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}.
        )rF   r#   r.   )r5   r#   constantr   r   r   lookupByValue=  s
   
zValues.lookupByValueN)r   r%   r&   r'   rK   r)   rG   rR   r   r   r   r   rP   6  s
    rP   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#   namesFlagConstantr$   r   )opleftrightr#   rS   resultr   r   r   _flagOpQ  s
   rY   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 )rT   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   rL   r
   rM   rN   r   r   r   r   n  rO   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joinr0   r	   r$   r#   rS   )r   r"   rS   r#   r   r   r   r   r$   s  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.
        )rY   r   r   r   r   r   __or__     zFlagConstant.__or__c                 C   rb   )z
        Define C{&} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with only flags set in both instances set.
        )rY   r   r   r   r   r   __and__  rd   zFlagConstant.__and__c                 C   rb   )z
        Define C{^} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with only flags set on exactly one instance
        set.
        )rY   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   )rT   r$   r   r_   rF   r#   )r   rX   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>)rS   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   rg   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}.
        )rl   r#   r   r   r   r   __nonzero__  s   
zFlagConstant.__nonzero__N)r   r%   r&   r'   r@   r   r$   rc   re   rf   rh   rk   rm   rn   __bool__r   r   r   r   rT   f  s    	rT   c                   @   s$   e Zd ZdZeZdZedd ZdS )Flagsz
    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.
    rZ   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}.
        rZ   )r#   r@   _value)r5   r   r8   r#   r   r   r   r1     s   

zFlags._constantFactoryN)	r   r%   r&   r'   rT   r)   rq   rG   r1   r   r   r   r   rp     s    rp   N)r'   
__future__r   r   __all__	functoolsr   	itertoolsr   operatorr   r   r   objectr@   nextr   r	   typer(   r>   rI   rJ   rK   rP   rY   rT   rp   r   r   r   r   <module>   s$   q?M	m