3
rY]F                 @   s>  d dl Z d dlmZmZ d dlmZ d dlmZ yd dl	m
Z
 W n  ek
r`   d dlm
Z
 Y nX 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)reduce)or_)OrderedDict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   lib/python3.6/enum.py_is_descriptor   s    

r   c             C   sT   | dd | dd   ko"dkn  oR| dd dkoR| dd	 dkoRt | dkS )
z3Returns True if a __dunder__ name, False otherwise.N   __   _   r   )len)namer   r   r   
_is_dunder   s    (r   c             C   sL   | d | d   kodkn  oJ| dd dkoJ| dd dkoJt | dkS )z1Returns True if a _sunder_ name, False otherwise.r      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   3   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  j  g | _g | _d S )N)super__init___member_names_last_values)r$   )	__class__r   r   r2   A   s    
z_EnumDict.__init__c                s   t |r.|dkrtd|dkrt| d| nt|rD|dkrd}n|| jkr\td| nxt|s|| krtd	|| | f t|tr|j	t
kr| j|d
t| j| jdd |_	|j	}| jj| | jj| t j|| 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_z(_names_ are reserved for future Enum use_generate_next_value	__order__zAttempted to reuse key: %rz%r already defined as: %rr    N)r6   r7   r8   r9   )r"   
ValueErrorsetattrr   r3   r#   r   
isinstancer   r/   r.   r:   r   r4   appendr1   __setitem__)r$   keyr/   )r5   r   r   r@   F   s,    	 


"z_EnumDict.__setitem__)r+   r(   r,   r-   r2   r@   __classcell__r   r   )r5   r   r0   :   s   r0   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ed%d& Zed'd( Z  ZS )*r   zMetaclass for Enumc             C   s0   t  }| j|\}}|d k	r,t|dd |d< |S )Nr8   )r0   _get_mixins_getattr)metaclsr)   bases	enum_dictmember_type
first_enumr   r   r   __prepare__r   s
    zEnumMeta.__prepare__c                s  | j |\}| j |\}}} fdd jD }x jD ]
}	 |	= q>W  jdd }
t|dh@ }|r~tdjdj|d krd d< t j	| || }g |_
t |_|_d	d
 |j D }i |_d krtk	rd}tfdd|D st| x6 jD ]*}|| }t|ts0|f}n|}tkrD|f}|sf||}t|ds||_n6||f| }t|dstkr||_n
| |_|j}||_||_|j|  x8|jj D ]\}	}|j|jkr|}P qW |j
j| ||krt||| ||j|< y||j|< W n tk
r6   Y nX qW xPdD ]H}	t||	}t|	d }t||	d }|d k	rD||krDt||	| qDW td k	r|r||_ tj	|_	|
d k	rt|
t!r|
j"ddj# }
|
|j
krtd|S )Nc                s   i | ]} | |qS r   r   ).0k)	classdictr   r   
<dictcomp>   s    z$EnumMeta.__new__.<locals>.<dictcomp>r6   mrozInvalid enum member name: {0},r-   zAn enumeration.c             S   s.   h | ]&}|j j D ]\}}t|tr|qqS r   )__dict__itemsr>   r   )rK   crL   vr   r   r   	<setcomp>   s    z#EnumMeta.__new__.<locals>.<setcomp>r'   __getnewargs_ex____getnewargs__
__reduce__c             3   s   | ]}| j kV  qd S )N)rQ   )rK   m)rH   r   r   	<genexpr>   s    z#EnumMeta.__new__.<locals>.<genexpr>_value___repr____str__
__format__ z#member order does not match _order_)rV   rW   r'   rX   )r\   r]   r^   r'   )$rC   
_find_new_r3   popsetr<   formatjoinr1   __new___member_names_r   _member_map__member_type_rO   _value2member_map_objectanyr*   r>   tupler   r[   _name___objclass__r2   rR   r?   r=   r#   rD   r   __new_member__strreplacesplit)rE   r)   rF   rM   rI   re   save_newuse_argsenum_membersr   r6   invalid_names
enum_classdynamic_attributesmethodsmember_namer/   argsenum_membercanonical_memberclass_method
obj_methodenum_method)r5   )rM   rH   r   re   |   s    


 









zEnumMeta.__new__c             C   s   dS )z6
        classes/types should always be True.
        Tr   )r$   r   r   r   __bool__  s    zEnumMeta.__bool__Nr    )modulequalnametypestartc            C   s*   |dkr| j | |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.

        N)r   r   r   r   )re   _create_)r)   r/   namesr   r   r   r   r   r   r   __call__  s    zEnumMeta.__call__c             C   s   t || o|j| jkS )N)r>   rm   rg   )r)   memberr   r   r   __contains__)  s    zEnumMeta.__contains__c                s(   || j krtd| j t j| d S )Nz%s: cannot delete Enum member.)rg   AttributeErrorr+   r1   __delattr__)r)   attr)r5   r   r   r   ,  s    
zEnumMeta.__delattr__c             C   s   ddddg| j  S )Nr5   r-   __members__r(   )rf   )r$   r   r   r   __dir__4  s    
zEnumMeta.__dir__c             C   s>   t |rt|y
| j| S  tk
r8   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   rg   KeyError)r)   r   r   r   r   __getattr__8  s    	
zEnumMeta.__getattr__c             C   s
   | j | S )N)rg   )r)   r   r   r   r   __getitem__H  s    zEnumMeta.__getitem__c                s    fdd j D S )Nc             3   s   | ]} j | V  qd S )N)rg   )rK   r   )r)   r   r   rZ   L  s    z$EnumMeta.__iter__.<locals>.<genexpr>)rf   )r)   r   )r)   r   __iter__K  s    zEnumMeta.__iter__c             C   s
   t | jS )N)r   rf   )r)   r   r   r   __len__N  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   rg   )r)   r   r   r   r   Q  s    zEnumMeta.__members__c             C   s
   d| j  S )Nz	<enum %r>)r+   )r)   r   r   r   r\   [  s    zEnumMeta.__repr__c                s    fddt  jD S )Nc             3   s   | ]} j | V  qd S )N)rg   )rK   r   )r)   r   r   rZ   _  s    z(EnumMeta.__reversed__.<locals>.<genexpr>)reversedrf   )r)   r   )r)   r   __reversed__^  s    zEnumMeta.__reversed__c                s0   | j jdi }||krtdt j|| 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.

        rg   zCannot reassign members.N)rQ   getr   r1   __setattr__)r)   r   r/   
member_map)r5   r   r   r   a  s    zEnumMeta.__setattr__c            C   s  | j }|dkr| fn|| f}| j|\}	}
|j||}t|trP|jddj }t|ttfr|rt|d tr|g  }}g }xDt	|D ]8\}}|
j
||||dd }|j| |j||f qW x6|D ].}t|tr|||  }}n|\}}|||< qW |j||||}|dkrTytjdjd }W n( ttfk
rR } zW Y dd}~X nX |dkrht| n||_|dk	r~||_|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.

        NrP   r_   r   r   r+   )r5   rC   rJ   r>   rp   rq   rr   rl   list	enumerater8   r?   re   sys	_getframe	f_globalsr   r<   r*   r(   r,   )r)   
class_namer   r   r   r   r   rE   rF   r   rI   rM   original_nameslast_valuescountr   r/   itemrz   member_valuerw   excr   r   r   r   n  s<    
 







zEnumMeta._create_c             C   s   | st tfS d }}x,| D ]$}|tk	rt|tr|jrtdqW t|tsTtdt| d tst| d }| d }n8x6| d jD ](}t|tr|dkr|}q|dkr|}qW ||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__

        NzCannot extend enumerationszHnew enumerations must be created as `ClassName([mixin_type,] enum_type)`r   r    r!   )rj   r   
issubclassrf   r#   __mro__)rF   rH   rI   baser   r   r   rC     s(    




zEnumMeta._get_mixins_c       	      C   s   | j dd}|dk	}|dkrtxVdD ]H}x8||fD ],}t||d}|ddjtjtjhkr0|}P q0W |dk	r"P q"W 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__

        re   Nro   FT)ro   re   )r   rD   re   rj   r   )	rM   rH   rI   re   rs   methodpossibletargetrt   r   r   r   r`     s(    


zEnumMeta._find_new_)N)r+   r(   r,   r-   classmethodrJ   re   r   r   r   r   r   r   r   r   r   propertyr   r\   r   r   r   staticmethodrC   r`   rB   r   r   )r5   r   r   p   s(   
 

5-c               @   s   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edddZdS )r   zRGeneric enumeration.

    Derive from this class to define new enumerations.

    c             C   sj   t || kr|S y|| jkr&| j| S W n6 tk
r^   x | jj D ]}|j|krD|S qDW Y nX | j|S )N)r   ri   r#   rg   valuesr[   r9   )r)   r/   r   r   r   r   re     s    

zEnum.__new__c             C   s8   x2t |D ]"}y|d S  tk
r*   Y q
X q
W |S d S )Nr    )r   r#   )r   r   r   r   
last_valuer   r   r   r8     s    
zEnum._generate_next_value_c             C   s   t d|| jf d S )Nz%r is not a valid %s)r<   r+   )r)   r/   r   r   r   r9   "  s    zEnum._missing_c             C   s   d| j j| j| jf S )Nz<%s.%s: %r>)r5   r+   rm   r[   )r$   r   r   r   r\   &  s    zEnum.__repr__c             C   s   d| j j| jf S )Nz%s.%s)r5   r+   rm   )r$   r   r   r   r]   *  s    zEnum.__str__c                s&    fdd j j D }dddg| S )Nc                s2   g | ]*}|j D ]}|d  dkr| jkr|qqS )r   r   )rQ   rg   )rK   r)   rY   )r$   r   r   
<listcomp>/  s   
z Enum.__dir__.<locals>.<listcomp>r5   r-   r(   )r5   rO   )r$   added_behaviorr   )r$   r   r   -  s    
zEnum.__dir__c             C   s0   | j tkrt}t| }n| j }| j}|j||S )N)rh   rj   rp   r[   r^   )r$   format_specr)   valr   r   r   r^   6  s    

zEnum.__format__c             C   s
   t | jS )N)hashrm   )r$   r   r   r   __hash__E  s    zEnum.__hash__c             C   s   | j | jffS )N)r5   r[   )r$   r%   r   r   r   r'   H  s    zEnum.__reduce_ex__c             C   s   | j S )zThe name of the Enum member.)rm   )r$   r   r   r   r   R  s    z	Enum.namec             C   s   | j S )zThe value of the Enum member.)r[   )r$   r   r   r   r/   W  s    z
Enum.valueNc                s   t tj| }rt n| fddj D }y|jdd d W n$ tk
rp   |jdd d Y nX | |||d} t| _|j| j	 | ||< | S )z[
        Create a new Enum subclass that replaces a collection of global constants
        c                s    g | ]} |r|| fqS r   r   )rK   r   )filtersourcer   r   r   q  s   z!Enum._convert.<locals>.<listcomp>c             S   s   | d | d fS )Nr    r   r   )tr   r   r   <lambda>v  s    zEnum._convert.<locals>.<lambda>)rA   c             S   s   | d S )Nr   r   )r   r   r   r   r   y  s    )r   )
varsr   moduleskeyssortr#   _reduce_ex_by_namer'   updater   )r)   r   r   r   r   module_globalsmembersr   )r   r   r   _convert\  s    

zEnum._convert)N)r+   r(   r,   r-   re   r8   r   r9   r\   r]   r   r^   r   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 )N)r   )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 xBt |D ]6}yt|}P W q tk
rR   td| dY qX qW d|d  S )z
        Generate the next value when not given.

        name: the name of the member
        start: the initital 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_bit	Exceptionr#   )r   r   r   r   r   high_bitr   r   r   r8     s    	zFlag._generate_next_value_c             C   s.   |}|dk r| }| j |}|dk r*| }|S )Nr   )r7   )r)   r/   original_valuepossible_memberr   r   r   r9     s    
zFlag._missing_c             C   sb   | j j|d}|dkr^t| |\}}|r:td|| jf tj| }d|_||_| j j	||}|S )zL
        Create a composite member iff value contains only members.
        Nz%r is not a valid %s)
ri   r   
_decomposer<   r+   rj   re   rm   r[   
setdefault)r)   r/   pseudo_memberr   extra_flagsr   r   r   r7     s    
zFlag._create_pseudo_member_c             C   s"   t || jstS |j| j@ |jkS )N)r>   r5   NotImplementedr[   )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jdd |D | jf S )Nz<%s.%s: %r>|c             S   s   g | ]}t |jp|jqS r   )rp   rm   r[   )rK   rY   r   r   r   r     s    z!Flag.__repr__.<locals>.<listcomp>)r5   rm   r+   r[   r   rd   )r$   r)   r   	uncoveredr   r   r   r\     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jdd |D f S d S )Nz%s.%sr    r   z%s.%rr   c             S   s   g | ]}t |jp|jqS r   )rp   rm   r[   )rK   rY   r   r   r   r     s    z Flag.__str__.<locals>.<listcomp>)r5   rm   r+   r   r[   r   rd   )r$   r)   r   r   r   r   r   r]     s    
zFlag.__str__c             C   s
   t | jS )N)boolr[   )r$   r   r   r   r     s    zFlag.__bool__c             C   s"   t || jstS | j| j|jB S )N)r>   r5   r   r[   )r$   r   r   r   r   __or__  s    zFlag.__or__c             C   s"   t || jstS | j| j|j@ S )N)r>   r5   r   r[   )r$   r   r   r   r   __and__  s    zFlag.__and__c             C   s"   t || jstS | j| j|jA S )N)r>   r5   r   r[   )r$   r   r   r   r   __xor__  s    zFlag.__xor__c                sD   t jj\ } fddjD }tt|jd}j|S )Nc                s&   g | ]}| kr|j j @  r|qS r   )r[   )rK   rY   )r   r$   r   r   r     s    z#Flag.__invert__.<locals>.<listcomp>r   )r   r5   r[   r   _or_)r$   r   inverted_membersinvertedr   )r   r$   r   
__invert__  s    zFlag.__invert__N)r+   r(   r,   r-   r8   r   r9   r7   r   r\   r]   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 | j|}|S )Nz%r is not a valid %s)r>   intr<   r+   r7   )r)   r/   
new_memberr   r   r   r9     s    

zIntFlag._missing_c             C   s   | j j|d }|d kr|g}t| |\}}xL|rvt|}d| }|| j kr\||kr\|j| || krld}q,||N }q,W x6t|D ]*}tj| |}d |_||_	| j j
||}qW |S )Nr   r   )ri   r   r   r   r?   r   r   re   rm   r[   r   )r)   r/   r   need_to_creater   r   bit
flag_valuer   r   r   r7     s&    


zIntFlag._create_pseudo_member_c             C   s0   t || jtfstS | j| j| j|jB }|S )N)r>   r5   r   r   r[   )r$   r   resultr   r   r   r     s    zIntFlag.__or__c             C   s,   t || jtfstS | j| j| j|j@ S )N)r>   r5   r   r   r[   )r$   r   r   r   r   r   #  s    zIntFlag.__and__c             C   s,   t || jtfstS | j| j| j|jA S )N)r>   r5   r   r   r[   )r$   r   r   r   r   r   (  s    zIntFlag.__xor__c             C   s   | j | j }|S )N)r5   r[   )r$   r   r   r   r   r   1  s    zIntFlag.__invert__N)r+   r(   r,   r-   r   r9   r7   r   r   r   __ror____rand____rxor__r   r   r   r   r   r     s   c             C   s   | j  d S )z@returns index of highest bit, or -1 if value is zero or negativer    )
bit_length)r/   r   r   r   r   6  s    r   c             C   sb   g }x0| j j D ]"\}}||jkr|j||jf qW |r^dj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   )rK   aliasr   r   r   r   r   B  s    zunique.<locals>.<listcomp>z duplicate values found in %r: %s)r   rR   r   r?   rd   r<   )enumeration
duplicatesr   r   alias_detailsr   r   r   r   :  s    
c             C   s   |}|dk }|r*dd t | jj D }ndd t | jj D }g }x4|D ],\}}|rL||@ |krL|j| || M }qLW | r|| jkr|j| j|  |jdd dd t|d	kr|d j|kr|jd ||fS )
z#Extract all members from the value.r   c             S   s"   g | ]\}}|j d k	r||fqS )N)r   )rK   rT   rY   r   r   r   r   R  s   z_decompose.<locals>.<listcomp>c             S   s*   g | ]"\}}|j d k	st|r||fqS )N)r   _power_of_two)rK   rT   rY   r   r   r   r   Y  s   c             S   s   | j S )N)r[   )rY   r   r   r   r   d  s    z_decompose.<locals>.<lambda>T)rA   reverser    )r   ri   rR   r?   r   r   r/   ra   )flagr/   not_coverednegativeflags_to_checkr   r   r   r   r   r   r   G  s$    

r   c             C   s   | dk rdS | dt |  kS )Nr    Fr   )r   )r/   r   r   r   r   j  s    r   )#r   typesr   r   	functoolsr   operatorr   r   _collectionsr   ImportErrorcollections__all__r   r   r"   r*   rj   r.   r   dictr0   r   r   r   r   r	   r   r
   r   r   r   r   r   r   r   r   r   <module>   s@   3    mA#