HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/constantly/__pycache__/_constants.cpython-38.pyc
U

?f�U�>�@sdZddlmZmZddddddgZdd	lmZdd
lmZddl	m
Z
mZmZe
�Zeee��ZGdd
�d
e
�ZGdd�de�ZGdd�dede
fi��ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Zdd�ZGdd�de�ZGdd�de�ZdS)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@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�	_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.
    cCsd|_t�|_dS�N)�
_container�_constantOrder�_index��self�r�7/usr/lib/python3/dist-packages/constantly/_constants.py�__init__#sz_Constant.__init__cCsd|jj|jfS)zq
        Return text identifying both which constant this is and which
        collection it belongs to.
        z<%s=%s>)r�__name__�namerrrr�__repr__(sz_Constant.__repr__cCs(t||j�r|j|jkstS|j|jkS)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�otherrrr�__lt__0s
�
�z_Constant.__lt__cCs0t||j�r|j|jkstS||kp.|j|jkS)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}.
        rr rrr�__le__Bs
�
�z_Constant.__le__cCs(t||j�r|j|jkstS|j|jkS)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}.
        rr rrr�__gt__Ts
�
�z_Constant.__gt__cCs0t||j�r|j|jkstS||kp.|j|jkS)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}.
        rr rrr�__ge__fs
�
�z_Constant.__ge__cCs||_||_dS)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)rr)r�	containerr�valuerrr�_realizexsz_Constant._realizeN)r�
__module__�__qualname__�__doc__rrr"r#r$r%r(rrrrrs
rcs eZdZdZ�fdd�Z�ZS)�_ConstantsContainerTypeza
    L{_ConstantsContainerType} is a metaclass for creating constants container
    classes.
    cs�tt|��||||�}t|dd�}|dkr.|Sg}|��D]B\}}t||j�r:|jdk	rjtd||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�itemsrr-r�
ValueErrorr�appendr�sorted�_constantFactoryr(�_enumerants)rr�bases�
attributes�clsZconstantType�	constants�
descriptorZ
enumerants�indexZ	enumerantr'�rrrr/�s6
�
��
z_ConstantsContainerType.__new__)rr)r*r+r/�
__classcell__rrr=rr,�sr,c@s@eZdZdZdZdd�Zedd��Zedd��Zed	d
��Z	dS)�_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.
    NcCstd|jf��dS)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)r9rrrr/�sz_ConstantsContainer.__new__cCstS)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)r9rr;rrrr5�s
z$_ConstantsContainer._constantFactorycCs ||jkrt||�St|��dS)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)r6r0r2)r9rrrr�lookupByName�s

z _ConstantsContainer.lookupByNamecCs|j��}tt|dd�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.
        cSs|jSr)r)r;rrr�<lambda>�z3_ConstantsContainer.iterconstants.<locals>.<lambda>)�key)r6�values�iterr4)r9r:rrr�
iterconstantss	
�z!_ConstantsContainer.iterconstants)
rr)r*r+r-r/�classmethodr5rBrHrrrrr?�s



r?�c@seZdZdZdS)ra
    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)rr)r*r+rrrrrsc@seZdZdZeZdS)rze
    A L{Names} subclass contains constants which differ only in their names and
    identities.
    N)rr)r*r+rr-rrrrr!sc@seZdZdZdd�ZdS)ra
    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.
    cCst�|�||_dSr�rrr'�rr'rrrr2s
zValueConstant.__init__N)rr)r*r+rrrrrr*sc@s eZdZdZeZedd��ZdS)rza
    A L{Values} subclass contains constants which are associated with arbitrary
    values.
    cCs,|��D]}|j|kr|Sqt|��dS)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)rHr'r2)r9r'Zconstantrrr�
lookupByValue?s


zValues.lookupByValueN)rr)r*r+rr-rIrMrrrrr8scCs6||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'�namesrr(r)�op�left�rightr'rN�resultrrr�_flagOpSs

rSc@s`eZdZdZefdd�Zdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
e
ZdS)ra
    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.
    cCst�|�||_dSrrKrLrrrrps
zFlagConstant.__init__cCsdt|t�r|}t|g�}n*t|�dkr.|\}ndd�t|��d}t�||||�||_||_	dS)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�joinr4rr(r'rN)rr&rNr'rrrrr(us
zFlagConstant._realizecCstt||�S)z�
        Define C{|} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with all flags set in either instance set.
        )rSr
r rrr�__or__�szFlagConstant.__or__cCstt||�S)z�
        Define C{&} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with only flags set in both instances set.
        )rSrr rrr�__and__�szFlagConstant.__and__cCstt||�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.
        )rSrr rrr�__xor__�szFlagConstant.__xor__cCsDt�}|�|jt�d�|j��D]}|j|j@dkr"||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)rr(rrYrHr')rrR�flagrrr�
__invert__�s
zFlagConstant.__invert__cs�fdd��jD�S)zI
        @return: An iterator of flags set on this instance set.
        c3s|]}�j�|�VqdSr)rrB)�.0rrrr�	<genexpr>�sz(FlagConstant.__iter__.<locals>.<genexpr>)rNrrrr�__iter__�szFlagConstant.__iter__cCs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)rr_rrr�__contains__�s	zFlagConstant.__contains__cCs
t|j�S)zL
        @return: C{False} if this flag's value is 0, else C{True}.
        )rdr'rrrr�__nonzero__�szFlagConstant.__nonzero__N)rr)r*r+rArr(r\r]r^r`rcrerf�__bool__rrrrrhs	
c@s$eZdZdZeZdZedd��ZdS)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.
    rTcCs4|jtkr |j}|jdK_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}.
        rT)r'rA�_value)r9rr;r'rrrr5�s

zFlags._constantFactoryN)	rr)r*r+rr-rhrIr5rrrrr	�s
N)r+�
__future__rr�__all__�	functoolsr
�	itertoolsr�operatorrr
r�objectrA�nextrr�typer,r?rrrrrSrr	rrrr�<module>s.�q?M	m