| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2013-09-25 07:14:41 -07:00
										 |  |  | from types import MappingProxyType, DynamicClassAttribute | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 22:03:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  | __all__ = [ | 
					
						
							|  |  |  |         'EnumMeta', | 
					
						
							|  |  |  |         'Enum', 'IntEnum', 'Flag', 'IntFlag', | 
					
						
							|  |  |  |         'auto', 'unique', | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-15 12:34:36 -07:00
										 |  |  | def _is_descriptor(obj): | 
					
						
							|  |  |  |     """Returns True if obj is a descriptor, False otherwise.""" | 
					
						
							|  |  |  |     return ( | 
					
						
							|  |  |  |             hasattr(obj, '__get__') or | 
					
						
							|  |  |  |             hasattr(obj, '__set__') or | 
					
						
							|  |  |  |             hasattr(obj, '__delete__')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | def _is_dunder(name): | 
					
						
							|  |  |  |     """Returns True if a __dunder__ name, False otherwise.""" | 
					
						
							| 
									
										
										
										
											2019-03-03 14:09:11 -08:00
										 |  |  |     return (len(name) > 4 and | 
					
						
							|  |  |  |             name[:2] == name[-2:] == '__' and | 
					
						
							|  |  |  |             name[2] != '_' and | 
					
						
							|  |  |  |             name[-3] != '_') | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _is_sunder(name): | 
					
						
							|  |  |  |     """Returns True if a _sunder_ name, False otherwise.""" | 
					
						
							| 
									
										
										
										
											2019-03-03 14:09:11 -08:00
										 |  |  |     return (len(name) > 2 and | 
					
						
							|  |  |  |             name[0] == name[-1] == '_' and | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |             name[1:2] != '_' and | 
					
						
							| 
									
										
										
										
											2019-03-03 14:09:11 -08:00
										 |  |  |             name[-2:-1] != '_') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _make_class_unpicklable(cls): | 
					
						
							|  |  |  |     """Make the given class un-picklable.""" | 
					
						
							| 
									
										
										
										
											2014-02-08 11:36:27 -08:00
										 |  |  |     def _break_on_call_reduce(self, proto): | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         raise TypeError('%r cannot be pickled' % self) | 
					
						
							| 
									
										
										
										
											2014-02-08 11:36:27 -08:00
										 |  |  |     cls.__reduce_ex__ = _break_on_call_reduce | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |     cls.__module__ = '<unknown>' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  | _auto_null = object() | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  | class auto: | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Instances are replaced with an appropriate value in Enum class suites. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |     value = _auto_null | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-15 12:34:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | class _EnumDict(dict): | 
					
						
							| 
									
										
										
										
											2013-09-15 12:34:36 -07:00
										 |  |  |     """Track enum member order and ensure member names are not reused.
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EnumMeta will use the names found in self._member_names as the | 
					
						
							|  |  |  |     enumeration member names. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     def __init__(self): | 
					
						
							|  |  |  |         super().__init__() | 
					
						
							|  |  |  |         self._member_names = [] | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |         self._last_values = [] | 
					
						
							| 
									
										
										
										
											2018-01-22 07:56:37 -08:00
										 |  |  |         self._ignore = [] | 
					
						
							| 
									
										
										
										
											2020-04-28 13:20:55 -04:00
										 |  |  |         self._auto_called = False | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __setitem__(self, key, value): | 
					
						
							| 
									
										
										
										
											2013-09-15 12:34:36 -07:00
										 |  |  |         """Changes anything not dundered or not a descriptor.
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         If an enum member name is used twice, an error is raised; duplicate | 
					
						
							|  |  |  |         values are not checked for. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Single underscore (sunder) names are reserved. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if _is_sunder(key): | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |             if key not in ( | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |                     '_order_', '_create_pseudo_member_', | 
					
						
							| 
									
										
										
										
											2018-01-22 07:56:37 -08:00
										 |  |  |                     '_generate_next_value_', '_missing_', '_ignore_', | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |                     ): | 
					
						
							| 
									
										
										
										
											2016-08-20 07:19:31 -07:00
										 |  |  |                 raise ValueError('_names_ are reserved for future Enum use') | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |             if key == '_generate_next_value_': | 
					
						
							| 
									
										
										
										
											2020-04-28 13:20:55 -04:00
										 |  |  |                 # check if members already defined as auto() | 
					
						
							|  |  |  |                 if self._auto_called: | 
					
						
							|  |  |  |                     raise TypeError("_generate_next_value_ must be defined before members") | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |                 setattr(self, '_generate_next_value', value) | 
					
						
							| 
									
										
										
										
											2018-01-22 07:56:37 -08:00
										 |  |  |             elif key == '_ignore_': | 
					
						
							|  |  |  |                 if isinstance(value, str): | 
					
						
							|  |  |  |                     value = value.replace(',',' ').split() | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     value = list(value) | 
					
						
							|  |  |  |                 self._ignore = value | 
					
						
							|  |  |  |                 already = set(value) & set(self._member_names) | 
					
						
							|  |  |  |                 if already: | 
					
						
							|  |  |  |                     raise ValueError('_ignore_ cannot specify already set names: %r' % (already, )) | 
					
						
							| 
									
										
										
										
											2013-09-15 12:34:36 -07:00
										 |  |  |         elif _is_dunder(key): | 
					
						
							| 
									
										
										
										
											2016-08-20 07:19:31 -07:00
										 |  |  |             if key == '__order__': | 
					
						
							|  |  |  |                 key = '_order_' | 
					
						
							| 
									
										
										
										
											2013-09-15 12:34:36 -07:00
										 |  |  |         elif key in self._member_names: | 
					
						
							|  |  |  |             # descriptor overwriting an enum? | 
					
						
							|  |  |  |             raise TypeError('Attempted to reuse key: %r' % key) | 
					
						
							| 
									
										
										
										
											2018-01-22 07:56:37 -08:00
										 |  |  |         elif key in self._ignore: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2013-09-15 12:34:36 -07:00
										 |  |  |         elif not _is_descriptor(value): | 
					
						
							|  |  |  |             if key in self: | 
					
						
							|  |  |  |                 # enum overwriting a descriptor? | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |                 raise TypeError('%r already defined as: %r' % (key, self[key])) | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |             if isinstance(value, auto): | 
					
						
							| 
									
										
										
										
											2020-04-28 13:20:55 -04:00
										 |  |  |                 self._auto_called = True | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |                 if value.value == _auto_null: | 
					
						
							|  |  |  |                     value.value = self._generate_next_value(key, 1, len(self._member_names), self._last_values[:]) | 
					
						
							|  |  |  |                 value = value.value | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |             self._member_names.append(key) | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |             self._last_values.append(value) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         super().__setitem__(key, value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-17 15:53:55 +03:00
										 |  |  | # Dummy value for Enum as EnumMeta explicitly checks for it, but of course | 
					
						
							|  |  |  | # until EnumMeta finishes running the first time the Enum class doesn't exist. | 
					
						
							|  |  |  | # This is also why there are checks in EnumMeta like `if Enum is not None` | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | Enum = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-20 00:00:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | class EnumMeta(type): | 
					
						
							|  |  |  |     """Metaclass for Enum""" | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2016-08-20 00:00:52 -07:00
										 |  |  |     def __prepare__(metacls, cls, bases): | 
					
						
							| 
									
										
										
										
											2020-09-16 07:35:14 -07:00
										 |  |  |         # check that previous enum members do not exist | 
					
						
							|  |  |  |         metacls._check_for_existing_members(cls, bases) | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         # create the namespace dict | 
					
						
							|  |  |  |         enum_dict = _EnumDict() | 
					
						
							|  |  |  |         # inherit previous flags and _generate_next_value_ function | 
					
						
							| 
									
										
										
										
											2020-09-16 07:35:14 -07:00
										 |  |  |         member_type, first_enum = metacls._get_mixins_(cls, bases) | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         if first_enum is not None: | 
					
						
							|  |  |  |             enum_dict['_generate_next_value_'] = getattr(first_enum, '_generate_next_value_', None) | 
					
						
							|  |  |  |         return enum_dict | 
					
						
							| 
									
										
										
										
											2016-08-20 00:00:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 23:55:19 -07:00
										 |  |  |     def __new__(metacls, cls, bases, classdict): | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         # an Enum class is final once enumeration items have been defined; it | 
					
						
							|  |  |  |         # cannot be mixed with other types (int, float, etc.) if it has an | 
					
						
							|  |  |  |         # inherited __new__ unless a new __new__ is defined (or the resulting | 
					
						
							|  |  |  |         # class will fail). | 
					
						
							| 
									
										
										
										
											2018-01-22 07:56:37 -08:00
										 |  |  |         # | 
					
						
							|  |  |  |         # remove any keys listed in _ignore_ | 
					
						
							|  |  |  |         classdict.setdefault('_ignore_', []).append('_ignore_') | 
					
						
							|  |  |  |         ignore = classdict['_ignore_'] | 
					
						
							|  |  |  |         for key in ignore: | 
					
						
							|  |  |  |             classdict.pop(key, None) | 
					
						
							| 
									
										
										
										
											2020-09-16 07:35:14 -07:00
										 |  |  |         member_type, first_enum = metacls._get_mixins_(cls, bases) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         __new__, save_new, use_args = metacls._find_new_(classdict, member_type, | 
					
						
							|  |  |  |                                                         first_enum) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # save enum items into separate mapping so they don't get baked into | 
					
						
							|  |  |  |         # the new class | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         enum_members = {k: classdict[k] for k in classdict._member_names} | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         for name in classdict._member_names: | 
					
						
							|  |  |  |             del classdict[name] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-20 07:19:31 -07:00
										 |  |  |         # adjust the sunders | 
					
						
							|  |  |  |         _order_ = classdict.pop('_order_', None) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         # check for illegal enum names (any others?) | 
					
						
							| 
									
										
										
										
											2019-03-03 14:09:11 -08:00
										 |  |  |         invalid_names = set(enum_members) & {'mro', ''} | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         if invalid_names: | 
					
						
							|  |  |  |             raise ValueError('Invalid enum member name: {0}'.format( | 
					
						
							|  |  |  |                 ','.join(invalid_names))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-11 23:23:06 -07:00
										 |  |  |         # create a default docstring if one has not been provided | 
					
						
							|  |  |  |         if '__doc__' not in classdict: | 
					
						
							|  |  |  |             classdict['__doc__'] = 'An enumeration.' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         # create our new Enum type | 
					
						
							|  |  |  |         enum_class = super().__new__(metacls, cls, bases, classdict) | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |         enum_class._member_names_ = []               # names in definition order | 
					
						
							| 
									
										
										
										
											2018-06-19 01:14:26 +09:00
										 |  |  |         enum_class._member_map_ = {}                 # name->value map | 
					
						
							| 
									
										
										
										
											2013-08-04 08:42:23 -07:00
										 |  |  |         enum_class._member_type_ = member_type | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 20:28:53 +03:00
										 |  |  |         # save DynamicClassAttribute attributes from super classes so we know | 
					
						
							|  |  |  |         # if we can take the shortcut of storing members in the class dict | 
					
						
							|  |  |  |         dynamic_attributes = {k for c in enum_class.mro() | 
					
						
							|  |  |  |                               for k, v in c.__dict__.items() | 
					
						
							|  |  |  |                               if isinstance(v, DynamicClassAttribute)} | 
					
						
							| 
									
										
										
										
											2015-03-11 08:43:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         # Reverse value->name map for hashable values. | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |         enum_class._value2member_map_ = {} | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 12:42:52 -08:00
										 |  |  |         # If a custom type is mixed into the Enum, and it does not know how | 
					
						
							|  |  |  |         # to pickle itself, pickle.dumps will succeed but pickle.loads will | 
					
						
							|  |  |  |         # fail.  Rather than have the error show up later and possibly far | 
					
						
							|  |  |  |         # from the source, sabotage the pickle protocol for this class so | 
					
						
							|  |  |  |         # that pickle.dumps also fails. | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # However, if the new class implements its own __reduce_ex__, do not | 
					
						
							|  |  |  |         # sabotage -- it's on them to make sure it works correctly.  We use | 
					
						
							|  |  |  |         # __reduce_ex__ instead of any of the others as it is preferred by | 
					
						
							|  |  |  |         # pickle over __reduce__, and it handles all pickle protocols. | 
					
						
							|  |  |  |         if '__reduce_ex__' not in classdict: | 
					
						
							| 
									
										
										
										
											2014-02-18 12:37:12 -08:00
										 |  |  |             if member_type is not object: | 
					
						
							|  |  |  |                 methods = ('__getnewargs_ex__', '__getnewargs__', | 
					
						
							|  |  |  |                         '__reduce_ex__', '__reduce__') | 
					
						
							| 
									
										
										
										
											2014-03-03 12:42:52 -08:00
										 |  |  |                 if not any(m in member_type.__dict__ for m in methods): | 
					
						
							| 
									
										
										
										
											2014-02-18 12:37:12 -08:00
										 |  |  |                     _make_class_unpicklable(enum_class) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # instantiate them, checking for duplicates as we go | 
					
						
							|  |  |  |         # we instantiate first instead of checking for duplicates first in case | 
					
						
							|  |  |  |         # a custom __new__ is doing something funky with the values -- such as | 
					
						
							|  |  |  |         # auto-numbering ;) | 
					
						
							|  |  |  |         for member_name in classdict._member_names: | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |             value = enum_members[member_name] | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |             if not isinstance(value, tuple): | 
					
						
							|  |  |  |                 args = (value, ) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 args = value | 
					
						
							|  |  |  |             if member_type is tuple:   # special case for tuple enums | 
					
						
							|  |  |  |                 args = (args, )     # wrap it one more time | 
					
						
							|  |  |  |             if not use_args: | 
					
						
							|  |  |  |                 enum_member = __new__(enum_class) | 
					
						
							| 
									
										
										
										
											2013-07-25 13:50:45 -07:00
										 |  |  |                 if not hasattr(enum_member, '_value_'): | 
					
						
							|  |  |  |                     enum_member._value_ = value | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 enum_member = __new__(enum_class, *args) | 
					
						
							| 
									
										
										
										
											2013-07-25 13:50:45 -07:00
										 |  |  |                 if not hasattr(enum_member, '_value_'): | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |                     if member_type is object: | 
					
						
							|  |  |  |                         enum_member._value_ = value | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         enum_member._value_ = member_type(*args) | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |             value = enum_member._value_ | 
					
						
							|  |  |  |             enum_member._name_ = member_name | 
					
						
							| 
									
										
										
										
											2013-09-15 16:59:35 -07:00
										 |  |  |             enum_member.__objclass__ = enum_class | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |             enum_member.__init__(*args) | 
					
						
							|  |  |  |             # If another member with the same value was already defined, the | 
					
						
							|  |  |  |             # new member becomes an alias to the existing one. | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |             for name, canonical_member in enum_class._member_map_.items(): | 
					
						
							| 
									
										
										
										
											2014-09-16 17:31:23 -07:00
										 |  |  |                 if canonical_member._value_ == enum_member._value_: | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |                     enum_member = canonical_member | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 # Aliases don't appear in member names (only in __members__). | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |                 enum_class._member_names_.append(member_name) | 
					
						
							| 
									
										
										
										
											2015-03-11 08:43:12 -07:00
										 |  |  |             # performance boost for any member that would not shadow | 
					
						
							|  |  |  |             # a DynamicClassAttribute | 
					
						
							| 
									
										
										
										
											2018-09-12 20:28:53 +03:00
										 |  |  |             if member_name not in dynamic_attributes: | 
					
						
							| 
									
										
										
										
											2015-03-11 08:43:12 -07:00
										 |  |  |                 setattr(enum_class, member_name, enum_member) | 
					
						
							|  |  |  |             # now add to _member_map_ | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |             enum_class._member_map_[member_name] = enum_member | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 # This may fail if value is not hashable. We can't add the value | 
					
						
							|  |  |  |                 # to the map, and by-value lookups for this value will be | 
					
						
							|  |  |  |                 # linear. | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |                 enum_class._value2member_map_[value] = enum_member | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |             except TypeError: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # double check that repr and friends are not the mixin's or various | 
					
						
							|  |  |  |         # things break (such as pickle) | 
					
						
							| 
									
										
										
										
											2020-09-16 03:58:33 -07:00
										 |  |  |         # however, if the method is defined in the Enum itself, don't replace | 
					
						
							|  |  |  |         # it | 
					
						
							| 
									
										
										
										
											2014-02-18 12:37:12 -08:00
										 |  |  |         for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'): | 
					
						
							| 
									
										
										
										
											2020-09-16 03:58:33 -07:00
										 |  |  |             if name in classdict: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |             class_method = getattr(enum_class, name) | 
					
						
							|  |  |  |             obj_method = getattr(member_type, name, None) | 
					
						
							|  |  |  |             enum_method = getattr(first_enum, name, None) | 
					
						
							|  |  |  |             if obj_method is not None and obj_method is class_method: | 
					
						
							|  |  |  |                 setattr(enum_class, name, enum_method) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # replace any other __new__ with our own (as long as Enum is not None, | 
					
						
							|  |  |  |         # anyway) -- again, this is to support pickle | 
					
						
							|  |  |  |         if Enum is not None: | 
					
						
							|  |  |  |             # if the user defined their own __new__, save it before it gets | 
					
						
							|  |  |  |             # clobbered in case they subclass later | 
					
						
							|  |  |  |             if save_new: | 
					
						
							|  |  |  |                 enum_class.__new_member__ = __new__ | 
					
						
							|  |  |  |             enum_class.__new__ = Enum.__new__ | 
					
						
							| 
									
										
										
										
											2016-08-20 07:19:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # py3 support for definition order (helps keep py2/py3 code in sync) | 
					
						
							|  |  |  |         if _order_ is not None: | 
					
						
							|  |  |  |             if isinstance(_order_, str): | 
					
						
							|  |  |  |                 _order_ = _order_.replace(',', ' ').split() | 
					
						
							|  |  |  |             if _order_ != enum_class._member_names_: | 
					
						
							|  |  |  |                 raise TypeError('member order does not match _order_') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         return enum_class | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-13 23:52:09 -07:00
										 |  |  |     def __bool__(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         classes/types should always be True. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-16 20:35:55 -07:00
										 |  |  |     def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1): | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         """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 | 
					
						
							| 
									
										
										
										
											2016-11-21 09:22:05 -08:00
										 |  |  |         (i.e. Color = Enum('Color', names='RED GREEN BLUE')). | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 12:42:52 -08:00
										 |  |  |         When used for the functional API: | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 12:42:52 -08:00
										 |  |  |         `value` will be the name of the new class. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         `names` should be either a string of white-space/comma delimited names | 
					
						
							| 
									
										
										
										
											2014-09-16 20:35:55 -07:00
										 |  |  |         (values will start at `start`), or an iterator/mapping of name, value pairs. | 
					
						
							| 
									
										
										
										
											2014-03-03 12:42:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         `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. | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if names is None:  # simple value lookup | 
					
						
							|  |  |  |             return cls.__new__(cls, value) | 
					
						
							|  |  |  |         # otherwise, functional API: we're creating a new Enum type | 
					
						
							| 
									
										
										
										
											2014-09-16 20:35:55 -07:00
										 |  |  |         return cls._create_(value, names, module=module, qualname=qualname, type=type, start=start) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __contains__(cls, member): | 
					
						
							| 
									
										
										
										
											2018-09-10 23:51:04 +05:30
										 |  |  |         if not isinstance(member, Enum): | 
					
						
							|  |  |  |             raise TypeError( | 
					
						
							|  |  |  |                 "unsupported operand type(s) for 'in': '%s' and '%s'" % ( | 
					
						
							|  |  |  |                     type(member).__qualname__, cls.__class__.__qualname__)) | 
					
						
							| 
									
										
										
										
											2014-09-16 17:31:23 -07:00
										 |  |  |         return isinstance(member, cls) and member._name_ in cls._member_map_ | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 16:18:19 -07:00
										 |  |  |     def __delattr__(cls, attr): | 
					
						
							|  |  |  |         # nicer error message when someone tries to delete an attribute | 
					
						
							|  |  |  |         # (see issue19025). | 
					
						
							|  |  |  |         if attr in cls._member_map_: | 
					
						
							|  |  |  |             raise AttributeError( | 
					
						
							|  |  |  |                     "%s: cannot delete Enum member." % cls.__name__) | 
					
						
							|  |  |  |         super().__delattr__(attr) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-12 06:51:41 -07:00
										 |  |  |     def __dir__(self): | 
					
						
							| 
									
										
										
										
											2013-09-22 16:18:19 -07:00
										 |  |  |         return (['__class__', '__doc__', '__members__', '__module__'] + | 
					
						
							|  |  |  |                 self._member_names_) | 
					
						
							| 
									
										
										
										
											2013-08-12 06:51:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |     def __getattr__(cls, name): | 
					
						
							|  |  |  |         """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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if _is_dunder(name): | 
					
						
							|  |  |  |             raise AttributeError(name) | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |             return cls._member_map_[name] | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         except KeyError: | 
					
						
							|  |  |  |             raise AttributeError(name) from None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __getitem__(cls, name): | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |         return cls._member_map_[name] | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __iter__(cls): | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |         return (cls._member_map_[name] for name in cls._member_names_) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __len__(cls): | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |         return len(cls._member_names_) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-14 18:11:24 -07:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def __members__(cls): | 
					
						
							|  |  |  |         """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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         return MappingProxyType(cls._member_map_) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |     def __repr__(cls): | 
					
						
							|  |  |  |         return "<enum %r>" % cls.__name__ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-14 18:11:24 -07:00
										 |  |  |     def __reversed__(cls): | 
					
						
							|  |  |  |         return (cls._member_map_[name] for name in reversed(cls._member_names_)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 07:16:48 -07:00
										 |  |  |     def __setattr__(cls, name, value): | 
					
						
							|  |  |  |         """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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         member_map = cls.__dict__.get('_member_map_', {}) | 
					
						
							|  |  |  |         if name in member_map: | 
					
						
							|  |  |  |             raise AttributeError('Cannot reassign members.') | 
					
						
							|  |  |  |         super().__setattr__(name, value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-16 04:40:35 +01:00
										 |  |  |     def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, start=1): | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         """Convenience method to create a new Enum class.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         `names` can be: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         * A string containing member names, separated either with spaces or | 
					
						
							| 
									
										
										
										
											2014-09-16 20:35:55 -07:00
										 |  |  |           commas.  Values are incremented by 1 from `start`. | 
					
						
							|  |  |  |         * An iterable of member names.  Values are incremented by 1 from `start`. | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         * An iterable of (member name, value) pairs. | 
					
						
							| 
									
										
										
										
											2014-09-16 20:35:55 -07:00
										 |  |  |         * A mapping of member name -> value pairs. | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         metacls = cls.__class__ | 
					
						
							|  |  |  |         bases = (cls, ) if type is None else (type, cls) | 
					
						
							| 
									
										
										
										
											2020-09-16 07:35:14 -07:00
										 |  |  |         _, first_enum = cls._get_mixins_(cls, bases) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         classdict = metacls.__prepare__(class_name, bases) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # special processing needed for names? | 
					
						
							|  |  |  |         if isinstance(names, str): | 
					
						
							|  |  |  |             names = names.replace(',', ' ').split() | 
					
						
							| 
									
										
										
										
											2017-06-22 01:52:32 +09:00
										 |  |  |         if isinstance(names, (tuple, list)) and names and isinstance(names[0], str): | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |             original_names, names = names, [] | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |             last_values = [] | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |             for count, name in enumerate(original_names): | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |                 value = first_enum._generate_next_value_(name, start, count, last_values[:]) | 
					
						
							|  |  |  |                 last_values.append(value) | 
					
						
							|  |  |  |                 names.append((name, value)) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Here, names is either an iterable of (name, value) or a mapping. | 
					
						
							|  |  |  |         for item in names: | 
					
						
							|  |  |  |             if isinstance(item, str): | 
					
						
							|  |  |  |                 member_name, member_value = item, names[item] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 member_name, member_value = item | 
					
						
							|  |  |  |             classdict[member_name] = member_value | 
					
						
							|  |  |  |         enum_class = metacls.__new__(metacls, class_name, bases, classdict) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # TODO: replace the frame hack if a blessed way to know the calling | 
					
						
							|  |  |  |         # module is ever developed | 
					
						
							|  |  |  |         if module is None: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 module = sys._getframe(2).f_globals['__name__'] | 
					
						
							| 
									
										
										
										
											2019-11-19 21:34:03 +00:00
										 |  |  |             except (AttributeError, ValueError, KeyError): | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |                 pass | 
					
						
							|  |  |  |         if module is None: | 
					
						
							|  |  |  |             _make_class_unpicklable(enum_class) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             enum_class.__module__ = module | 
					
						
							| 
									
										
										
										
											2014-02-08 11:36:27 -08:00
										 |  |  |         if qualname is not None: | 
					
						
							|  |  |  |             enum_class.__qualname__ = qualname | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return enum_class | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 20:28:53 +03:00
										 |  |  |     def _convert_(cls, name, module, filter, source=None): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Create a new Enum subclass that replaces a collection of global constants | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         # convert all constants from source (or module) that pass filter() to | 
					
						
							|  |  |  |         # a new Enum called name, and export the enum and its members back to | 
					
						
							|  |  |  |         # module; | 
					
						
							|  |  |  |         # also, replace the __reduce_ex__ method so unpickling works in | 
					
						
							|  |  |  |         # previous Python versions | 
					
						
							|  |  |  |         module_globals = vars(sys.modules[module]) | 
					
						
							|  |  |  |         if source: | 
					
						
							|  |  |  |             source = vars(source) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             source = module_globals | 
					
						
							|  |  |  |         # _value2member_map_ is populated in the same order every time | 
					
						
							|  |  |  |         # for a consistent reverse mapping of number to name when there | 
					
						
							|  |  |  |         # are multiple names for the same number. | 
					
						
							|  |  |  |         members = [ | 
					
						
							|  |  |  |                 (name, value) | 
					
						
							|  |  |  |                 for name, value in source.items() | 
					
						
							|  |  |  |                 if filter(name)] | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             # sort by value | 
					
						
							|  |  |  |             members.sort(key=lambda t: (t[1], t[0])) | 
					
						
							|  |  |  |         except TypeError: | 
					
						
							|  |  |  |             # unless some values aren't comparable, in which case sort by name | 
					
						
							|  |  |  |             members.sort(key=lambda t: t[0]) | 
					
						
							|  |  |  |         cls = cls(name, members, module=module) | 
					
						
							|  |  |  |         cls.__reduce_ex__ = _reduce_ex_by_name | 
					
						
							|  |  |  |         module_globals.update(cls.__members__) | 
					
						
							|  |  |  |         module_globals[name] = cls | 
					
						
							|  |  |  |         return cls | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2020-09-16 07:35:14 -07:00
										 |  |  |     def _check_for_existing_members(class_name, bases): | 
					
						
							|  |  |  |         for chain in bases: | 
					
						
							|  |  |  |             for base in chain.__mro__: | 
					
						
							|  |  |  |                 if issubclass(base, Enum) and base._member_names_: | 
					
						
							|  |  |  |                     raise TypeError("%s: cannot extend enumeration %r" % (class_name, base.__name__)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							|  |  |  |     def _get_mixins_(class_name, bases): | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         """Returns the type for creating enum members, and the first inherited
 | 
					
						
							|  |  |  |         enum class. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bases: the tuple of bases that was given to __new__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if not bases: | 
					
						
							|  |  |  |             return object, Enum | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 19:03:09 -07:00
										 |  |  |         def _find_data_type(bases): | 
					
						
							| 
									
										
										
										
											2020-09-15 16:59:48 -07:00
										 |  |  |             data_types = [] | 
					
						
							| 
									
										
										
										
											2018-09-21 19:03:09 -07:00
										 |  |  |             for chain in bases: | 
					
						
							| 
									
										
										
										
											2020-09-15 16:59:48 -07:00
										 |  |  |                 candidate = None | 
					
						
							| 
									
										
										
										
											2018-09-21 19:03:09 -07:00
										 |  |  |                 for base in chain.__mro__: | 
					
						
							|  |  |  |                     if base is object: | 
					
						
							|  |  |  |                         continue | 
					
						
							|  |  |  |                     elif '__new__' in base.__dict__: | 
					
						
							| 
									
										
										
										
											2018-10-05 23:29:36 -07:00
										 |  |  |                         if issubclass(base, Enum): | 
					
						
							| 
									
										
										
										
											2018-09-21 19:03:09 -07:00
										 |  |  |                             continue | 
					
						
							| 
									
										
										
										
											2020-09-15 16:59:48 -07:00
										 |  |  |                         data_types.append(candidate or base) | 
					
						
							|  |  |  |                         break | 
					
						
							|  |  |  |                     elif not issubclass(base, Enum): | 
					
						
							|  |  |  |                         candidate = base | 
					
						
							|  |  |  |             if len(data_types) > 1: | 
					
						
							| 
									
										
										
										
											2020-09-16 07:35:14 -07:00
										 |  |  |                 raise TypeError('%r: too many data types: %r' % (class_name, data_types)) | 
					
						
							| 
									
										
										
										
											2020-09-15 16:59:48 -07:00
										 |  |  |             elif data_types: | 
					
						
							|  |  |  |                 return data_types[0] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 return None | 
					
						
							| 
									
										
										
										
											2018-09-21 19:03:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # ensure final parent class is an Enum derivative, find any concrete | 
					
						
							|  |  |  |         # data type, and check that Enum has no members | 
					
						
							|  |  |  |         first_enum = bases[-1] | 
					
						
							|  |  |  |         if not issubclass(first_enum, Enum): | 
					
						
							|  |  |  |             raise TypeError("new enumerations should be created as " | 
					
						
							|  |  |  |                     "`EnumName([mixin_type, ...] [data_type,] enum_type)`") | 
					
						
							|  |  |  |         member_type = _find_data_type(bases) or object | 
					
						
							|  |  |  |         if first_enum._member_names_: | 
					
						
							|  |  |  |             raise TypeError("Cannot extend enumerations") | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |         return member_type, first_enum | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							|  |  |  |     def _find_new_(classdict, member_type, first_enum): | 
					
						
							|  |  |  |         """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__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         # now find the correct __new__, checking to see of one was defined | 
					
						
							|  |  |  |         # by the user; also check earlier enum classes in case a __new__ was | 
					
						
							|  |  |  |         # saved as __new_member__ | 
					
						
							|  |  |  |         __new__ = classdict.get('__new__', None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # should __new__ be saved as __new_member__ later? | 
					
						
							|  |  |  |         save_new = __new__ is not None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if __new__ is None: | 
					
						
							|  |  |  |             # check all possibles for __new_member__ before falling back to | 
					
						
							|  |  |  |             # __new__ | 
					
						
							|  |  |  |             for method in ('__new_member__', '__new__'): | 
					
						
							|  |  |  |                 for possible in (member_type, first_enum): | 
					
						
							|  |  |  |                     target = getattr(possible, method, None) | 
					
						
							|  |  |  |                     if target not in { | 
					
						
							|  |  |  |                             None, | 
					
						
							|  |  |  |                             None.__new__, | 
					
						
							|  |  |  |                             object.__new__, | 
					
						
							|  |  |  |                             Enum.__new__, | 
					
						
							|  |  |  |                             }: | 
					
						
							|  |  |  |                         __new__ = target | 
					
						
							|  |  |  |                         break | 
					
						
							|  |  |  |                 if __new__ is not None: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 __new__ = object.__new__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # if a non-object.__new__ is used then whatever value/tuple was | 
					
						
							|  |  |  |         # assigned to the enum member name will be passed to __new__ and to the | 
					
						
							|  |  |  |         # new enum member's __init__ | 
					
						
							|  |  |  |         if __new__ is object.__new__: | 
					
						
							|  |  |  |             use_args = False | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             use_args = True | 
					
						
							|  |  |  |         return __new__, save_new, use_args | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Enum(metaclass=EnumMeta): | 
					
						
							|  |  |  |     """Generic enumeration.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Derive from this class to define new enumerations. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     def __new__(cls, value): | 
					
						
							|  |  |  |         # all enum instances are actually created during class construction | 
					
						
							|  |  |  |         # without calling this method; this method is called by the metaclass' | 
					
						
							|  |  |  |         # __call__ (i.e. Color(3) ), and by pickle | 
					
						
							|  |  |  |         if type(value) is cls: | 
					
						
							| 
									
										
										
										
											2016-11-21 09:22:05 -08:00
										 |  |  |             # For lookups like Color(Color.RED) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |             return value | 
					
						
							|  |  |  |         # by-value search for a matching enum member | 
					
						
							|  |  |  |         # see if it's in the reverse mapping (for hashable values) | 
					
						
							| 
									
										
										
										
											2013-07-19 19:35:56 -07:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2018-12-26 20:45:33 +02:00
										 |  |  |             return cls._value2member_map_[value] | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             # Not found, no need to do long O(n) search | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2013-07-19 19:35:56 -07:00
										 |  |  |         except TypeError: | 
					
						
							|  |  |  |             # not there, now do long search -- O(n) behavior | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |             for member in cls._member_map_.values(): | 
					
						
							| 
									
										
										
										
											2014-09-16 17:31:23 -07:00
										 |  |  |                 if member._value_ == value: | 
					
						
							| 
									
										
										
										
											2013-07-19 19:35:56 -07:00
										 |  |  |                     return member | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         # still not found -- try _missing_ hook | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:34 -07:00
										 |  |  |         try: | 
					
						
							|  |  |  |             exc = None | 
					
						
							|  |  |  |             result = cls._missing_(value) | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             exc = e | 
					
						
							|  |  |  |             result = None | 
					
						
							|  |  |  |         if isinstance(result, cls): | 
					
						
							|  |  |  |             return result | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-07-18 20:37:13 +02:00
										 |  |  |             ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__)) | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:34 -07:00
										 |  |  |             if result is None and exc is None: | 
					
						
							|  |  |  |                 raise ve_exc | 
					
						
							|  |  |  |             elif exc is None: | 
					
						
							|  |  |  |                 exc = TypeError( | 
					
						
							|  |  |  |                         'error in %s._missing_: returned %r instead of None or a valid member' | 
					
						
							|  |  |  |                         % (cls.__name__, result) | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |             exc.__context__ = ve_exc | 
					
						
							|  |  |  |             raise exc | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |     def _generate_next_value_(name, start, count, last_values): | 
					
						
							|  |  |  |         for last_value in reversed(last_values): | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 return last_value + 1 | 
					
						
							|  |  |  |             except TypeError: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |             return start | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |     @classmethod | 
					
						
							|  |  |  |     def _missing_(cls, value): | 
					
						
							| 
									
										
										
										
											2019-07-18 20:37:13 +02:00
										 |  |  |         raise ValueError("%r is not a valid %s" % (value, cls.__qualname__)) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         return "<%s.%s: %r>" % ( | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |                 self.__class__.__name__, self._name_, self._value_) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __str__(self): | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |         return "%s.%s" % (self.__class__.__name__, self._name_) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-12 06:51:41 -07:00
										 |  |  |     def __dir__(self): | 
					
						
							| 
									
										
										
										
											2014-10-14 08:58:32 -07:00
										 |  |  |         added_behavior = [ | 
					
						
							|  |  |  |                 m | 
					
						
							|  |  |  |                 for cls in self.__class__.mro() | 
					
						
							|  |  |  |                 for m in cls.__dict__ | 
					
						
							| 
									
										
										
										
											2015-03-11 08:43:12 -07:00
										 |  |  |                 if m[0] != '_' and m not in self._member_map_ | 
					
						
							| 
									
										
										
										
											2014-10-14 08:58:32 -07:00
										 |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2014-10-21 13:40:35 -07:00
										 |  |  |         return (['__class__', '__doc__', '__module__'] + added_behavior) | 
					
						
							| 
									
										
										
										
											2013-08-12 06:51:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-31 19:17:41 -07:00
										 |  |  |     def __format__(self, format_spec): | 
					
						
							|  |  |  |         # mixed-in Enums should use the mixed-in type's __format__, otherwise | 
					
						
							|  |  |  |         # we can get strange results with the Enum name showing up instead of | 
					
						
							|  |  |  |         # the value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 11:28:37 -07:00
										 |  |  |         # pure Enum branch, or branch with __str__ explicitly overridden | 
					
						
							|  |  |  |         str_overridden = type(self).__str__ != Enum.__str__ | 
					
						
							|  |  |  |         if self._member_type_ is object or str_overridden: | 
					
						
							| 
									
										
										
										
											2013-08-31 19:17:41 -07:00
										 |  |  |             cls = str | 
					
						
							|  |  |  |             val = str(self) | 
					
						
							|  |  |  |         # mix-in branch | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             cls = self._member_type_ | 
					
						
							| 
									
										
										
										
											2014-09-16 17:31:23 -07:00
										 |  |  |             val = self._value_ | 
					
						
							| 
									
										
										
										
											2013-08-31 19:17:41 -07:00
										 |  |  |         return cls.__format__(val, format_spec) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |     def __hash__(self): | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |         return hash(self._name_) | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 11:36:27 -08:00
										 |  |  |     def __reduce_ex__(self, proto): | 
					
						
							| 
									
										
										
										
											2014-02-18 12:37:12 -08:00
										 |  |  |         return self.__class__, (self._value_, ) | 
					
						
							| 
									
										
										
										
											2014-02-08 11:36:27 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-27 23:02:02 -07:00
										 |  |  |     # DynamicClassAttribute is used to provide access to the `name` and | 
					
						
							|  |  |  |     # `value` properties of enum members while keeping some measure of | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |     # protection from modification, while still allowing for an enumeration | 
					
						
							|  |  |  |     # to have members named `name` and `value`.  This works because enumeration | 
					
						
							|  |  |  |     # members are not set directly on the enum class -- __getattr__ is | 
					
						
							|  |  |  |     # used to look them up. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 07:14:41 -07:00
										 |  |  |     @DynamicClassAttribute | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |     def name(self): | 
					
						
							| 
									
										
										
										
											2013-09-15 16:59:35 -07:00
										 |  |  |         """The name of the Enum member.""" | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |         return self._name_ | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 07:14:41 -07:00
										 |  |  |     @DynamicClassAttribute | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  |     def value(self): | 
					
						
							| 
									
										
										
										
											2013-09-15 16:59:35 -07:00
										 |  |  |         """The value of the Enum member.""" | 
					
						
							| 
									
										
										
										
											2013-07-19 19:47:21 -07:00
										 |  |  |         return self._value_ | 
					
						
							| 
									
										
										
										
											2013-06-14 16:55:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class IntEnum(int, Enum): | 
					
						
							|  |  |  |     """Enum where members are also (and must be) ints""" | 
					
						
							| 
									
										
										
										
											2013-07-18 17:05:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 17:27:57 -07:00
										 |  |  | def _reduce_ex_by_name(self, proto): | 
					
						
							|  |  |  |     return self.name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 23:55:19 -07:00
										 |  |  | class Flag(Enum): | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |     """Support for flags""" | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _generate_next_value_(name, start, count, last_values): | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Generate the next value when not given. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         name: the name of the member | 
					
						
							| 
									
										
										
										
											2019-09-21 13:22:54 +08:00
										 |  |  |         start: the initial start value or None | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         count: the number of existing members | 
					
						
							|  |  |  |         last_value: the last value assigned or None | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if not count: | 
					
						
							|  |  |  |             return start if start is not None else 1 | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |         for last_value in reversed(last_values): | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 high_bit = _high_bit(last_value) | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |             except Exception: | 
					
						
							| 
									
										
										
										
											2016-09-10 23:36:59 -07:00
										 |  |  |                 raise TypeError('Invalid Flag value: %r' % last_value) from None | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         return 2 ** (high_bit+1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def _missing_(cls, value): | 
					
						
							|  |  |  |         original_value = value | 
					
						
							|  |  |  |         if value < 0: | 
					
						
							|  |  |  |             value = ~value | 
					
						
							|  |  |  |         possible_member = cls._create_pseudo_member_(value) | 
					
						
							|  |  |  |         if original_value < 0: | 
					
						
							|  |  |  |             possible_member = ~possible_member | 
					
						
							|  |  |  |         return possible_member | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def _create_pseudo_member_(cls, value): | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Create a composite member iff value contains only members. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         pseudo_member = cls._value2member_map_.get(value, None) | 
					
						
							|  |  |  |         if pseudo_member is None: | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |             # verify all bits are accounted for | 
					
						
							|  |  |  |             _, extra_flags = _decompose(cls, value) | 
					
						
							|  |  |  |             if extra_flags: | 
					
						
							| 
									
										
										
										
											2019-07-18 20:37:13 +02:00
										 |  |  |                 raise ValueError("%r is not a valid %s" % (value, cls.__qualname__)) | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |             # construct a singleton enum pseudo-member | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |             pseudo_member = object.__new__(cls) | 
					
						
							|  |  |  |             pseudo_member._name_ = None | 
					
						
							|  |  |  |             pseudo_member._value_ = value | 
					
						
							| 
									
										
										
										
											2017-01-24 12:12:06 -08:00
										 |  |  |             # use setdefault in case another thread already created a composite | 
					
						
							|  |  |  |             # with this value | 
					
						
							|  |  |  |             pseudo_member = cls._value2member_map_.setdefault(value, pseudo_member) | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         return pseudo_member | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __contains__(self, other): | 
					
						
							|  |  |  |         if not isinstance(other, self.__class__): | 
					
						
							| 
									
										
										
										
											2018-09-10 23:51:04 +05:30
										 |  |  |             raise TypeError( | 
					
						
							|  |  |  |                 "unsupported operand type(s) for 'in': '%s' and '%s'" % ( | 
					
						
							|  |  |  |                     type(other).__qualname__, self.__class__.__qualname__)) | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         return other._value_ & self._value_ == other._value_ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         cls = self.__class__ | 
					
						
							|  |  |  |         if self._name_ is not None: | 
					
						
							|  |  |  |             return '<%s.%s: %r>' % (cls.__name__, self._name_, self._value_) | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |         members, uncovered = _decompose(cls, self._value_) | 
					
						
							| 
									
										
										
										
											2016-09-04 11:39:01 -07:00
										 |  |  |         return '<%s.%s: %r>' % ( | 
					
						
							|  |  |  |                 cls.__name__, | 
					
						
							|  |  |  |                 '|'.join([str(m._name_ or m._value_) for m in members]), | 
					
						
							|  |  |  |                 self._value_, | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __str__(self): | 
					
						
							|  |  |  |         cls = self.__class__ | 
					
						
							|  |  |  |         if self._name_ is not None: | 
					
						
							|  |  |  |             return '%s.%s' % (cls.__name__, self._name_) | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |         members, uncovered = _decompose(cls, self._value_) | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         if len(members) == 1 and members[0]._name_ is None: | 
					
						
							|  |  |  |             return '%s.%r' % (cls.__name__, members[0]._value_) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return '%s.%s' % ( | 
					
						
							|  |  |  |                     cls.__name__, | 
					
						
							|  |  |  |                     '|'.join([str(m._name_ or m._value_) for m in members]), | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-02 16:32:32 -07:00
										 |  |  |     def __bool__(self): | 
					
						
							|  |  |  |         return bool(self._value_) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |     def __or__(self, other): | 
					
						
							|  |  |  |         if not isinstance(other, self.__class__): | 
					
						
							|  |  |  |             return NotImplemented | 
					
						
							|  |  |  |         return self.__class__(self._value_ | other._value_) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __and__(self, other): | 
					
						
							|  |  |  |         if not isinstance(other, self.__class__): | 
					
						
							|  |  |  |             return NotImplemented | 
					
						
							|  |  |  |         return self.__class__(self._value_ & other._value_) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __xor__(self, other): | 
					
						
							|  |  |  |         if not isinstance(other, self.__class__): | 
					
						
							|  |  |  |             return NotImplemented | 
					
						
							|  |  |  |         return self.__class__(self._value_ ^ other._value_) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __invert__(self): | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |         members, uncovered = _decompose(self.__class__, self._value_) | 
					
						
							| 
									
										
										
										
											2017-09-26 00:55:55 +03:00
										 |  |  |         inverted = self.__class__(0) | 
					
						
							|  |  |  |         for m in self.__class__: | 
					
						
							|  |  |  |             if m not in members and not (m._value_ & self._value_): | 
					
						
							|  |  |  |                 inverted = inverted | m | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         return self.__class__(inverted) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 23:55:19 -07:00
										 |  |  | class IntFlag(int, Flag): | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |     """Support for integer-based Flags""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |     @classmethod | 
					
						
							|  |  |  |     def _missing_(cls, value): | 
					
						
							|  |  |  |         if not isinstance(value, int): | 
					
						
							| 
									
										
										
										
											2019-07-18 20:37:13 +02:00
										 |  |  |             raise ValueError("%r is not a valid %s" % (value, cls.__qualname__)) | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |         new_member = cls._create_pseudo_member_(value) | 
					
						
							|  |  |  |         return new_member | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |     @classmethod | 
					
						
							|  |  |  |     def _create_pseudo_member_(cls, value): | 
					
						
							|  |  |  |         pseudo_member = cls._value2member_map_.get(value, None) | 
					
						
							|  |  |  |         if pseudo_member is None: | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |             need_to_create = [value] | 
					
						
							|  |  |  |             # get unaccounted for bits | 
					
						
							|  |  |  |             _, extra_flags = _decompose(cls, value) | 
					
						
							|  |  |  |             # timer = 10 | 
					
						
							|  |  |  |             while extra_flags: | 
					
						
							|  |  |  |                 # timer -= 1 | 
					
						
							|  |  |  |                 bit = _high_bit(extra_flags) | 
					
						
							|  |  |  |                 flag_value = 2 ** bit | 
					
						
							|  |  |  |                 if (flag_value not in cls._value2member_map_ and | 
					
						
							|  |  |  |                         flag_value not in need_to_create | 
					
						
							|  |  |  |                         ): | 
					
						
							|  |  |  |                     need_to_create.append(flag_value) | 
					
						
							|  |  |  |                 if extra_flags == -flag_value: | 
					
						
							|  |  |  |                     extra_flags = 0 | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     extra_flags ^= flag_value | 
					
						
							|  |  |  |             for value in reversed(need_to_create): | 
					
						
							|  |  |  |                 # construct singleton pseudo-members | 
					
						
							|  |  |  |                 pseudo_member = int.__new__(cls, value) | 
					
						
							|  |  |  |                 pseudo_member._name_ = None | 
					
						
							|  |  |  |                 pseudo_member._value_ = value | 
					
						
							| 
									
										
										
										
											2017-01-24 12:12:06 -08:00
										 |  |  |                 # use setdefault in case another thread already created a composite | 
					
						
							|  |  |  |                 # with this value | 
					
						
							|  |  |  |                 pseudo_member = cls._value2member_map_.setdefault(value, pseudo_member) | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  |         return pseudo_member | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __or__(self, other): | 
					
						
							|  |  |  |         if not isinstance(other, (self.__class__, int)): | 
					
						
							|  |  |  |             return NotImplemented | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |         result = self.__class__(self._value_ | self.__class__(other)._value_) | 
					
						
							|  |  |  |         return result | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __and__(self, other): | 
					
						
							|  |  |  |         if not isinstance(other, (self.__class__, int)): | 
					
						
							|  |  |  |             return NotImplemented | 
					
						
							|  |  |  |         return self.__class__(self._value_ & self.__class__(other)._value_) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __xor__(self, other): | 
					
						
							|  |  |  |         if not isinstance(other, (self.__class__, int)): | 
					
						
							|  |  |  |             return NotImplemented | 
					
						
							|  |  |  |         return self.__class__(self._value_ ^ self.__class__(other)._value_) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     __ror__ = __or__ | 
					
						
							|  |  |  |     __rand__ = __and__ | 
					
						
							|  |  |  |     __rxor__ = __xor__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __invert__(self): | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |         result = self.__class__(~self._value_) | 
					
						
							|  |  |  |         return result | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _high_bit(value): | 
					
						
							| 
									
										
										
										
											2016-09-02 15:50:21 -07:00
										 |  |  |     """returns index of highest bit, or -1 if value is zero or negative""" | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |     return value.bit_length() - 1 | 
					
						
							| 
									
										
										
										
											2016-08-31 00:12:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-18 17:05:39 -07:00
										 |  |  | def unique(enumeration): | 
					
						
							|  |  |  |     """Class decorator for enumerations ensuring unique member values.""" | 
					
						
							|  |  |  |     duplicates = [] | 
					
						
							|  |  |  |     for name, member in enumeration.__members__.items(): | 
					
						
							|  |  |  |         if name != member.name: | 
					
						
							|  |  |  |             duplicates.append((name, member.name)) | 
					
						
							|  |  |  |     if duplicates: | 
					
						
							|  |  |  |         alias_details = ', '.join( | 
					
						
							|  |  |  |                 ["%s -> %s" % (alias, name) for (alias, name) in duplicates]) | 
					
						
							|  |  |  |         raise ValueError('duplicate values found in %r: %s' % | 
					
						
							|  |  |  |                 (enumeration, alias_details)) | 
					
						
							|  |  |  |     return enumeration | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _decompose(flag, value): | 
					
						
							|  |  |  |     """Extract all members from the value.""" | 
					
						
							|  |  |  |     # _decompose is only called if the value is not named | 
					
						
							|  |  |  |     not_covered = value | 
					
						
							|  |  |  |     negative = value < 0 | 
					
						
							|  |  |  |     members = [] | 
					
						
							| 
									
										
										
										
											2019-11-27 06:36:02 +08:00
										 |  |  |     for member in flag: | 
					
						
							|  |  |  |         member_value = member.value | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |         if member_value and member_value & value == member_value: | 
					
						
							|  |  |  |             members.append(member) | 
					
						
							|  |  |  |             not_covered &= ~member_value | 
					
						
							| 
									
										
										
										
											2019-11-27 06:36:02 +08:00
										 |  |  |     if not negative: | 
					
						
							|  |  |  |         tmp = not_covered | 
					
						
							|  |  |  |         while tmp: | 
					
						
							|  |  |  |             flag_value = 2 ** _high_bit(tmp) | 
					
						
							|  |  |  |             if flag_value in flag._value2member_map_: | 
					
						
							|  |  |  |                 members.append(flag._value2member_map_[flag_value]) | 
					
						
							|  |  |  |                 not_covered &= ~flag_value | 
					
						
							|  |  |  |             tmp &= ~flag_value | 
					
						
							| 
									
										
										
										
											2016-09-18 13:15:41 -07:00
										 |  |  |     if not members and value in flag._value2member_map_: | 
					
						
							|  |  |  |         members.append(flag._value2member_map_[value]) | 
					
						
							|  |  |  |     members.sort(key=lambda m: m._value_, reverse=True) | 
					
						
							|  |  |  |     if len(members) > 1 and members[0].value == value: | 
					
						
							|  |  |  |         # we have the breakdown, don't need the value member itself | 
					
						
							|  |  |  |         members.pop(0) | 
					
						
							|  |  |  |     return members, not_covered |