B Y6d@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|_d|_dS)NF)super__init__ _member_names _last_values_ignore _auto_called)r ) __class__rrr.@s  z_EnumDict.__init__csdt|r|dkrtd|dkr<|jr.tdt|d|nV|dkrt|tr`|dd}nt |}||_ t |t |j @}|rtd |fnt |r|d krd }n||j krtd |n||j krnt|sR||krtd |||ft|tr:d|_|jtkr4||dt|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 user6z4_generate_next_value_ must be defined before members_generate_next_valuer8, z-_ignore_ cannot specify already set names: %rZ __order__r4zAttempted to reuse key: %rz%r already defined as: %rTrN)r ValueErrorr2rsetattr isinstancestrreplacesplitlistr1setr/rrr r+r*r9rr0appendr- __setitem__)r keyr+Zalready)r3rrrEGsD       "  z_EnumDict.__setitem__)r'r$r(r)r.rE __classcell__rr)r3rr,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)Nr6)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)Nr8csi|]}||qSrr).0k) classdictrr sz$EnumMeta.__new__..r4mrozInvalid enum member name: {0}r:r)zAn enumeration.cSs.h|]&}|jD]\}}t|tr|qqSr)__dict__itemsr>r)rOcrPvrrr sz#EnumMeta.__new__..r#)Z__getnewargs_ex____getnewargs__r# __reduce__c3s|]}|jkVqdS)N)rU)rOm)rLrr sz#EnumMeta.__new__.._value_)__repr____str__ __format__r#r;z#member order does not match _order_)% setdefaultrDpoprH _find_new_r/rCr<formatjoinr-__new___member_names_r _member_map_ _member_type_rS_value2member_map_objectanyr&r>tuplerr^_name_ __objclass__r.rVr=rrIr__new_member__r?r@rA)rJr%rKrQignorerFrMrgsave_newuse_argsZ enum_membersrr4Z invalid_names enum_classZdynamic_attributesmethods member_namer+argsZ enum_memberZcanonical_memberZ class_methodZ obj_methodZ enum_method)r3)rQrLrrgs                           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)rzr{r|r})rg_create_)r%r+namesrzr{r|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>rwarningswarnDeprecationWarningrori)r%memberrrrr __contains__?s  zEnumMeta.__contains__cs(||jkrtd|jt|dS)Nz%s: cannot delete Enum member.)riAttributeErrorr'r- __delattr__)r%Zattr)r3rrrHs  zEnumMeta.__delattr__cCsddddg|jS)Nr3r) __members__r$)rh)r rrr__dir__Ps 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)rrriKeyError)r%rrrr __getattr__Ts  zEnumMeta.__getattr__cCs |j|S)N)ri)r%rrrr __getitem__dszEnumMeta.__getitem__csfddjDS)Nc3s|]}j|VqdS)N)ri)rOr)r%rrr]hsz$EnumMeta.__iter__..)rh)r%r)r%r__iter__gszEnumMeta.__iter__cCs t|jS)N)rrh)r%rrr__len__jszEnumMeta.__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. )rri)r%rrrrmszEnumMeta.__members__cCs d|jS)Nz )r')r%rrrr_wszEnumMeta.__repr__csfddtjDS)Nc3s|]}j|VqdS)N)ri)rOr)r%rrr]{sz(EnumMeta.__reversed__..)reversedrh)r%r)r%r __reversed__zszEnumMeta.__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. rizCannot reassign members.N)rUgetrr- __setattr__)r%rr+Z member_map)r3rrr}szEnumMeta.__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. Nr:r;rrr')r3rHrNr>r?r@rArnrB enumerater6rDrgsys _getframe f_globalsrr<rr&r$r()r%Z class_namerrzr{r|r}rJrKrrMrQZoriginal_names last_valuescountrr+itemrw member_valueruexcrrrr~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)Nrg)__mro__rlrU issubclassr)rKchainbaserrr_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)rlrrrrh)rKrrMrLrrrrHs   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__ rgN)rqrgFT)rrIrgrlr) rQrLrMrgrsmethodZpossibletargetrtrrrrds(     zEnumMeta._find_new_)N)r'r$r(r) classmethodrNrgryrrrrrrrrpropertyrr_rrr~ staticmethodrHrdrGrr)r3rrs(    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|rkrrrivaluesr^r7 Exceptionr>r<r' __context__)r%r+rrresulteZve_excrrrrgs6    z Enum.__new__c Cs8x2t|D]"}y|dStk r*Yq Xq W|SdS)Nr)rr)rr}rr last_valuerrrr6;s  zEnum._generate_next_value_cCstd||jfdS)Nz%r is not a valid %s)r<r')r%r+rrrr7DszEnum._missing_cCsd|jj|j|jfS)Nz <%s.%s: %r>)r3r'ror^)r rrrr_Hsz Enum.__repr__cCsd|jj|jfS)Nz%s.%s)r3r'ro)r rrrr`Lsz Enum.__str__cs&fddjD}dddg|S)Ncs2g|]*}|jD]}|ddkr|jkr|qqS)rr)rUri)rOr%r\)r rr Qs z Enum.__dir__..r3r)r$)r3rS)r Zadded_behaviorr)r rrOs z Enum.__dir__cCs0|jtkrt}t|}n |j}|j}|||S)N)rjrlr?r^ra)r Z format_specr%valrrrraXs   zEnum.__format__cCs t|jS)N)hashro)r rrr__hash__gsz Enum.__hash__cCs|j|jffS)N)r3r^)r r!rrrr#jszEnum.__reduce_ex__cCs|jS)zThe name of the Enum member.)ro)r rrrrtsz Enum.namecCs|jS)zThe value of the Enum member.)r^)r rrrr+ysz 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)rOr)filtersourcerrrsz!Enum._convert..cSs|d|dfS)Nrrr)trrrzEnum._convert..)rFcSs|dS)Nrr)rrrrrr)rz) varsrmoduleskeyssortr_reduce_ex_by_namer#updater)r%rrzrrZmodule_globalsmembersr)rrr_convert~s     z Enum._convert)N)r'r$r(r)rgr6rr7r_r`rrarr#rrr+rrrrrr s(    ) 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 initial 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_bitrrrr6s zFlag._generate_next_value_cCs.|}|dkr|}||}|dkr*|}|S)Nr)r5)r%r+Zoriginal_valueZpossible_memberrrrr7s 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) rkr _decomposer<r'rlrgror^rb)r%r+ pseudo_memberr extra_flagsrrrr5s 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>r3rrrr^)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?ror^)rOr\rrrrsz!Flag.__repr__..)r3ror'r^rrf)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?ror^)rOr\rrrrsz Flag.__str__..)r3ror'rr^rrf)r r%rrrrrr`s z Flag.__str__cCs t|jS)N)boolr^)r rrrrysz Flag.__bool__cCs"t||jstS||j|jBS)N)r>r3NotImplementedr^)r rrrr__or__s z Flag.__or__cCs"t||jstS||j|j@S)N)r>r3rr^)r rrrr__and__s z Flag.__and__cCs"t||jstS||j|jAS)N)r>r3rr^)r rrrr__xor__ s z Flag.__xor__cCsRt|j|j\}}|d}x*|jD] }||kr$|j|j@s$||B}q$W||S)Nr)rr3r^)r rrZinvertedr\rrr __invert__s    zFlag.__invert__N)r'r$r(r)r6rr7r5rr_r`ryrrrrrrrrrs    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'r5)r%r+Z new_memberrrrr7s  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) rkrrrrDrrrgror^rb)r%r+rZneed_to_createrrZbitZ flag_valuerrrr5%s&     zIntFlag._create_pseudo_member_cCs0t||jtfstS||j||jB}|S)N)r>r3rrr^)r rrrrrrCszIntFlag.__or__cCs,t||jtfstS||j||j@S)N)r>r3rrr^)r rrrrrIszIntFlag.__and__cCs,t||jtfstS||j||jAS)N)r>r3rrr^)r rrrrrNszIntFlag.__xor__cCs||j}|S)N)r3r^)r rrrrrWszIntFlag.__invert__N)r'r$r(r)rr7r5rrr__ror____rand____rxor__rrrrrr s  cCs |dS)z@returns index of highest bit, or -1 if value is zero or negativer) bit_length)r+rrrr\srcCsbg}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)rOaliasrrrrrhszunique..z duplicate values found in %r: %s)rrVrrDrfr<)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)rOrXr\rrrrxsz_decompose..cSs*g|]"\}}|jdk st|r||fqS)N)r _power_of_two)rOrXr\rrrrscSs|jS)N)r^)r\rrrrrz_decompose..T)rFreverser)rBrkrVrDrrr+rc)Zflagr+Z not_coveredZnegativeZflags_to_checkrrrrrrrms$  rcCs|dkr dS|dt|kS)NrFr)r)r+rrrrsr)rZtypesrrZ _collectionsr ImportErrorZ collections__all__rrrr&rlr*r dictr,rr|rrrrrr rr rrrrrrs<DqA #