Your IP : 216.73.216.196


Current Path : /opt/alt/python37/lib/python3.7/site-packages/constantly/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/constantly/__pycache__/_constants.cpython-37.pyc

B

?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�F/opt/alt/python37/lib/python3.7/site-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__�name)rrrr�__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}.
        )rrrrr)rrrrr�__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}.
        )rrrrr)rrrrr�__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}.
        )rrrrr)rrrrr�__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__rrrrr 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}xN|��D]B\}}t||j�r<|jdk	rltd||j	f��|�
|j||f�q<Wi}x8t|�D],\}	}
}|�
|
|�}|�||
|�|||
<q�W||_|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�basesZ
attributes�clsZconstantType�	constants�
descriptorZ
enumerants�indexZ	enumerantr#)rrrr+�s(

z_ConstantsContainerType.__new__)rr%r&r'r+�
__classcell__rr)rrr(�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)r4rrrr+�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)r4rr6rrrr1�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)r2r,r.)r4rrrr�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|jS)N)r)r6rrr�<lambda>�z3_ConstantsContainer.iterconstants.<locals>.<lambda>)�key)r2�values�iterr0)r4r5rrr�
iterconstantss	
z!_ConstantsContainer.iterconstants)
rr%r&r'r)r+�classmethodr1r<rBrrrrr9�s
r9�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�|�||_dS)N)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,x|��D]}|j|kr
|Sq
Wt|��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)rBr#r.)r4r#Zconstantrrr�
lookupByValue?s

zValues.lookupByValueN)rr%r&r'rr)rCrErrrrr8scCs6||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#rF�resultrrr�_flagOpSs

rKc@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�|�||_dS)N)rrr#)rr#rrrrps
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�joinr0rr$r#rF)rr"rFr#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.
        )rKr
)rrrrr�__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.
        )rKr)rrrrr�__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.
        )rKr)rrrrr�__xor__�szFlagConstant.__xor__cCsHt�}|�|jt�d�x*|j��D]}|j|j@dkr$||O}q$W|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$rrQrBr#)rrJ�flagrrr�
__invert__�szFlagConstant.__invert__cs�fdd��jD�S)zI
        @return: An iterator of flags set on this instance set.
        c3s|]}�j�|�VqdS)N)rr<)�.0r)rrr�	<genexpr>�sz(FlagConstant.__iter__.<locals>.<genexpr>)rF)rr)rr�__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)rrWrrr�__contains__�s	zFlagConstant.__contains__cCs
t|j�S)zL
        @return: C{False} if this flag's value is 0, else C{True}.
        )r\r#)rrrr�__nonzero__�szFlagConstant.__nonzero__N)rr%r&r'r;rr$rTrUrVrXr[r]r^�__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.
    rLcCs4|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}.
        rL)r#r;�_value)r4rr6r#rrrr1�s

zFlags._constantFactoryN)	rr%r&r'rr)r`rCr1rrrrr	�sN)r'�
__future__rr�__all__�	functoolsr
�	itertoolsr�operatorrr
r�objectr;�nextrr�typer(r9rrrrrKrr	rrrr�<module>s$
q?M	m