B \]ڇ@s&ddlZddlmZmZyddlmZWn ek rHddlmZYnXdddddd d gZd d Z d dZ ddZ ddZ e ZGdd d ZGdddeZdZGdddeZGdddedZGdddeeZddZGdddeZGdddeeZddZd d Zd!d"Zd#d$ZdS)%N)MappingProxyTypeDynamicClassAttribute) OrderedDictEnumMetaEnumIntEnumFlagIntFlagautouniquecCst|dpt|dpt|dS)z5Returns True if obj is a descriptor, False otherwise.__get____set__ __delete__)hasattr)objr /usr/local/lib/python3.7/enum.py_is_descriptors  rcCsLt|dkoJ|dd|ddko.dknoJ|ddkoJ|ddkS)z3Returns True if a __dunder__ name, False otherwise.NZ___)len)namerrr _is_dunders ( rcCsLt|dkoJ|d|dko&dknoJ|dddkoJ|dddkS)z1Returns True if a _sunder_ name, False otherwise.rrrr)r)rrrr _is_sunder"s  rcCsdd}||_d|_dS)z"Make the given class un-picklable.cSstd|dS)Nz%r cannot be pickled) TypeError)selfprotorrr_break_on_call_reduce,sz6_make_class_unpicklable.._break_on_call_reducez N) __reduce_ex__ __module__)clsr"rrr_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 2scs,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. cs tg|_g|_g|_dS)N)super__init__ _member_names _last_values_ignore)r ) __class__rrr.@s z_EnumDict.__init__csPt|r|dkrtd|dkr.t|d|nV|dkrt|trR|dd}nt|}||_t |t |j @}|rtd|fnt |r|d krd }n||j krt d |n||jkrn~t |s>||krt d |||ft|tr&|jtkr ||d t|j |jdd|_|j}|j ||j|t||dS)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 user5_generate_next_valuer7, z-_ignore_ cannot specify already set names: %rZ __order__r3zAttempted to reuse key: %rz%r already defined as: %rrN)r ValueErrorsetattr isinstancestrreplacesplitlistr1setr/rrrr r+r*r8rr0appendr- __setitem__)r keyr+Zalready)r2rrrDFs>       "  z_EnumDict.__setitem__)r'r$r(r)r.rD __classcell__rr)r2rr,9s 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}||\}}|dk r,t|dd|d<|S)Nr5)r, _get_mixins_getattr)metaclsr%basesZ enum_dict member_type first_enumrrr __prepare__}s zEnumMeta.__prepare__c s"dgdd}x|D]}|dq W||\}||\}}} fddjD} xjD] } | =qrWdd} t| ddh@} | rtdd | d krd d <t |||}g|_ t |_|_d d |D}i|_d kr:tk r:d}tfdd|Ds:t|x6jD]*}| |}t|tsf|f}n|}tkrz|f}| s||}t|ds||_n6||f|}t|ds҈tkr||_n ||_|j}||_||_|j|x8|jD]\} }|j|jkr|}PqW|j |||kr>t|||||j|<y||j|<Wntk rlYnXqDWxPdD]H} t|| }t| d}t|| d}|dk rz||krzt|| |qzWt dk r|r||_!t j |_ | dk rt| t"r | #dd$} | |j krtd|S)Nr7csi|]}||qSrr).0k) classdictrr sz$EnumMeta.__new__..r3mrozInvalid enum member name: {0}r9r)zAn enumeration.cSs.h|]&}|jD]\}}t|tr|qqSr)__dict__itemsr=r)rNcrOvrrr sz#EnumMeta.__new__..r#)Z__getnewargs_ex____getnewargs__r# __reduce__c3s|]}|jkVqdS)N)rT)rNm)rKrr sz#EnumMeta.__new__.._value_)__repr____str__ __format__r#r:z#member order does not match _order_)% setdefaultrCpoprG _find_new_r/rBr;formatjoinr-__new___member_names_r _member_map_ _member_type_rR_value2member_map_objectanyr&r=tuplerr]_name_ __objclass__r.rUr<rrHr__new_member__r>r?r@)rIr%rJrPignorerErLrfsave_newuse_argsZ enum_membersrr3Z invalid_names enum_classZdynamic_attributesmethods member_namer+argsZ enum_memberZcanonical_memberZ class_methodZ obj_methodZ enum_method)r2)rPrKrrfs                           zEnumMeta.__new__cCsdS)z6 classes/types should always be True. Tr)r rrr__bool__szEnumMeta.__bool__Nr)modulequalnametypestartcCs*|dkr|||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. N)ryrzr{r|)rf_create_)r%r+namesryrzr{r|rrr__call__s zEnumMeta.__call__cCs6t|ts ddl}|dtdt||o4|j|jkS)NrzHusing non-Enums in containment checks will raise TypeError in Python 3.8r)r=rwarningswarnDeprecationWarningrnrh)r%memberrrrr __contains__:s  zEnumMeta.__contains__cs(||jkrtd|jt|dS)Nz%s: cannot delete Enum member.)rhAttributeErrorr'r- __delattr__)r%Zattr)r2rrrCs  zEnumMeta.__delattr__cCsddddg|jS)Nr2r) __members__r$)rg)r rrr__dir__Ks zEnumMeta.__dir__cCs>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)rrrhKeyError)r%rrrr __getattr__Os  zEnumMeta.__getattr__cCs |j|S)N)rh)r%rrrr __getitem___szEnumMeta.__getitem__csfddjDS)Nc3s|]}j|VqdS)N)rh)rNr)r%rrr\csz$EnumMeta.__iter__..)rg)r%r)r%r__iter__bszEnumMeta.__iter__cCs t|jS)N)rrg)r%rrr__len__eszEnumMeta.__len__cCs t|jS)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. )rrh)r%rrrrhszEnumMeta.__members__cCs d|jS)Nz )r')r%rrrr^rszEnumMeta.__repr__csfddtjDS)Nc3s|]}j|VqdS)N)rh)rNr)r%rrr\vsz(EnumMeta.__reversed__..)reversedrg)r%r)r%r __reversed__uszEnumMeta.__reversed__cs0|jdi}||krtdt||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. rhzCannot reassign members.N)rTgetrr- __setattr__)r%rr+Z member_map)r2rrrxszEnumMeta.__setattr__c Cs|j}|dkr|fn||f}||\} } |||} t|trP|dd}t|ttfr|rt|dtr|g} }g} xDt | D]8\}}| |||| dd}| || ||fqWx6|D].}t|tr|||}}n|\}}|| |<qW| |||| }|dkrVyt djd}Wn*tttfk rT}zWdd}~XYnX|dkrjt|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. Nr9r:rrr')r2rGrMr=r>r?r@rmrA enumerater5rCrfsys _getframe f_globalsrr;rr&r$r()r%Z class_namer~ryrzr{r|rIrJrrLrPZoriginal_names last_valuescountrr+itemrv member_valuertexcrrrr}s<             zEnumMeta._create_cCsP|s ttfSdd}|d}t|ts.td||p8t}|jrHtd||fS)zReturns the type for creating enum members, and the first inherited enum class. bases: the tuple of bases that was given to __new__ cSsHxB|D]:}x4|jD]*}|tkr"qqd|jkrt|tr8q|SqWqWdS)Nrf)__mro__rkrT issubclassr)rJchainbaserrr_find_data_types    z.EnumMeta._get_mixins_.._find_data_typerzZnew enumerations should be created as `EnumName([mixin_type, ...] [data_type,] enum_type)`zCannot extend enumerations)rkrrrrg)rJrrLrKrrrrGs   zEnumMeta._get_mixins_c Cs|dd}|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__ rfN)rprfFT)rrHrfrkr) rPrKrLrfrrmethodZpossibletargetrsrrrrcs(     zEnumMeta._find_new_)N)r'r$r(r) classmethodrMrfrxrrrrrrrrpropertyrr^rrr} staticmethodrGrcrFrr)r2rr{s(    5 c@seZdZdZddZddZeddZdd Zd d Z d d Z ddZ ddZ ddZ eddZeddZedddZdS)rzRGeneric enumeration. Derive from this class to define new enumerations. c Cst||kr|Sy |j|Stk r.Yn6tk rbx |jD]}|j|krH|SqHWYnXyd}||}Wn*tk r}z |}d}Wdd}~XYnXt ||r|St d||j f}|dkr|dkr|n|dkrtd|j |f}||_ |dS)Nz%r is not a valid %szDerror in %s._missing_: returned %r instead of None or a valid member) r{rjrrrhvaluesr]r6 Exceptionr=r;r' __context__)r%r+rrresulteZve_excrrrrfs6    z Enum.__new__c Cs8x2t|D]"}y|dStk r*Yq Xq W|SdS)Nr)rr)rr|rr last_valuerrrr56s  zEnum._generate_next_value_cCstd||jfdS)Nz%r is not a valid %s)r;r')r%r+rrrr6?szEnum._missing_cCsd|jj|j|jfS)Nz <%s.%s: %r>)r2r'rnr])r rrrr^Csz Enum.__repr__cCsd|jj|jfS)Nz%s.%s)r2r'rn)r rrrr_Gsz Enum.__str__cs&fddjD}dddg|S)Ncs2g|]*}|jD]}|ddkr|jkr|qqS)rr)rTrh)rNr%r[)r rr Ls z Enum.__dir__..r2r)r$)r2rR)r Zadded_behaviorr)r rrJs z Enum.__dir__cCs0|jtkrt}t|}n |j}|j}|||S)N)rirkr>r]r`)r Z format_specr%valrrrr`Ss   zEnum.__format__cCs t|jS)N)hashrn)r rrr__hash__bsz Enum.__hash__cCs|j|jffS)N)r2r])r r!rrrr#eszEnum.__reduce_ex__cCs|jS)zThe name of the Enum member.)rn)r rrrrosz Enum.namecCs|jS)zThe value of the Enum member.)r])r rrrr+tsz Enum.valueNcsttj|}rtn|fddD}y|jdddWn$tk rp|jdddYnX||||d}t|_||j |||<|S)z[ Create a new Enum subclass that replaces a collection of global constants cs g|]}|r||fqSrr)rNr)filtersourcerrrsz!Enum._convert..cSs|d|dfS)Nrrr)trrrzEnum._convert..)rEcSs|dS)Nrr)rrrrrr)ry) varsrmoduleskeyssortr_reduce_ex_by_namer#updater)r%rryrrZmodule_globalsmembersr)rrr_convertys     z Enum._convert)N)r'r$r(r)rfr5rr6r^r_rr`rr#rrr+rrrrrrs(    ) metaclassc@seZdZdZdS)rz.Enum where members are also (and must be) intsN)r'r$r(r)rrrrrscCs|jS)N)r)r r!rrrrsrc@speZdZdZddZeddZeddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZdS)rzSupport 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_bitrr)rr|rrrZhigh_bitrrrr5s zFlag._generate_next_value_cCs.|}|dkr|}||}|dkr*|}|S)Nr)r4)r%r+Zoriginal_valueZpossible_memberrrrr6s zFlag._missing_cCsb|j|d}|dkr^t||\}}|r:td||jft|}d|_||_|j ||}|S)zL Create a composite member iff value contains only members. Nz%r is not a valid %s) rjr _decomposer;r'rkrfrnr]ra)r%r+ pseudo_memberr extra_flagsrrrr4s zFlag._create_pseudo_member_cCs8t||js&ddl}|dtddS|j|j@|jkS)NrzHusing non-Flags in containment checks will raise TypeError in Python 3.8rF)r=r2rrrr])r otherrrrrrs zFlag.__contains__cCsV|j}|jdk r$d|j|j|jfSt||j\}}d|jddd|D|jfS)Nz <%s.%s: %r>|cSsg|]}t|jp|jqSr)r>rnr])rNr[rrrrsz!Flag.__repr__..)r2rnr'r]rre)r r%r uncoveredrrrr^s z Flag.__repr__cCs|j}|jdk r d|j|jfSt||j\}}t|dkr^|djdkr^d|j|djfSd|jddd|DfSdS)Nz%s.%srrz%s.%rrcSsg|]}t|jp|jqSr)r>rnr])rNr[rrrrsz Flag.__str__..)r2rnr'rr]rre)r r%rrrrrr_s z Flag.__str__cCs t|jS)N)boolr])r rrrrxsz Flag.__bool__cCs"t||jstS||j|jBS)N)r=r2NotImplementedr])r rrrr__or__s z Flag.__or__cCs"t||jstS||j|j@S)N)r=r2rr])r rrrr__and__s z Flag.__and__cCs"t||jstS||j|jAS)N)r=r2rr])r rrrr__xor__s z Flag.__xor__cCsRt|j|j\}}|d}x*|jD] }||kr$|j|j@s$||B}q$W||S)Nr)rr2r])r rrZinvertedr[rrr __invert__ s    zFlag.__invert__N)r'r$r(r)r5rr6r4rr^r_rxrrrrrrrrrs    c@sTeZdZdZeddZeddZddZdd Zd d Z eZ eZ e Z d d Z dS)r zSupport for integer-based FlagscCs*t|tstd||jf||}|S)Nz%r is not a valid %s)r=intr;r'r4)r%r+Z new_memberrrrr6s  zIntFlag._missing_cCs|j|d}|dkr|g}t||\}}xL|rvt|}d|}||jkr\||kr\|||| krld}q,||N}q,Wx6t|D]*}t||}d|_||_ |j ||}qW|S)Nrr) rjrrrrCrrrfrnr]ra)r%r+rZneed_to_createrrZbitZ flag_valuerrrr4 s&     zIntFlag._create_pseudo_member_cCs0t||jtfstS||j||jB}|S)N)r=r2rrr])r rrrrrr>szIntFlag.__or__cCs,t||jtfstS||j||j@S)N)r=r2rrr])r rrrrrDszIntFlag.__and__cCs,t||jtfstS||j||jAS)N)r=r2rrr])r rrrrrIszIntFlag.__xor__cCs||j}|S)N)r2r])r rrrrrRszIntFlag.__invert__N)r'r$r(r)rr6r4rrr__ror____rand____rxor__rrrrrr s  cCs |dS)z@returns index of highest bit, or -1 if value is zero or negativer) bit_length)r+rrrrWsrcCsbg}x0|jD]"\}}||jkr|||jfqW|r^ddd|D}td||f|S)z?Class decorator for enumerations ensuring unique member values.z, cSsg|]\}}d||fqS)z%s -> %sr)rNaliasrrrrrcszunique..z duplicate values found in %r: %s)rrUrrCrer;)Z enumerationZ duplicatesrrZ alias_detailsrrrr [s  cCs|}|dk}|r*ddt|jD}nddt|jD}g}x4|D],\}}|rL||@|krL||||M}qLW|s||jkr||j||jddddt|d kr|dj|kr|d||fS) z#Extract all members from the value.rcSs"g|]\}}|jdk r||fqS)N)r)rNrWr[rrrrssz_decompose..cSs*g|]"\}}|jdk st|r||fqS)N)r _power_of_two)rNrWr[rrrrzscSs|jS)N)r])r[rrrrrz_decompose..T)rEreverser)rArjrUrCrrr+rb)Zflagr+Z not_coveredZnegativeZflags_to_checkrrrrrrrhs$  rcCs|dkr dS|dt|kS)NrFr)r)r+rrrrsr)rZtypesrrZ _collectionsr ImportErrorZ collections__all__rrrr&rkr*r dictr,rr{rrrrrr rr rrrrrrs<?qA #