3 ^Fc@s>ddlZddlmZmZddlmZddlmZyddl m Z Wn e k r`ddl m Z YnXdddd d d d gZ d dZddZddZddZeZGdd d ZGdddeZdZGdddeZGdddedZGdddeeZddZGdd d eZGdd d eeZd d!Zd"d Z d#d$Z!d%d&Z"dS)'N)MappingProxyTypeDynamicClassAttribute)reduce)or_) OrderedDictEnumMetaEnumIntEnumFlagIntFlagautouniquecCst|dpt|dpt|dS)z5Returns True if obj is a descriptor, False otherwise.Z__get__Z__set__Z __delete__)hasattr)Zobjr/usr/lib64/python3.6/enum.py_is_descriptors  rcCsT|dd|ddko"dknoR|dddkoR|dd dkoRt|dkS) z3Returns True if a __dunder__ name, False otherwise.NZ__i_iirlennamerrr _is_dunders(rcCsL|d|dkodknoJ|dddkoJ|dddkoJt|dkS)z1Returns True if a _sunder_ name, False otherwise.rrrrrrrrrr _is_sunder$s rcCsdd}||_d|_dS)z"Make the given class un-picklable.cSstd|dS)Nz%r cannot be pickled) TypeErrorselfZprotorrr_break_on_call_reduce-z6_make_class_unpicklable.._break_on_call_reducez N) __reduce_ex__ __module__)clsrrrr_make_class_unpicklable+sr$c@seZdZdZeZdS)r zP Instances are replaced with an appropriate value in Enum class suites. N)__name__r" __qualname____doc__ _auto_nullvaluerrrrr 3scs,eZdZdZfddZfddZZS) _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. cstjg|_g|_dSN)super__init__ _member_names _last_valuesr __class__rrr-As z_EnumDict.__init__cst|r.|d krtd|dkrt|d|nt|rD|dkrd}n||jkr\td|nxt|s||krtd |||ft|tr|j t kr|j |d t |j|j d d |_ |j }|jj||j j|tj||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_valueZ __order__zAttempted to reuse key: %rz%r already defined as: %rrN)r3r4r5r6)r ValueErrorsetattrrr.rr isinstancer r)r(r7rr/appendr, __setitem__)rkeyr)r1rrr<Fs,    "  z_EnumDict.__setitem__)r%r"r&r'r-r< __classcell__rrr1rr*:s r*cseZdZdZeddZfddZddZd)dddd d d d Zd dZ fddZ ddZ ddZ ddZ ddZddZeddZddZdd Zfd!d"Zdddd d d#d$Zed%d&Zed'd(ZZS)*rzMetaclass for EnumcCs0t}|j|\}}|dk r,t|dd|d<|S)Nr5)r* _get_mixins_getattr)metaclsr#basesZ enum_dict member_type first_enumrrr __prepare__rs zEnumMeta.__prepare__c s|j|\}|j|\}}}fddjD}xjD] } | =q>Wjdd} t|dh@} | r~tdjdj| dkrdd<tj |||} g| _ t | _ | _ d d | jD} i| _d krtk rd}tfdd|Dst| x6jD]*}||}t|ts0|f}n|}tkrD|f}|sf|| }t|ds||_n6|| f|}t|dstkr||_n ||_|j}||_| |_|j|x8| j jD]\} }|j|jkr|}PqW| j j||| krt| |||| j |<y|| j|<Wntk r6YnXqWxPdD]H} t| | }t| d}t|| d}|dk rD||krDt| | |qDWtdk r|r|| _ tj | _ | dk rt| t!r| j"ddj#} | | j krtd| S)Ncsi|]}||qSrr).0k) classdictrrz z$EnumMeta.__new__..r3mrozInvalid enum member name: {0},r'zAn enumeration.cSs.h|]&}|jjD]\}}t|tr|qqSr)__dict__itemsr:r)rFZcrGvrrrz sz#EnumMeta.__new__..r!__getnewargs_ex____getnewargs__ __reduce__c3s|]}|jkVqdSr+)rLrFm)rCrr z#EnumMeta.__new__.._value___repr____str__ __format__ z#member order does not match _order_)rOrPr!rQ)rWrXrYr!)$r? _find_new_r.popZsetr8Zformatjoinr,__new___member_names_r _member_map_ _member_type_rJ_value2member_map_objectZanyr$r:tupler rV_name_Z __objclass__r-rMr;r9rr@r__new_member__strreplacesplit)rAr#rBrHrDr^save_newuse_argsZ enum_membersrr3Z invalid_names enum_classZdynamic_attributesZmethods member_namer)ZargsZ enum_memberZcanonical_memberZ class_methodZ obj_methodZ enum_methodr1)rHrCrr^|s                          zEnumMeta.__new__cCsdS)z6 classes/types should always be True. Trr0rrr__bool__szEnumMeta.__bool__NrmodulequalnametypestartcCs*|dkr|j||S|j||||||dS)aEither 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. Nro)r^_create_)r#r)namesrprqrrrsrrr__call__ s zEnumMeta.__call__cCst||o|j|jkSr+)r:rer`)r#memberrrr __contains__)r zEnumMeta.__contains__cs(||jkrtd|jtj|dS)Nz%s: cannot delete Enum member.)r`AttributeErrorr%r, __delattr__)r#Zattrr1rrrz,s  zEnumMeta.__delattr__cCsddddg|jS)Nr2r' __members__r"r_r0rrr__dir__4s zEnumMeta.__dir__c Cs>t|rt|y |j|Stk r8t|dYnXdS)a5Return 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)rryr`ZKeyErrorr#rrrr __getattr__8s  zEnumMeta.__getattr__cCs |j|Sr+r`r~rrr __getitem__Hr zEnumMeta.__getitem__csfddjDS)Nc3|]}j|VqdSr+rrFrr#rrrTLrUz$EnumMeta.__iter__..r|rrrr__iter__Kr zEnumMeta.__iter__cC t|jSr+)rr_rrrr__len__Nr zEnumMeta.__len__cCr)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. )rr`rrrrr{QszEnumMeta.__members__cCs d|jS)Nz )r%rrrrrW[r zEnumMeta.__repr__csfddtjDS)Nc3rr+rrrrrrT_rUz(EnumMeta.__reversed__..)reversedr_rrrr __reversed__^r zEnumMeta.__reversed__cs0|jjdi}||krtdtj||dS)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. r`zCannot reassign members.N)rLgetryr, __setattr__)r#rr)Z member_mapr1rrraszEnumMeta.__setattr__cCs|j}|dkr|fn||f}|j|\} } |j||} t|trP|jddj}t|ttfr|rt|dtr|g} }g} xDt | D]8\}}| j |||| dd}| j ||j ||fqWx6|D].}t|tr|||}}n|\}}|| |<qW|j |||| }|dkrTyt jdjd}Wn(ttfk rR}zWYdd}~XnX|dkrht|n||_|dk r~||_|S)aConvenience 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. NrKrZrrr%)r2r?rEr:rgrhrirdlistZ enumerater5r;r^sysZ _getframeZ f_globalsryr8r$r"r&)r#Z class_namerurprqrrrsrArBrrDrHZoriginal_names last_valuescountrr)Zitemrm member_valuerlZexcrrrrtns<             zEnumMeta._create_cCs|s ttfSd}}x,|D]$}|tk rt|tr|jrtdqWt|tsTtdt|dtst|d}|d}n8x6|djD](}t|tr|dkr|}q|dkr|}qW||fS)zReturns the type for creating enum members, and the first inherited enum class. bases: the tuple of bases that was given to __new__ NzCannot extend enumerationszHnew enumerations must be created as `ClassName([mixin_type,] enum_type)`rrr)rcrZ issubclassr_rZ__mro__)rBrCrDZbaserrrr?s(      zEnumMeta._get_mixins_c Cs|jdd}|dk }|dkrtxVdD]H}x8||fD],}t||d}|ddjtjtjhkr0|}Pq0W|dk r"Pq"Wtj}|tjkrd}nd}|||fS)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__ r^NrfFT)rfr^)rr@r^rcr) rHrCrDr^rjZmethodZpossibleZtargetrkrrrr[s(     zEnumMeta._find_new_r+)r%r"r&r' classmethodrEr^rnrvrxrzr}rrrrZpropertyr{rWrrrtZ staticmethodr?r[r>rrr1rrps(     5 -c@seZdZdZddZddZeddZdd Zd d Z d d Z ddZ ddZ ddZ eddZeddZedddZdS)rzRGeneric enumeration. Derive from this class to define new enumerations. c Csjt||kr|Sy||jkr&|j|SWn6tk r^x |jjD]}|j|krD|SqDWYnX|j|Sr+)rrrbrr`ZvaluesrVr6)r#r)rwrrrr^s   z Enum.__new__c Cs8x2t|D]"}y|dStk r*Yq Xq W|SdS)Nr)rr)rrsrr last_valuerrrr5s  zEnum._generate_next_value_cCstd||jfdSN%r is not a valid %s)r8r%)r#r)rrrr6"zEnum._missing_cCsd|jj|j|jfS)N <%s.%s: %r>)r2r%rerVr0rrrrW&sz Enum.__repr__cCsd|jj|jfS)N%s.%s)r2r%rer0rrrrX*r z Enum.__str__cs&fddjjD}dddg|S)Ncs2g|]*}|jD]}|ddkr|jkr|qqS)rr)rLr`)rFr#rSr0rr /s z Enum.__dir__..r2r'r")r2rJ)rZadded_behaviorrr0rr}-s z Enum.__dir__cCs0|jtkrt}t|}n |j}|j}|j||Sr+)rarcrgrVrY)rZ format_specr#ZvalrrrrY6s   zEnum.__format__cCrr+)Zhashrer0rrr__hash__Er z Enum.__hash__cCs|j|jffSr+r2rVrrrrr!Hr zEnum.__reduce_ex__cC|jS)zThe name of the Enum member.)rer0rrrrRz Enum.namecCr)zThe value of the Enum member.rVr0rrrr)Wrz Enum.valueNc sttj|}rtn|fddjD}y|jdddWn$tk rp|jdddYnX||||d}t|_|j|j |||<|S)z[ Create a new Enum subclass that replaces a collection of global constants cs g|]}|r||fqSrrrfiltersourcerrrqsz!Enum._convert..cSs|d|dfS)NrrrZtrrrvzEnum._convert..)r=cSs|dSNrrrrrrryr)rp) ZvarsrZmodulesZkeyssortr_reduce_ex_by_namer!Zupdater{)r#rrprrZmodule_globalsmembersrrr_convert\s     z Enum._convertr+)r%r"r&r'r^r5rr6rWrXr}rYrr!rrr)rrrrrrs    )Z metaclassc@seZdZdZdS)rz.Enum where members are also (and must be) intsN)r%r"r&r'rrrrrscCrr+rrrrrrr rc@speZdZdZddZeddZeddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZdS)r zSupport for flagsc Csd|s|dk r|SdSxBt|D]6}yt|}PWqtk rRtd|dYqXqWd|dS)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 NrzInvalid Flag value: %rr)r _high_bitZ Exceptionr)rrsrrrZhigh_bitrrrr5s zFlag._generate_next_value_cCs.|}|dkr|}|j|}|dkr*|}|Sr)r4)r#r)Zoriginal_valueZpossible_memberrrrr6s zFlag._missing_cCsb|jj|d}|dkr^t||\}}|r:td||jftj|}d|_||_|jj ||}|S)zL Create a composite member iff value contains only members. Nr) rbr _decomposer8r%rcr^rerV setdefault)r#r) pseudo_memberr extra_flagsrrrr4s zFlag._create_pseudo_member_cCs"t||jstS|j|j@|jkSr+r:r2NotImplementedrVrotherrrrrx zFlag.__contains__cCsV|j}|jdk r$d|j|j|jfSt||j\}}d|jdjdd|D|jfS)Nr|cSg|]}t|jp|jqSrrgrerVrRrrrrrIz!Flag.__repr__..)r2rer%rVrr]rr#r uncoveredrrrrWs z Flag.__repr__cCs|j}|jdk r d|j|jfSt||j\}}t|dkr^|djdkr^d|j|djfSd|jdjdd|DfSdS)Nrrrz%s.%rrcSrrrrRrrrrrIz Flag.__str__..)r2rer%rrVrr]rrrrrXs z Flag.__str__cCrr+)ZboolrVr0rrrrnr z Flag.__bool__cCs"t||jstS|j|j|jBSr+rrrrr__or__rz Flag.__or__cCs"t||jstS|j|j|j@Sr+rrrrr__and__rz Flag.__and__cCs"t||jstS|j|j|jASr+rrrrr__xor__rz Flag.__xor__csDtjj\}fddjD}tt|jd}j|S)Ncs&g|]}|kr|jj@ r|qSrrrRrrrrrsz#Flag.__invert__..r)rr2rVr_or_)rrZinverted_membersZinvertedrrr __invert__szFlag.__invert__N)r%r"r&r'r5rr6r4rxrWrXrnrrrrrrrrr s   c@sTeZdZdZeddZeddZddZdd Zd d Z eZ eZ e Z d d Z dS)r zSupport for integer-based FlagscCs*t|tstd||jf|j|}|Sr)r:intr8r%r4)r#r)Z new_memberrrrr6s  zIntFlag._missing_cCs|jj|d}|dkr|g}t||\}}xL|rvt|}d|}||jkr\||kr\|j||| krld}q,||N}q,Wx6t|D]*}tj||}d|_||_ |jj ||}qW|S)Nrr) rbrrrr;rrr^rerVr)r#r)rZneed_to_createrrZbitZ flag_valuerrrr4s&     zIntFlag._create_pseudo_member_cCs0t||jtfstS|j|j|j|jB}|Sr+r:r2rrrV)rrresultrrrrszIntFlag.__or__cCs,t||jtfstS|j|j|j|j@Sr+rrrrrr#zIntFlag.__and__cCs,t||jtfstS|j|j|j|jASr+rrrrrr(rzIntFlag.__xor__cCs|j|j}|Sr+r)rrrrrr1szIntFlag.__invert__N)r%r"r&r'rr6r4rrrZ__ror__Z__rand__Z__rxor__rrrrrr s  cCs |jdS)z@returns index of highest bit, or -1 if value is zero or negativer)Z bit_lengthr)rrrr6rrcCsbg}x0|jjD]"\}}||jkr|j||jfqW|r^djdd|D}td||f|S)z?Class decorator for enumerations ensuring unique member values.z, cSsg|]\}}d||fqS)z%s -> %sr)rFZaliasrrrrrBrIzunique..z duplicate values found in %r: %s)r{rMrr;r]r8)Z enumerationZ duplicatesrrwZ alias_detailsrrrr :s  cCs|}|dk}|r*ddt|jjD}nddt|jjD}g}x4|D],\}}|rL||@|krL|j|||M}qLW| r||jkr|j|j||jddddt|d kr|dj|kr|jd||fS) z#Extract all members from the value.rcSs"g|]\}}|jdk r||fqSr+rrFrNrSrrrrRz_decompose..cSs*g|]"\}}|jdk st|r||fqSr+)r _power_of_tworrrrrYrcSrr+r)rSrrrrdrz_decompose..T)r=Zreverser)rrbrMr;rrr)r\)Zflagr)Z not_coveredZnegativeZflags_to_checkrrwrrrrrGs$  rcCs|dkr dS|dt|kS)NrFr)rrrrrrjsr)#rZtypesrrZ functoolsrZoperatorrrZ _collectionsrZ ImportErrorZ collectionsZ__all__rrrr$rcr(r Zdictr*rrrrrrrr r rr rrrrrrZs@  3mA #