3 ^F@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.__get____set__ __delete__)hasattr)objr/usr/lib64/python3.6/enum.py_is_descriptors  rcCsT|dd|ddko"dknoR|dddkoR|dd dkoRt|dkS) z3Returns True if a __dunder__ name, False otherwise.N___r)len)namerrr _is_dunders(rcCsL|d|dkodknoJ|dddkoJ|dddkoJt|dkS)z1Returns True if a _sunder_ name, False otherwise.rrrrr!)r)rrrr _is_sunder$s r"cCsdd}||_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 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|_dS)N)super__init__ _member_names _last_values)r$) __class__rrr2As 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_value __order__zAttempted to reuse key: %rz%r already defined as: %rr N)r6r7r8r9)r" ValueErrorsetattrrr3r#r isinstancer r/r.r:rr4appendr1 __setitem__)r$keyr/)r5rrr@Fs,    "  z_EnumDict.__setitem__)r+r(r,r-r2r@ __classcell__rr)r5rr0:s r0cseZdZdZeddZfddZddZd)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)Nr8)r0 _get_mixins_getattr)metaclsr)bases 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) classdictrr sz$EnumMeta.__new__..r6mrozInvalid enum member name: {0},r-zAn enumeration.cSs.h|]&}|jjD]\}}t|tr|qqSr)__dict__itemsr>r)rKcrLvrrr sz#EnumMeta.__new__..r'__getnewargs_ex____getnewargs__ __reduce__c3s|]}|jkVqdS)N)rQ)rKm)rHrr sz#EnumMeta.__new__.._value___repr____str__ __format__ z#member order does not match _order_)rVrWr'rX)r\r]r^r')$rC _find_new_r3popsetr<formatjoinr1__new___member_names_r _member_map_ _member_type_rO_value2member_map_objectanyr*r>tuplerr[_name_ __objclass__r2rRr?r=r#rDr__new_member__strreplacesplit)rEr)rFrMrIresave_newuse_args enum_membersrr6 invalid_names enum_classdynamic_attributesmethods member_namer/args enum_membercanonical_member class_method obj_method enum_method)r5)rMrHrre|s                          zEnumMeta.__new__cCsdS)z6 classes/types should always be True. Tr)r$rrr__bool__szEnumMeta.__bool__Nr )modulequalnametypestartcCs*|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. N)rrrr)re_create_)r)r/namesrrrrrrr__call__ s zEnumMeta.__call__cCst||o|j|jkS)N)r>rmrg)r)memberrrr __contains__)szEnumMeta.__contains__cs(||jkrtd|jtj|dS)Nz%s: cannot delete Enum member.)rgAttributeErrorr+r1 __delattr__)r)attr)r5rrr,s  zEnumMeta.__delattr__cCsddddg|jS)Nr5r- __members__r()rf)r$rrr__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)rrrgKeyError)r)rrrr __getattr__8s  zEnumMeta.__getattr__cCs |j|S)N)rg)r)rrrr __getitem__HszEnumMeta.__getitem__csfddjDS)Nc3s|]}j|VqdS)N)rg)rKr)r)rrrZLsz$EnumMeta.__iter__..)rf)r)r)r)r__iter__KszEnumMeta.__iter__cCs t|jS)N)rrf)r)rrr__len__NszEnumMeta.__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. )rrg)r)rrrrQszEnumMeta.__members__cCs d|jS)Nz )r+)r)rrrr\[szEnumMeta.__repr__csfddtjDS)Nc3s|]}j|VqdS)N)rg)rKr)r)rrrZ_sz(EnumMeta.__reversed__..)reversedrf)r)r)r)r __reversed__^szEnumMeta.__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. rgzCannot reassign members.N)rQgetrr1 __setattr__)r)rr/ member_map)r5rrraszEnumMeta.__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. NrPr_rrr+)r5rCrJr>rprqrrrllist enumerater8r?resys _getframe f_globalsrr<r*r(r,)r) class_namerrrrrrErFrrIrMoriginal_names last_valuescountrr/itemrz member_valuerwexcrrrrns<             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)`rr r!)rjr issubclassrfr#__mro__)rFrHrIbaserrrrCs(      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__ reNroFT)rore)rrDrerjr) rMrHrIrersmethodpossibletargetrtrrrr`s(     zEnumMeta._find_new_)N)r+r(r,r- classmethodrJrerrrrrrrrrpropertyrr\rrr staticmethodrCr`rBrr)r5rrps(     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|S)N)rrir#rgvaluesr[r9)r)r/rrrrres   z Enum.__new__c Cs8x2t|D]"}y|dStk r*Yq Xq W|SdS)Nr )rr#)rrrr last_valuerrrr8s  zEnum._generate_next_value_cCstd||jfdS)Nz%r is not a valid %s)r<r+)r)r/rrrr9"szEnum._missing_cCsd|jj|j|jfS)Nz <%s.%s: %r>)r5r+rmr[)r$rrrr\&sz Enum.__repr__cCsd|jj|jfS)Nz%s.%s)r5r+rm)r$rrrr]*sz Enum.__str__cs&fddjjD}dddg|S)Ncs2g|]*}|jD]}|ddkr|jkr|qqS)rr)rQrg)rKr)rY)r$rr /s z Enum.__dir__..r5r-r()r5rO)r$added_behaviorr)r$rr-s z Enum.__dir__cCs0|jtkrt}t|}n |j}|j}|j||S)N)rhrjrpr[r^)r$ format_specr)valrrrr^6s   zEnum.__format__cCs t|jS)N)hashrm)r$rrr__hash__Esz Enum.__hash__cCs|j|jffS)N)r5r[)r$r%rrrr'HszEnum.__reduce_ex__cCs|jS)zThe name of the Enum member.)rm)r$rrrrRsz Enum.namecCs|jS)zThe value of the Enum member.)r[)r$rrrr/Wsz 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||fqSrr)rKr)filtersourcerrrqsz!Enum._convert..cSs|d|dfS)Nr rr)trrrvszEnum._convert..)rAcSs|dS)Nrr)rrrrrys)r) varsrmoduleskeyssortr#_reduce_ex_by_namer'updater)r)rrrrmodule_globalsmembersr)rrr_convert\s     z Enum._convert)N)r+r(r,r-rer8rr9r\r]rr^rr'rrr/rrrrrrs    ) metaclassc@seZdZdZdS)r z.Enum where members are also (and must be) intsN)r+r(r,r-rrrrr scCs|jS)N)r)r$r%rrrrsrc@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 Nr zInvalid Flag value: %rr)r _high_bit Exceptionr#)rrrrrhigh_bitrrrr8s zFlag._generate_next_value_cCs.|}|dkr|}|j|}|dkr*|}|S)Nr)r7)r)r/original_valuepossible_memberrrrr9s zFlag._missing_cCsb|jj|d}|dkr^t||\}}|r:td||jftj|}d|_||_|jj ||}|S)zL Create a composite member iff value contains only members. Nz%r is not a valid %s) rir _decomposer<r+rjrermr[ setdefault)r)r/ pseudo_memberr extra_flagsrrrr7s zFlag._create_pseudo_member_cCs"t||jstS|j|j@|jkS)N)r>r5NotImplementedr[)r$otherrrrrs zFlag.__contains__cCsV|j}|jdk r$d|j|j|jfSt||j\}}d|jdjdd|D|jfS)Nz <%s.%s: %r>|cSsg|]}t|jp|jqSr)rprmr[)rKrYrrrrsz!Flag.__repr__..)r5rmr+r[rrd)r$r)r uncoveredrrrr\s z Flag.__repr__cCs|j}|jdk r d|j|jfSt||j\}}t|dkr^|djdkr^d|j|djfSd|jdjdd|DfSdS)Nz%s.%sr rz%s.%rrcSsg|]}t|jp|jqSr)rprmr[)rKrYrrrrsz Flag.__str__..)r5rmr+rr[rrd)r$r)rrrrrr]s z Flag.__str__cCs t|jS)N)boolr[)r$rrrrsz Flag.__bool__cCs"t||jstS|j|j|jBS)N)r>r5rr[)r$rrrr__or__s z Flag.__or__cCs"t||jstS|j|j|j@S)N)r>r5rr[)r$rrrr__and__s z Flag.__and__cCs"t||jstS|j|j|jAS)N)r>r5rr[)r$rrrr__xor__s z Flag.__xor__csDtjj\}fddjD}tt|jd}j|S)Ncs&g|]}|kr|jj@ r|qSr)r[)rKrY)rr$rrrsz#Flag.__invert__..r)rr5r[r_or_)r$rinverted_membersinvertedr)rr$r __invert__szFlag.__invert__N)r+r(r,r-r8rr9r7rr\r]rrrrrrrrrr s   c@sTeZdZdZeddZeddZddZdd Zd d Z eZ eZ e Z d d Z dS)r zSupport for integer-based FlagscCs*t|tstd||jf|j|}|S)Nz%r is not a valid %s)r>intr<r+r7)r)r/ new_memberrrrr9s  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) rirrrr?rrrermr[r)r)r/rneed_to_createrrbit flag_valuerrrr7s&     zIntFlag._create_pseudo_member_cCs0t||jtfstS|j|j|j|jB}|S)N)r>r5rrr[)r$rresultrrrrszIntFlag.__or__cCs,t||jtfstS|j|j|j|j@S)N)r>r5rrr[)r$rrrrr#szIntFlag.__and__cCs,t||jtfstS|j|j|j|jAS)N)r>r5rrr[)r$rrrrr(szIntFlag.__xor__cCs|j|j}|S)N)r5r[)r$rrrrr1szIntFlag.__invert__N)r+r(r,r-rr9r7rrr__ror____rand____rxor__rrrrrr s  cCs |jdS)z@returns index of highest bit, or -1 if value is zero or negativer ) bit_length)r/rrrr6srcCsbg}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)rKaliasrrrrrBszunique..z duplicate values found in %r: %s)rrRrr?rdr<) enumeration duplicatesrr 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||fqS)N)r)rKrTrYrrrrRsz_decompose..cSs*g|]"\}}|jdk st|r||fqS)N)r _power_of_two)rKrTrYrrrrYscSs|jS)N)r[)rYrrrrdsz_decompose..T)rAreverser )rrirRr?rrr/ra)flagr/ not_coverednegativeflags_to_checkrrrrrrrGs$  rcCs|dkr dS|dt|kS)Nr Fr)r)r/rrrrjsr)#rtypesrr functoolsroperatorrr _collectionsr ImportError collections__all__rrr"r*rjr.r dictr0rrrrr rr r rr rrrrrrs@  3mA #