U
    `B                     @   s   d dl Z d dlmZmZ ddddddd	gZd
d Zdd Zdd Zdd Ze	 Z
G dd dZG dd deZdZG dd deZG dd dedZG dd deeZdd ZG dd deZG dd deeZdd Zdd	 Zd d! Zd"d# ZdS )$    N)MappingProxyTypeDynamicClassAttributeEnumMetaEnumIntEnumFlagIntFlagautouniquec                 C   s   t | dpt | dpt | dS )z5Returns True if obj is a descriptor, False otherwise.__get____set__
__delete__)hasattr)obj r   /usr/lib/python3.8/enum.py_is_descriptor   s
    
r   c                 C   sL   t | dkoJ| dd | dd   ko.dkn  oJ| d dkoJ| d dkS )z3Returns True if a __dunder__ name, False otherwise.   N   ___lennamer   r   r   
_is_dunder   s    &

r   c                 C   sL   t | dkoJ| d | d   ko&dkn  oJ| dd dkoJ| dd dkS )z1Returns True if a _sunder_ name, False otherwise.r   r   r      r   r   r   r   r   r   
_is_sunder   s    r    c                 C   s   dd }|| _ d| _dS )z"Make the given class un-picklable.c                 S   s   t d|  d S )Nz%r cannot be pickled)	TypeErrorselfprotor   r   r   _break_on_call_reduce&   s    z6_make_class_unpicklable.<locals>._break_on_call_reducez	<unknown>N)__reduce_ex__
__module__)clsr%   r   r   r   _make_class_unpicklable$   s    r)   c                   @   s   e Zd ZdZeZdS )r	   zP
    Instances are replaced with an appropriate value in Enum class suites.
    N)__name__r'   __qualname____doc__
_auto_nullvaluer   r   r   r   r	   ,   s   c                       s,   e Zd ZdZ fddZ fddZ  ZS )	_EnumDictzTrack enum member order and ensure member names are not reused.

    EnumMeta will use the names found in self._member_names as the
    enumeration member names.

    c                    s&   t    g | _g | _g | _d| _d S )NF)super__init___member_names_last_values_ignore_auto_calledr#   	__class__r   r   r1   :   s
    
z_EnumDict.__init__c                    sd  t |r|dkrtd|dkr<| jr.tdt| d| nV|dkrt|tr`|dd }nt	|}|| _
t|t| j@ }|rtd	|f nt|r|d
krd}n|| jkrtd| n|| j
krnt|sR|| krtd|| | f t|tr:d| _|jtkr4| |dt| j| jdd |_|j}| j| | j| t || dS )zChanges anything not dundered or not a descriptor.

        If an enum member name is used twice, an error is raised; duplicate
        values are not checked for.

        Single underscore (sunder) names are reserved.

        )_order__create_pseudo_member__generate_next_value_	_missing__ignore_z(_names_ are reserved for future Enum user;   z4_generate_next_value_ must be defined before members_generate_next_valuer=   , z-_ignore_ cannot specify already set names: %r	__order__r9   zAttempted to reuse key: %rz%r already defined as: %rTr   N)r    
ValueErrorr5   r!   setattr
isinstancestrreplacesplitlistr4   setr2   r   r   r	   r.   r-   r>   r   r3   appendr0   __setitem__)r#   keyr.   alreadyr7   r   r   rK   A   sB    	



"z_EnumDict.__setitem__)r*   r'   r+   r,   r1   rK   __classcell__r   r   r7   r   r/   3   s   r/   c                       s   e Zd ZdZedd Z fddZdd Zd-dddd	d
ddZdd Z	 fddZ
dd Zdd Zdd Zdd Zdd Zedd Zdd Zdd  Z fd!d"Zdddd	d
d#d$Zd.d%d&Zd'd( Zed)d* Zed+d, Z  ZS )/r   zMetaclass for Enumc                 C   s0   t  }| |\}}|d k	r,t|dd |d< |S )Nr;   )r/   _get_mixins_getattr)metaclsr(   bases	enum_dictmember_type
first_enumr   r   r   __prepare__|   s
    zEnumMeta.__prepare__c              	      s    dg d  d }|D ]} |d  q| |\}|  |\}}}	 fdd jD }
 jD ]
} |= ql dd }t|
ddh@ }|rtdd	|d	 krd
 d	< t
 | || }g |_i |_|_dd | D }i |_d kr0tk	r0d}tfdd|D s0t|  jD ]*}|
