o
    Ñó·i…>  ã                   @   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   ú[C:\Users\psant\Downloads\PAF backend\school\venv\Lib\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>N)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}.
        N©Ú
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}.
        Nr   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}.
        Nr   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}.
        Nr   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ÚclsZconstantTypeÚ	constantsÚ
descriptorZ
enumerantsÚindexZ	enumerantr#   ©r   r   r   r+   Œ   s0   
ÿ
ÿÿ€
z_ConstantsContainerType.__new__)r   r%   r&   r'   r+   Ú__classcell__r   r   r9   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.N)Ú	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.
        N)Ú_unspecified)r5   r   r7   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}.
        N)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   )r7   r   r   r   Ú<lambda>  s    z3_ConstantsContainer.iterconstants.<locals>.<lambda>)ÚkeyN)r2   ÚvaluesÚiterr0   )r5   r6   r   r   r   Úiterconstants  s   
	ÿz!_ConstantsContainer.iterconstants)
r   r%   r&   r'   r)   r+   Úclassmethodr1   r>   rC   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   rF     s    rF   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'   rF   r)   r   r   r   r   rG     s    rG   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   rH   (  s    rH   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}.
        N)rC   r#   r.   )r5   r#   Zconstantr   r   r   ÚlookupByValue=  s
   
ÿzValues.lookupByValueN)r   r%   r&   r'   rH   r)   rD   rN   r   r   r   r   rM   6  s
    rM   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.
    N)r#   ÚnamesÚFlagConstantr$   r   )ÚopÚleftÚrightr#   rO   Úresultr   r   r   Ú_flagOpQ  s
   rU   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 )rP   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   rI   r
   rJ   rK   r   r   r   r   n  rL   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#   rO   )r   r"   rO   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.
        N)rU   r   r   r   r   r   Ú__or__‘  ó   zFlagConstant.__or__c                 C   r^   )zš
        Define C{&} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with only flags set in both instances set.
        N)rU   r   r   r   r   r   Ú__and__™  r`   zFlagConstant.__and__c                 C   r^   )z¨
        Define C{^} on two L{FlagConstant} instances to create a new
        L{FlagConstant} instance with only flags set on exactly one instance
        set.
        N)rU   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   N)rP   r$   r   r[   rC   r#   )r   rT   Ú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   r>   )Ú.0r   r   r   r   Ú	<genexpr>»  s   € z(FlagConstant.__iter__.<locals>.<genexpr>N)rO   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}.
        N)Úbool)r   rc   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}.
        N)rh   r#   r   r   r   r   Ú__nonzero__Ê  s   
zFlagConstant.__nonzero__N)r   r%   r&   r'   r=   r   r$   r_   ra   rb   rd   rg   ri   rj   Ú__bool__r   r   r   r   rP   f  s    	rP   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.
    rV   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}.
        rV   N)r#   r=   Ú_value)r5   r   r7   r#   r   r   r   r1   Ý  s   
þ
zFlags._constantFactoryN)	r   r%   r&   r'   rP   r)   rm   rD   r1   r   r   r   r   rl   Ó  s    rl   N)r'   Ú
__future__r   r   Ú__all__Ú	functoolsr   Ú	itertoolsr   Úoperatorr   r   r   Úobjectr=   Únextr   r	   Útyper(   r;   rF   rG   rH   rM   rU   rP   rl   r   r   r   r   Ú<module>   s$   q?M	m