a
    yŒ@hš  ã                   @   sê   d dl Z d dlmZmZ g d¢Zdd„ 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S )%é    N)ÚMappingProxyTypeÚDynamicClassAttribute)ÚEnumMetaÚEnumÚIntEnumÚFlagÚIntFlagÚautoÚuniquec                 C   s   t | dƒpt | dƒpt | dƒS )z?
    Returns True if obj is a descriptor, False otherwise.
    Ú__get__Ú__set__Ú
__delete__)Úhasattr)Úobj© r   úlib/python3.9/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 )z=
    Returns 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 )z;
    Returns True if a _sunder_ name, False otherwise.
    r   r   éÿÿÿÿr   é   r   r   r   r   r   r   Ú
_is_sunder!   s    ÿþür    c                 C   sT   d| f }t |ƒdkrL| |¡rL|t |ƒ dkrL|d dksH|d dkrLdS dS d S )Nz_%s__é   r   r   r   TF)r   Ú
startswith)Úcls_namer   Úpatternr   r   r   Ú_is_private,   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_unpicklable9   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	   C   s   r	   c                       s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )Ú	_EnumDictz«
    Track 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   r6   Q   s
    
z_EnumDict.__init__c                    s�  t | j|ƒr*ddl}|jd|f tdd� t|ƒr¾|dvrBtdƒ‚|dkrf| jrXtd	ƒ‚t	| d
|ƒ nV|dkr¼t
|tƒrŠ| dd¡ ¡ }nt|ƒ}|| _t|ƒt| jƒ@ }|r¼td|f ƒ‚nÀt|ƒrÔ|dkrÒd}nª|| jv rìtd| ƒ‚n’|| jv røn†t|ƒ�s~|| v �r td|| | f ƒ‚t
|tƒ�rf|jtk�r`|  |dt| jƒ| jdd… ¡|_d| _|j}| j |¡ | j |¡ tƒ  ||¡ dS )zí
        Changes 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.
        r   Nz@private variables, such as %r, will be normal attributes in 3.10r   )Ú
stacklevel)Ú_order_Ú_create_pseudo_member_Ú_generate_next_value_Ú	_missing_Ú_ignore_z(_names_ are reserved for future Enum userA   z4_generate_next_value_ must be defined before membersÚ_generate_next_valuerC   ú,ú z-_ignore_ cannot specify already set names: %rÚ	__order__r?   zAttempted to reuse key: %rz%r already defined as: %rr   T)r%   Ú	_cls_nameÚwarningsÚwarnÚDeprecationWarningr    Ú
ValueErrorr:   r&   ÚsetattrÚ
isinstanceÚstrÚreplaceÚsplitÚlistr9   Úsetr7   r   r   r	   r3   r2   rD   r   r8   Úappendr5   Ú__setitem__)r(   Úkeyr3   rI   Úalreadyr<   r   r   rU   X   sf    	ÿü
ÿÿ



üz_EnumDict.__setitem__)r/   r,   r0   r1   r6   rU   Ú__classcell__r   r   r<   r   r4   J   s   r4   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ed'd(„ ƒZed)d*„ ƒZed+d,„ ƒZ‡  ZS )/r   z
    Metaclass for Enum
    c                 K   sD   |   ||¡ tƒ }||_|  ||¡\}}|d ur@t|dd ƒ|d< |S )NrA   )Ú_check_for_existing_membersr4   rH   Ú_get_mixins_Úgetattr)Úmetaclsr-   ÚbasesÚkwdsÚ	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 ]
}ˆ|= qnˆ dd ¡}t|ƒddh@ }|r®td d 	|¡¡ƒ‚d	ˆvr¾d
ˆd	< t
ƒ j| ||ˆfi |¤Ž}g |_i |_ˆ|_dd„ | ¡ D ƒ}i |_dˆv�rÌˆtu�rÌd}t‡fdd„|D ƒƒ�sÌdˆv �rFt|ƒ n†d }|D ]n}|jD ]P‰ ˆ tu �rl�qXn:t‡ fdd„|D ƒƒ�r�d} �qªndˆ jv �rXd} �qª�qX|d u�rN �q¾�qN|�rÌt|ƒ ˆjD �]*}|| }t|tƒ�sô|f}n|}ˆtu �r|f}|
�s*||ƒ}t|dƒ�sb||_n8||g|¢R Ž }t|dƒ�sbˆtu �rX||_n
ˆ|Ž |_|j}||_||_|j|Ž  |j ¡ D ]"\}}|j|jk�rˆ|} �q¸�qˆ|j |¡ ||v�rÎt|||ƒ ||j|< z||j|< W n t�yú   Y n0 �qÒdD ]V}|ˆv �r�qt ||ƒ}t ˆ|d ƒ}t ||d ƒ}|d u�r||u �rt|||ƒ �qt!d u�rz|	�rr||_"t!j|_|d u�r´t|t#ƒ�r | $dd¡ %¡ }||jk�r´tdƒ‚|S )NrC   c                    s   i | ]}|ˆ | “qS r   r   )Ú.0Úk)Ú	classdictr   r   Ú
<dictcomp>Å   ó    z$EnumMeta.__new__.<locals>.<dictcomp>r?   ÚmroÚ zInvalid enum member name: {0}rE   r1   zAn enumeration.c                 S   s.   h | ]&}|j  ¡ D ]\}}t|tƒr|’qqS r   )Ú__dict__ÚitemsrN   r   )rc   Úcrd   Úvr   r   r   Ú	<setcomp>Ý   s   
þz#EnumMeta.__new__.<locals>.<setcomp>r+   )Ú__getnewargs_ex__Ú__getnewargs__r+   Ú
__reduce__c                 3   s   | ]}|ˆ j v V  qd S ©N©rj   ©rc   Úm)r`   r   r   Ú	<genexpr>ô   rg   z#EnumMeta.__new__.<locals>.<genexpr>Ú__new__c                 3   s   | ]}|ˆ j v V  qd S rr   rs   rt   )Úbaser   r   rv     rg   FTÚ_value_)Ú__repr__Ú__str__Ú
__format__r+   rF   z#member order does not match _order_)&Ú
setdefaultrT   ÚpoprZ   Ú
_find_new_r7   rS   rL   ÚformatÚjoinr5   rw   Ú_member_names_Ú_member_map_Ú_member_type_rh   Ú_value2member_map_ÚobjectÚanyr.   Ú__mro__rj   rN   Útupler   ry   Ú_name_Ú__objclass__r6   rk   rM   r&   r[   r   Ú__new_member__rO   rP   rQ   )r\   r-   r]   re   r^   ÚignorerV   ra   rw   Úsave_newÚuse_argsÚenum_membersr   r?   Úinvalid_namesÚ
enum_classÚdynamic_attributesÚmethodsÚsabotageÚchainÚmember_namer3   ÚargsÚenum_memberÚcanonical_memberÚclass_methodÚ
obj_methodÚenum_methodr<   )rx   re   r`   r   rw   ³   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u 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Ÿ   )rw   Ú_create_)r-   r3   Únamesr    r¡   r¢   r£   r   r   r   Ú__call__f  s    úzEnumMeta.__call__c                 C   s:   t |tƒs$tdt|ƒj| jjf ƒ‚t || ƒo8|j| jv S )Nú3unsupported operand type(s) for 'in': '%s' and '%s')rN   r   r&   r¢   r0   r=   rŠ   rƒ   )r-   Úmemberr   r   r   Ú__contains__‹  s    
ÿÿzEnumMeta.__contains__c                    s(   || j v rtd| j ƒ‚tƒ  |¡ d S )Nz%s: cannot delete Enum member.)rƒ   ÚAttributeErrorr/   r5   Ú__delattr__)r-   Úattrr<   r   r   r«   ’  s    
zEnumMeta.__delattr__c                 C   s   g d¢| j  S )N)r=   r1   Ú__members__r,   ©r‚   r;   r   r   r   Ú__dir__™  s    ÿÿzEnumMeta.__dir__c                 C   s>   t |ƒrt|ƒ‚z| j| W S  ty8   t|ƒd‚Y n0 dS )a=  
        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ª   rƒ   ÚKeyError©r-   r   r   r   r   Ú__getattr__Ÿ  s    	zEnumMeta.__getattr__c                 C   s
   | j | S rr   ©rƒ   r±   r   r   r   Ú__getitem__¯  s    zEnumMeta.__getitem__c                    s   ‡ fdd„ˆ j D ƒS )z6
        Returns members in definition order.
        c                 3   s   | ]}ˆ j | V  qd S rr   r³   ©rc   r   ©r-   r   r   rv   ¶  rg   z$EnumMeta.__iter__.<locals>.<genexpr>r®   r¶   r   r¶   r   Ú__iter__²  s    zEnumMeta.__iter__c                 C   s
   t | jƒS rr   )r   r‚   r¶   r   r   r   Ú__len__¸  s    zEnumMeta.__len__c                 C   s
   t | jƒS )z¿
        Returns 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   rƒ   r¶   r   r   r   r­   »  s    zEnumMeta.__members__c                 C   s
   d| j  S )Nz	<enum %r>)r/   r¶   r   r   r   rz   Å  s    zEnumMeta.__repr__c                    s   ‡ fdd„t ˆ jƒD ƒS )z>
        Returns members in reverse definition order.
        c                 3   s   | ]}ˆ j | V  qd S rr   r³   rµ   r¶   r   r   rv   Ì  rg   z(EnumMeta.__reversed__.<locals>.<genexpr>)Úreversedr‚   r¶   r   r¶   r   Ú__reversed__È  s    zEnumMeta.__reversed__c                    s0   | j  di ¡}||v rtdƒ‚tƒ  ||¡ dS )a  
        Block 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.
        rƒ   zCannot reassign members.N)rj   Úgetrª   r5   Ú__setattr__)r-   r   r3   Ú
member_mapr<   r   r   r¼   Î  s    zEnumMeta.__setattr__c             
   C   sn  | j }|du r| fn|| f}|  | |¡\}	}
| ||¡}t|tƒrR| dd¡ ¡ }t|ttfƒrÂ|rÂt|d tƒrÂ|g  }}g }t	|ƒD ]8\}}|
 
||||dd… ¡}| |¡ | ||f¡ qˆ|D ].}t|tƒrä|||  }}n|\}}|||< qÆ| ||||¡}|du �r@zt d¡jd }W n tttf�y>   Y n0 |du �rTt|ƒ n||_|du�rj||_|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.
        NrE   rF   r   r   r/   )r=   rZ   rb   rN   rO   rP   rQ   r‰   rR   Ú	enumeraterA   rT   rw   ÚsysÚ	_getframeÚ	f_globalsrª   rL   r°   r.   r,   r0   )r-   Ú
class_namer¥   r    r¡   r¢   r£   r\   r]   r   ra   re   Úoriginal_namesÚlast_valuesÚcountr   r3   Úitemr—   Úmember_valuer’   r   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yl   |jdd„ d� Y n0 | |||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   )rc   r   r3   ©Úfilterr   r   Ú
<listcomp>!  s   þz&EnumMeta._convert_.<locals>.<listcomp>c                 S   s   | d | d fS )Nr   r   r   ©Útr   r   r   Ú<lambda>'  rg   z$EnumMeta._convert_.<locals>.<lambda>)rV   c                 S   s   | d S ©Nr   r   rË   r   r   r   rÍ   *  rg   )r    )
Úvarsr¿   Úmodulesrk   Úsortr&   Ú_reduce_ex_by_namer+   Úupdater­   )r-   r   r    rÉ   ÚsourceÚmodule_globalsÚmembersr   rÈ   r   Ú	_convert_  s     	

þzEnumMeta._convert_c                 C   s<   |D ]2}|j D ]&}t|tƒr|jrtd| |jf ƒ‚qqd S )Nz %s: cannot extend enumeration %r)rˆ   Ú
issubclassr   r‚   r&   r/   )rÂ   r]   r–   rx   r   r   r   rY   1  s    
ÿÿz$EnumMeta._check_for_existing_membersc                    sT   |st tfS ‡ fdd„}|d }t|tƒs2tdƒ‚||ƒp<t }|jrLtdƒ‚||fS )z¦
        Returns the type for creating enum members, and the first inherited
        enum class.

        bases: the tuple of bases that was given to __new__
        c                    s¶   t ƒ }| D ]x}d }|jD ]h}|tu r(qqt|tƒrN|jtur€| |j¡  q
qd|jv rxt|tƒrdq| |pn|¡  q
q|p~|}qq
t|ƒdkr¢t	dˆ |f ƒ‚n|r®| 
¡ S d S d S )Nrw   r   z%r: too many data types: %r)rS   rˆ   r†   rØ   r   r„   Úaddrj   r   r&   r~   )r]   Ú
data_typesr–   Ú	candidaterx   ©rÂ   r   r   Ú_find_data_typeF  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)r†   r   rØ   r&   r‚   )rÂ   r]   rÝ   ra   r`   r   rÜ   r   rZ   ;  s    
zEnumMeta._get_mixins_c           	      C   sŽ   |   dd¡}|du}|du rpdD ]H}||fD ].}t||dƒ}|ddjtjtjhvr,|} q\q,|dur  qpq tj}|tju r€d}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__
        rw   N)rŒ   rw   FT)r»   r[   rw   r†   r   )	re   r`   ra   rw   rŽ   ÚmethodÚpossibleÚtargetr�   r   r   r   r   j  s*    ü
zEnumMeta._find_new_)N)N)r/   r,   r0   r1   Úclassmethodrb   rw   rž   r¦   r©   r«   r¯   r²   r´   r·   r¸   Úpropertyr­   rz   rº   r¼   r¤   r×   ÚstaticmethodrY   rZ   r   rX   r   r   r<   r   r       s6   
 .%
	5
!
	
.r   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   zV
    Generic enumeration.

    Derive from this class to define new enumerations.
    c              
   C   s(  t |ƒ| u r|S z| j| W S  ty.   Y n8 tyd   | j ¡ D ]}|j|krD|   Y S qDY n0 zd }|  |¡}W n, ty¤ } z|}d }W Y d }~n
d }~0 0 zrt	|| ƒrÀ|W d }d }S t
d|| jf ƒ}|d u rè|d u rè|‚n|d u �rtd| j|f ƒ}||_|‚W d }d }n
d }d }0 d S )Nú%r is not a valid %szDerror in %s._missing_: returned %r instead of None or a valid member)r¢   r…   r°   r&   rƒ   Úvaluesry   rB   Ú	ExceptionrN   rL   r0   r/   Ú__context__)r-   r3   r¨   ÚexcÚresultÚeÚve_excr   r   r   rw   ž  sJ    

ñ
ÿÿÿzEnum.__new__c              	   C   s4   t |ƒD ]&}z|d W   S  ty,   Y q0 q|S )úð
        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
        r   N)r¹   r&   )r   r£   rÅ   rÄ   Ú
last_valuer   r   r   rA   Ë  s    	zEnum._generate_next_value_c                 C   s   d S rr   r   )r-   r3   r   r   r   rB   Ü  s    zEnum._missing_c                 C   s   d| j j| j| jf S )Nú<%s.%s: %r>)r=   r/   rŠ   ry   r;   r   r   r   rz   à  s    ÿzEnum.__repr__c                 C   s   d| j j| jf S )Nú%s.%s)r=   r/   rŠ   r;   r   r   r   r{   ä  s    zEnum.__str__c                    s4   ‡ fdd„ˆ j  ¡ D ƒdd„ ˆ jD ƒ }g d¢| S )z<
        Returns all members and all public methods
        c                    s2   g | ]*}|j D ]}|d  dkr|ˆ jvr|‘qqS ©r   r   )rj   rƒ   )rc   r-   ru   r;   r   r   rÊ   ë  s   
ýz Enum.__dir__.<locals>.<listcomp>c                 S   s   g | ]}|d  dkr|‘qS rð   r   rt   r   r   r   rÊ   ð  rg   )r=   r1   r,   )r=   rh   rj   )r(   Úadded_behaviorr   r;   r   r¯   ç  s    
þûzEnum.__dir__c                 C   sJ   t | ƒjtjtjfv}| jtu s$|r2t}t| ƒ}n| j}| j}| ||¡S )z\
        Returns format using actual value type unless __str__ has been overridden.
        )	r¢   r{   r   r   r„   r†   rO   ry   r|   )r(   Úformat_specÚstr_overriddenr-   Úvalr   r   r   r|   ó  s    	
zEnum.__format__c                 C   s
   t | jƒS rr   )ÚhashrŠ   r;   r   r   r   Ú__hash__  s    zEnum.__hash__c                 C   s   | j | jffS rr   ©r=   ry   r'   r   r   r   r+   	  s    zEnum.__reduce_ex__c                 C   s   | j S )zThe name of the Enum member.)rŠ   r;   r   r   r   r     s    z	Enum.namec                 C   s   | j S )zThe value of the Enum member.©ry   r;   r   r   r   r3     s    z
Enum.valueN)r/   r,   r0   r1   rw   rA   rá   rB   rz   r{   r¯   r|   rö   r+   r   r   r3   r   r   r   r   r   ˜  s   -


r   )Ú	metaclassc                   @   s   e Zd ZdZdS )r   z.Enum where members are also (and must be) intsN)r/   r,   r0   r1   r   r   r   r   r     s   r   c                 C   s   | j S rr   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 flags
    c              	   C   sb   |s|dur|S dS t |ƒD ]8}zt|ƒ}W  qVW q tyR   td| ƒd‚Y q0 qd|d  S )rì   Nr   zInvalid Flag value: %rr   )r¹   Ú	_high_bitræ   r&   )r   r£   rÅ   rÄ   rí   Úhigh_bitr   r   r   rA   *  s    	
zFlag._generate_next_value_c                 C   s.   |}|dk r| }|   |¡}|dk r*| }|S )úV
        Returns member (possibly creating it) if one can be found for value.
        r   )r@   )r-   r3   Úoriginal_valueÚpossible_memberr   r   r   rB   =  s    
zFlag._missing_c                 C   sb   | j  |d¡}|du r^t| |ƒ\}}|r:td|| jf ƒ‚t | ¡}d|_||_| j  	||¡}|S )úL
        Create a composite member iff value contains only members.
        Nrä   )
r…   r»   Ú
_decomposerL   r0   r†   rw   rŠ   ry   r}   )r-   r3   Úpseudo_memberr   Úextra_flagsr   r   r   r@   J  s    
zFlag._create_pseudo_member_c                 C   s8   t || jƒs&tdt|ƒj| jjf ƒ‚|j| j@ |jkS )zP
        Returns True if self has at least the same flags set as other.
        r§   )rN   r=   r&   r¢   r0   ry   ©r(   Úotherr   r   r   r©   ^  s    ÿÿzFlag.__contains__c                 C   sV   | j }| jd u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   ©rO   rŠ   ry   rt   r   r   r   rÊ   o  rg   z!Flag.__repr__.<locals>.<listcomp>)r=   rŠ   r/   ry   r   r�   ©r(   r-   rÖ   Ú	uncoveredr   r   r   rz   h  s    
ýzFlag.__repr__c                 C   s€   | j }| jd ur d|j| jf S t|| jƒ\}}t|ƒdkr^|d jd u 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  rt   r   r   r   rÊ   }  rg   z Flag.__str__.<locals>.<listcomp>)r=   rŠ   r/   r   ry   r   r�   r  r   r   r   r{   s  s    
þzFlag.__str__c                 C   s
   t | jƒS rr   )Úboolry   r;   r   r   r   rž   €  s    zFlag.__bool__c                 C   s"   t || jƒstS |  | j|jB ¡S rr   ©rN   r=   ÚNotImplementedry   r  r   r   r   Ú__or__ƒ  s    zFlag.__or__c                 C   s"   t || jƒstS |  | j|j@ ¡S rr   r
  r  r   r   r   Ú__and__ˆ  s    zFlag.__and__c                 C   s"   t || jƒstS |  | j|jA ¡S rr   r
  r  r   r   r   Ú__xor__�  s    zFlag.__xor__c                 C   sN   t | j| jƒ\}}|  d¡}| jD ] }||vr"|j| j@ s"||B }q"|  |¡S rÎ   )r   r=   ry   )r(   rÖ   r  Úinvertedru   r   r   r   Ú
__invert__’  s    


zFlag.__invert__N)r/   r,   r0   r1   rA   rá   rB   r@   r©   rz   r{   rž   r  r  r  r  r   r   r   r   r   %  s   


r   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 Flags
    c                 C   s*   t |tƒstd|| jf ƒ‚|  |¡}|S )rü   rä   )rN   ÚintrL   r0   r@   )r-   r3   Ú
new_memberr   r   r   rB      s    

zIntFlag._missing_c                 C   s¬   | j  |d¡}|du r¨|g}t| |ƒ\}}|rtt|ƒ}d| }|| j vrZ||vrZ| |¡ || krjd}q*||N }q*t|ƒD ]*}t | |¡}d|_||_	| j  
||¡}q||S )rÿ   Nr   r   )r…   r»   r   rú   rT   r¹   r  rw   rŠ   ry   r}   )r-   r3   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 rr   ©rN   r=   r  r  ry   )r(   r  ré   r   r   r   r  Ë  s    zIntFlag.__or__c                 C   s,   t || jtfƒstS |  | j|  |¡j@ ¡S rr   r  r  r   r   r   r  Ñ  s    zIntFlag.__and__c                 C   s,   t || jtfƒstS |  | j|  |¡jA ¡S rr   r  r  r   r   r   r  Ö  s    zIntFlag.__xor__c                 C   s   |   | j ¡}|S rr   r÷   )r(   ré   r   r   r   r  ß  s    zIntFlag.__invert__N)r/   r,   r0   r1   rá   rB   r@   r  r  r  Ú__ror__Ú__rand__Ú__rxor__r  r   r   r   r   r   ›  s   
	
 r   c                 C   s   |   ¡ d S )zJ
    returns index of highest bit, or -1 if value is zero or negative
    r   )Ú
bit_length)r3   r   r   r   rú   ä  s    rú   c                 C   s^   g }| j  ¡ D ]"\}}||jkr| ||jf¡ q|rZd dd„ |D ƒ¡}td| |f ƒ‚| S )zI
    Class decorator for enumerations ensuring unique member values.
    z, c                 S   s   g | ]\}}d ||f ‘qS )z%s -> %sr   )rc   Úaliasr   r   r   r   rÊ   ô  rg   zunique.<locals>.<listcomp>z duplicate values found in %r: %s)r­   rk   r   rT   r�   rL   )ÚenumerationÚ
duplicatesr   r¨   Úalias_detailsr   r   r   r
   ê  s    
ÿÿr
   c           	      C   sè   |}|dk }g }| D ].}|j }|r||@ |kr| |¡ || M }q|sŒ|}|rŒdt|ƒ }|| jv r€| | j| ¡ || M }|| M }qL|sª|| jv 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   r   c                 S   s   | j S rr   rø   )ru   r   r   r   rÍ     rg   z_decompose.<locals>.<lambda>T)rV   Úreverser   )r3   rT   rú   r…   rÑ   r   r~   )	Úflagr3   Únot_coveredÚnegativerÖ   r¨   rÇ   Útmpr  r   r   r   r   ù  s,    



r   )r¿   Útypesr   r   Ú__all__r   r   r    r%   r.   r†   r2   r	   Údictr4   r   r¢   r   r  r   rÒ   r   r   rú   r
   r   r   r   r   r   Ú<module>   s0   
	T   { vI