| }t|tsX|f}n|}tkrl|f}|	s||}t|ds||_n6||f| }t|dsĈtkr||_n
| |_|j}||_||_|j|  |j D ]"\}}|j|jkr|} qq|j| ||kr0t||| ||j|< z||j|< W n tk
r^   Y nX q6dD ]H}t||}t|d }t||d }|d k	rh||krht||| qhtd k	r|r||_ tj|_|d k	r
t|t!r|"dd# }||jkr
td|S )Nr=   c                    s   i | ]}| | qS r   r   ).0k)	classdictr   r   
<dictcomp>   s      z$EnumMeta.__new__.<locals>.<dictcomp>r9   mro zInvalid enum member name: {0}r?   r,   zAn enumeration.c                 S   s.   h | ]&}|j  D ]\}}t|tr|qqS r   )__dict__itemsrD   r   )rW   crX   vr   r   r   	<setcomp>   s
     
z#EnumMeta.__new__.<locals>.<setcomp>r&   )__getnewargs_ex____getnewargs__r&   
__reduce__c                 3   s   | ]}| j kV  qd S N)r]   rW   m)rT   r   r   	<genexpr>   s     z#EnumMeta.__new__.<locals>.<genexpr>_value_)__repr____str__
__format__r&   r@   z#member order does not match _order_)$
setdefaultrJ   poprO   
_find_new_r2   rI   rB   formatjoinr0   __new___member_names__member_map__member_type_r[   _value2member_map_objectanyr)   rD   tupler   ri   _name___objclass__r1   r^   rC   r!   rP   r   __new_member__rE   rF   rG   )rQ   r(   rR   rY   ignorerL   rU   rr   save_newuse_argsenum_membersr   r9   invalid_names
enum_classdynamic_attributesmethodsmember_namer.   argsenum_membercanonical_memberclass_method
obj_methodenum_methodr7   )rY   rT   r   rr      s    














zEnumMeta.__new__c                 C   s   dS )z6
        classes/types should always be True.
        Tr   r6   r   r   r   __bool__  s    zEnumMeta.__bool__Nr   modulequalnametypestartc                C   s*   |dkr|  | |S | j||||||dS )a  Either returns an existing member, or creates a new enum class.

        This method is used both when an enum class is given a value to match
        to an enumeration member (i.e. Color(3)) and for the functional API
        (i.e. Color = Enum('Color', names='RED GREEN BLUE')).

        When used for the functional API:

        `value` will be the name of the new class.

        `names` should be either a string of white-space/comma delimited names
        (values will start at `start`), or an iterator/mapping of name, value pairs.

        `module` should be set to the module this class is being created in;
        if it is not set, an attempt to find that module will be made, but if
        it fails the class will not be picklable.

        `qualname` should be set to the actual location this class can be found
        at in its module; by default it is set to the global scope.  If this is
        not correct, unpickling will fail in some circumstances.

        `type`, if set, will be mixed in as the first base class.

        Nr   )rr   _create_)r(   r.   namesr   r   r   r   r   r   r   __call__  s    zEnumMeta.__call__c                 C   s:   t |ts$tdt|j| jjf t || o8|j| jkS Nz3unsupported operand type(s) for 'in': '%s' and '%s')rD   r   r!   r   r+   r8   rz   rt   )r(   memberr   r   r   __contains__9  s    
 zEnumMeta.__contains__c                    s(   || j krtd| j t | d S )Nz%s: cannot delete Enum member.)rt   AttributeErrorr*   r0   __delattr__)r(   attrr7   r   r   r   @  s
    
zEnumMeta.__delattr__c                 C   s   ddddg| j  S )Nr8   r,   __members__r'   rs   r6   r   r   r   __dir__H  s    
zEnumMeta.__dir__c                 C   s@   t |rt|z| j| W S  tk
r:   t|dY nX dS )a5  Return the enum member matching `name`

        We use __getattr__ instead of descriptors or inserting into the enum
        class' __dict__ in order to support `name` and `value` being both
        properties for enum members (which live in the class' __dict__) and
        enum members themselves.

        N)r   r   rt   KeyErrorr(   r   r   r   r   __getattr__L  s    	zEnumMeta.__getattr__c                 C   s
   | j | S re   rt   r   r   r   r   __getitem__\  s    zEnumMeta.__getitem__c                    s    fdd j D S )Nc                 3   s   | ]} j | V  qd S re   r   rW   r   r(   r   r   rh   `  s     z$EnumMeta.__iter__.<locals>.<genexpr>r   r   r   r   r   __iter___  s    zEnumMeta.__iter__c                 C   s
   t | jS re   )r   rs   r   r   r   r   __len__b  s    zEnumMeta.__len__c                 C   s
   t | jS )zReturns a mapping of member name->value.

        This mapping lists all enum members, including aliases. Note that this
        is a read-only view of the internal mapping.

        )r   rt   r   r   r   r   r   e  s    zEnumMeta.__members__c                 C   s
   d| j  S )Nz	<enum %r>)r*   r   r   r   r   rj   o  s    zEnumMeta.__repr__c                    s    fddt  jD S )Nc                 3   s   | ]} j | V  qd S re   r   r   r   r   r   rh   s  s     z(EnumMeta.__reversed__.<locals>.<genexpr>)reversedrs   r   r   r   r   __reversed__r  s    zEnumMeta.__reversed__c                    s0   | j di }||krtdt || dS )zBlock attempts to reassign Enum members.

        A simple assignment to the class namespace only changes one of the
        several possible ways to get an Enum member from the Enum class,
        resulting in an inconsistent Enumeration.

        rt   zCannot reassign members.N)r]   getr   r0   __setattr__)r(   r   r.   
member_mapr7   r   r   r   u  s    zEnumMeta.__setattr__c             
   C   s|  | j }|dkr| fn|| f}| |\}	}
|||}t|trP|dd }t|ttfr|rt|d tr|g  }}g }t	|D ]8\}}|

||||dd }|| |||f q|D ].}t|tr|||  }}n|\}}|||< q|||||}|dkrNztdjd }W n* tttfk
rL } zW 5 d}~X Y nX |dkrbt| n||_|dk	rx||_|S )a  Convenience method to create a new Enum class.

        `names` can be:

        * A string containing member names, separated either with spaces or
          commas.  Values are incremented by 1 from `start`.
        * An iterable of member names.  Values are incremented by 1 from `start`.
        * An iterable of (member name, value) pairs.
        * A mapping of member name -> value pairs.

        Nr?   r@   r   r   r*   )r8   rO   rV   rD   rE   rF   rG   ry   rH   	enumerater;   rJ   rr   sys	_getframe	f_globalsr   rB   r   r)   r'   r+   )r(   
class_namer   r   r   r   r   rQ   rR   r   rU   rY   original_nameslast_valuescountr   r.   itemr   member_valuer   excr   r   r   r     s<    
 







zEnumMeta._create_c                    s   t tj| }|rt |}n|} fdd| D }z|jdd d W n$ tk
rn   |jdd d Y nX | |||d} t| _|| j	 | ||< | S )z[
        Create a new Enum subclass that replaces a collection of global constants
        c                    s    g | ]\}} |r||fqS r   r   )rW   r   r.   filterr   r   
<listcomp>  s   z&EnumMeta._convert_.<locals>.<listcomp>c                 S   s   | d | d fS )Nr   r   r   tr   r   r   <lambda>      z$EnumMeta._convert_.<locals>.<lambda>)rL   c                 S   s   | d S Nr   r   r   r   r   r   r     r   )r   )
varsr   modulesr^   sortr!   _reduce_ex_by_namer&   updater   )r(   r   r   r   sourcemodule_globalsmembersr   r   r   	_convert_  s     	

zEnumMeta._convert_c                 O   s$   dd l }|jdtdd | j||S )Nr   zI_convert is deprecated and will be removed in 3.9, use _convert_ instead.r   )
stacklevel)warningswarnDeprecationWarningr   )r(   r   kwargsr   r   r   r   _convert  s     zEnumMeta._convertc                 C   sP   | st tfS dd }| d }t|ts.td|| p8t }|jrHtd||fS )zReturns the type for creating enum members, and the first inherited
        enum class.

        bases: the tuple of bases that was given to __new__

        c                 S   sH   | D ]>}|j D ]2}|tkrqqd|jkrt|tr4q|    S qqd S )Nrr   )__mro__rw   r]   
issubclassr   )rR   chainbaser   r   r   _find_data_type  s    


z.EnumMeta._get_mixins_.<locals>._find_data_typer   zZnew enumerations should be created as `EnumName([mixin_type, ...] [data_type,] enum_type)`zCannot extend enumerations)rw   r   r   r!   rs   )rR   r   rU   rT   r   r   r   rO     s    
zEnumMeta._get_mixins_c           	      C   s   |  dd}|dk	}|dkrpdD ]H}||fD ].}t||d}|ddjtjtjhkr,|} q\q,|dk	r  qpq tj}|tjkrd}nd}|||fS )a  Returns the __new__ to be used for creating the enum members.

        classdict: the class dictionary given to __new__
        member_type: the data type whose __new__ will be used by default
        first_enum: enumeration to check for an overriding __new__

        rr   N)r|   rr   FT)r   rP   rr   rw   r   )	rY   rT   rU   rr   r~   methodpossibletargetr   r   r   r   ro     s*    
zEnumMeta._find_new_)N)N)r*   r'   r+   r,   classmethodrV   rr   r   r   r   r   r   r   r   r   r   propertyr   rj   r   r   r   r   r   staticmethodrO   ro   rN   r   r   r7   r   r   z   s4   
	 
	5
!
c                   @   st   e Zd ZdZdd Zdd Zedd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zedd Zedd ZdS )r   zRGeneric enumeration.

    Derive from this class to define new enumerations.

    c              
   C   s  t || kr|S z| j| W S  tk
r0   Y n: tk
rh   | j D ]}|j|krH|   Y S qHY nX zd }| |}W n* tk
r } z|}d }W 5 d }~X Y nX t	|| r|S t
d|| jf }|d kr|d kr|n|d krtd| j|f }||_|d S )N%r is not a valid %szDerror in %s._missing_: returned %r instead of None or a valid member)r   rv   r   r!   rt   valuesri   r<   	ExceptionrD   rB   r*   __context__)r(   r.   r   r   resulteve_excr   r   r   rr   2  s:    

zEnum.__new__c              	   C   s6   t |D ](}z|d W   S  tk
r.   Y qX q|S )Nr   )r   r!   )r   r   r   r   
last_valuer   r   r   r;   Z  s    zEnum._generate_next_value_c                 C   s   t d|| jf d S Nr   )rB   r*   )r(   r.   r   r   r   r<   c  s    zEnum._missing_c                 C   s   d| j j| j| jf S )N<%s.%s: %r>)r8   r*   rz   ri   r6   r   r   r   rj   g  s
      zEnum.__repr__c                 C   s   d| j j| jf S )N%s.%s)r8   r*   rz   r6   r   r   r   rk   k  s    zEnum.__str__c                    s&    fdd j  D }dddg| S )Nc                    s2   g | ]*}|j D ]}|d  dkr| jkr|qqS )r   r   )r]   rt   )rW   r(   rg   r6   r   r   r   o  s     
z Enum.__dir__.<locals>.<listcomp>r8   r,   r'   )r8   r[   )r#   added_behaviorr   r6   r   r   n  s    
zEnum.__dir__c                 C   s0   | j tkrt}t| }n| j }| j}|||S re   )ru   rw   rE   ri   rl   )r#   format_specr(   valr   r   r   rl   w  s    

zEnum.__format__c                 C   s
   t | jS re   )hashrz   r6   r   r   r   __hash__  s    zEnum.__hash__c                 C   s   | j | jffS re   r8   ri   r"   r   r   r   r&     s    zEnum.__reduce_ex__c                 C   s   | j S )zThe name of the Enum member.)rz   r6   r   r   r   r     s    z	Enum.namec                 C   s   | j S )zThe value of the Enum member.ri   r6   r   r   r   r.     s    z
Enum.valueN)r*   r'   r+   r,   rr   r;   r   r<   rj   rk   r   rl   r   r&   r   r   r.   r   r   r   r   r   ,  s   (	
	

)	metaclassc                   @   s   e Zd ZdZdS )r   z.Enum where members are also (and must be) intsN)r*   r'   r+   r,   r   r   r   r   r     s   c                 C   s   | j S re   r   r"   r   r   r   r     s    r   c                   @   sp   e Zd ZdZdd Zedd Ze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dS )r   zSupport for flagsc              	   C   sd   |s|dk	r|S dS t |D ]:}zt|}W  qXW q tk
rT   td| dY qX qd|d  S )z
        Generate the next value when not given.

        name: the name of the member
        start: the initial start value or None
        count: the number of existing members
        last_value: the last value assigned or None
        Nr   zInvalid Flag value: %rr   )r   	_high_bitr   r!   )r   r   r   r   r   high_bitr   r   r   r;     s    	
zFlag._generate_next_value_c                 C   s.   |}|dk r| }|  |}|dk r*| }|S r   )r:   )r(   r.   original_valuepossible_memberr   r   r   r<     s    
zFlag._missing_c                 C   sb   | j |d}|dkr^t| |\}}|r:td|| jf t| }d|_||_| j 	||}|S )zL
        Create a composite member iff value contains only members.
        Nr   )
rv   r   
_decomposerB   r*   rw   rr   rz   ri   rm   )r(   r.   pseudo_memberr   extra_flagsr   r   r   r:     s    
zFlag._create_pseudo_member_c                 C   s8   t || js&tdt|j| jjf |j| j@ |jkS r   )rD   r8   r!   r   r+   ri   r#   otherr   r   r   r     s     zFlag.__contains__c                 C   sV   | j }| jd k	r$d|j| j| jf S t|| j\}}d|jddd |D | jf S )Nr   |c                 S   s   g | ]}t |jp|jqS r   rE   rz   ri   rf   r   r   r   r     s     z!Flag.__repr__.<locals>.<listcomp>)r8   rz   r*   ri   r   rq   r#   r(   r   	uncoveredr   r   r   rj     s    
zFlag.__repr__c                 C   s   | j }| jd k	r d|j| jf S t|| j\}}t|dkr^|d jd kr^d|j|d jf S d|jddd |D f S d S )Nr   r   r   z%s.%rr   c                 S   s   g | ]}t |jp|jqS r   r   rf   r   r   r   r     s     z Flag.__str__.<locals>.<listcomp>)r8   rz   r*   r   ri   r   rq   r   r   r   r   rk     s    
zFlag.__str__c                 C   s
   t | jS re   )boolri   r6   r   r   r   r     s    zFlag.__bool__c                 C   s"   t || jstS | | j|jB S re   rD   r8   NotImplementedri   r   r   r   r   __or__  s    zFlag.__or__c                 C   s"   t || jstS | | j|j@ S re   r   r   r   r   r   __and__   s    zFlag.__and__c                 C   s"   t || jstS | | j|jA S re   r   r   r   r   r   __xor__  s    zFlag.__xor__c                 C   sN   t | j| j\}}| d}| jD ] }||kr"|j| j@ s"||B }q"| |S r   )r   r8   ri   )r#   r   r   invertedrg   r   r   r   
__invert__
  s    


zFlag.__invert__N)r*   r'   r+   r,   r;   r   r<   r:   r   rj   rk   r   r   r   r   r   r   r   r   r   r     s   
	
c                   @   sT   e Zd ZdZedd Zedd Zdd Zdd	 Zd
d Z	eZ
eZe	Zdd ZdS )r   zSupport for integer-based Flagsc                 C   s*   t |tstd|| jf | |}|S r   )rD   intrB   r*   r:   )r(   r.   
new_memberr   r   r   r<     s    

zIntFlag._missing_c                 C   s   | j |d }|d kr|g}t| |\}}|rtt|}d| }|| j krZ||krZ|| || krjd}q*||N }q*t|D ]*}t| |}d |_||_	| j 
||}q||S )Nr   r   )rv   r   r   r   rJ   r   r  rr   rz   ri   rm   )r(   r.   r   need_to_creater   r   bit
flag_valuer   r   r   r:     s(    



zIntFlag._create_pseudo_member_c                 C   s0   t || jtfstS | | j| |jB }|S re   rD   r8   r  r   ri   )r#   r   r   r   r   r   r   ;  s    zIntFlag.__or__c                 C   s,   t || jtfstS | | j| |j@ S re   r  r   r   r   r   r   A  s    zIntFlag.__and__c                 C   s,   t || jtfstS | | j| |jA S re   r  r   r   r   r   r   F  s    zIntFlag.__xor__c                 C   s   |  | j }|S re   r   )r#   r   r   r   r   r   O  s    zIntFlag.__invert__N)r*   r'   r+   r,   r   r<   r:   r   r   r   __ror____rand____rxor__r   r   r   r   r   r     s   

c                 C   s   |   d S )z@returns index of highest bit, or -1 if value is zero or negativer   )
bit_lengthr.   r   r   r   r   T  s    r   c                 C   s^   g }| j  D ]"\}}||jkr|||jf q|rZddd |D }td| |f | S )z?Class decorator for enumerations ensuring unique member values.z, c                 S   s   g | ]\}}d ||f qS )z%s -> %sr   )rW   aliasr   r   r   r   r   `  s     zunique.<locals>.<listcomp>z duplicate values found in %r: %s)r   r^   r   rJ   rq   rB   )enumeration
duplicatesr   r   alias_detailsr   r   r   r
   X  s    
c                 C   s   |}|dk }|r*dd t | j D }ndd t | j D }g }|D ],\}}|rJ||@ |krJ|| || M }qJ|s|| jkr|| j|  |jdd dd t|d	kr|d j|kr|d ||fS )
z#Extract all members from the value.r   c                 S   s"   g | ]\}}|j d k	r||fqS re   r   rW   r`   rg   r   r   r   r   o  s   
z_decompose.<locals>.<listcomp>c                 S   s*   g | ]"\}}|j d k	st|r||fqS re   )r   _power_of_twor  r   r   r   r   v  s   
 c                 S   s   | j S re   r   )rg   r   r   r   r     r   z_decompose.<locals>.<lambda>T)rL   reverser   )rH   rv   r^   rJ   r   r   r.   rn   )flagr.   not_coverednegativeflags_to_checkr   r   r   r   r   r   r   e  s(    

r   c                 C   s   | dk rdS | dt |  kS )Nr   Fr   )r   r  r   r   r   r    s    r  )r   typesr   r   __all__r   r   r    r)   rw   r-   r	   dictr/   r   r   r   r  r   r   r   r   r   r
   r   r  r   r   r   r   <module>   s<       D   5rnA#