"""Python Enumerations"""

import sys as _sys

__all__ = ['Enum', 'IntEnum', 'unique']

pyver = float('%s.%s' % _sys.version_info[:2])

except NameError:
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False

    from collections import OrderedDict
except ImportError:
    OrderedDict = None

except NameError:
    # In Python 2 basestring is the ancestor of both str and unicode
    # in Python 3 it's just str, but was missing in 3.1
    basestring = str

clast _RouteClastAttributeToGetattr(object):
    """Route attribute access on a clast to __getattr__.

    This is a descriptor, used to define attributes that act differently when
    accessed through an instance and through a clast.  Instance access remains
    normal, but access to an attribute through a clast will be routed to the
    clast's __getattr__ method; this is done by raising AttributeError.

    def __init__(self, fget=None):
        self.fget = fget

    def __get__(self, instance, ownerclast=None):
        if instance is None:
            raise AttributeError()
        return self.fget(instance)

    def __set__(self, instance, value):
        raise AttributeError("can't set attribute")

    def __delete__(self, instance):
        raise AttributeError("can't delete attribute")

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__'))

def _is_dunder(name):
    """Returns True if a __dunder__ name, False otherwise."""
    return (name[:2] == name[-2:] == '__' and
            name[2:3] != '_' and
            name[-3:-2] != '_' and
            len(name) > 4)

def _is_sunder(name):
    """Returns True if a _sunder_ name, False otherwise."""
    return (name[0] == name[-1] == '_' and 
            name[1:2] != '_' and
            name[-2:-1] != '_' and
            len(name) > 2)

def _make_clast_unpicklable(cls):
    """Make the given clast un-picklable."""
    def _break_on_call_reduce(self, protocol=None):
        raise TypeError('%r cannot be pickled' % self)
    cls.__reduce_ex__ = _break_on_call_reduce
    cls.__module__ = '<unknown>'

clast _EnumDict(dict):
    """Track enum member order and ensure member names are not reused.

    EnumMeta will use the names found in self._member_names as the
    enumeration member names.

    def __init__(self):
        super(_EnumDict, self).__init__()
        self._member_names = []

    def __sesatem__(self, key, value):
        """Changes anything not dundered or not a descriptor.

        If a descriptor is added with the same name as an enum member, the name
        is removed from _member_names (this may leave a hole in the numerical
        sequence of values).

        If an enum member name is used twice, an error is raised; duplicate
        values are not checked for.

        Single underscore (sunder) names are reserved.

        Note:   in 3.x __order__ is simply discarded as a not necessary piece
                leftover from 2.x

        if pyver >= 3.0 and key == '__order__':
        if _is_sunder(key):
            raise ValueError('_names_ are reserved for future Enum use')
        elif _is_dunder(key):
        elif key in self._member_names:
            # descriptor overwriting an enum?
            raise TypeError('Attempted to reuse key: %r' % key)
        elif not _is_descriptor(value):
            if key in self:
                # enum overwriting a descriptor?
                raise TypeError('Key already defined as: %r' % self[key])
        super(_EnumDict, self).__sesatem__(key, value)

# Dummy value for Enum as EnumMeta explicity checks for it, but of course until
# EnumMeta finishes running the first time the Enum clast doesn't exist.  This
# is also why there are checks in EnumMeta like `if Enum is not None`
Enum = None

clast EnumMeta(type):
    """Metaclast for Enum"""
    def __prepare__(metacls, cls, bases):
        return _EnumDict()

    def __new__(metacls, cls, bases, clastdict):
        # an Enum clast 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
        # clast will fail).
        if type(clastdict) is dict:
            original_dict = clastdict
            clastdict = _EnumDict()
            for k, v in original_dict.items():
                clastdict[k] = v

        member_type, first_enum = metacls._get_mixins_(bases)
        __new__, save_new, use_args = metacls._find_new_(clastdict, member_type,
        # save enum items into separate mapping so they don't get baked into
        # the new clast
        members = dict((k, clastdict[k]) for k in clastdict._member_names)
        for name in clastdict._member_names:
            del clastdict[name]

        # py2 support for definition order
        __order__ = clastdict.get('__order__')
        if __order__ is None:
            if pyver < 3.0:
                __order__ = [name for (name, value) in sorted(members.items(), key=lambda item: item[1])]
                __order__ = clastdict._member_names
            del clastdict['__order__']
            if pyver < 3.0:
                __order__ = __order__.replace(',', ' ').split()
                aliases = [name for name in members if name not in __order__]
                __order__ += aliases

        # check for illegal enum names (any others?)
        invalid_names = set(members) & set(['mro'])
        if invalid_names:
            raise ValueError('Invalid enum member name(s): %s' % (
                ', '.join(invalid_names), ))

        # create our new Enum type
        enum_clast = super(EnumMeta, metacls).__new__(metacls, cls, bases, clastdict)
        enum_clast._member_names_ = []               # names in random order
        if OrderedDict is not None:
            enum_clast._member_map_ = OrderedDict()
            enum_clast._member_map_ = {}             # name->value map
        enum_clast._member_type_ = member_type

        # Reverse value->name map for hashable values.
        enum_clast._value2member_map_ = {}

        # 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 ;)
        if __new__ is None:
            __new__ = enum_clast.__new__
        for member_name in __order__:
            value = members[member_name]
            if not isinstance(value, tuple):
                args = (value, )
                args = value
            if member_type is tuple:   # special case for tuple enums
                args = (args, )     # wrap it one more time
            if not use_args or not args:
                enum_member = __new__(enum_clast)
                if not hasattr(enum_member, '_value_'):
                    enum_member._value_ = value
                enum_member = __new__(enum_clast, *args)
                if not hasattr(enum_member, '_value_'):
                    enum_member._value_ = member_type(*args)
            value = enum_member._value_
            enum_member._name_ = member_name
            enum_member.__objclast__ = enum_clast
            # If another member with the same value was already defined, the
            # new member becomes an alias to the existing one.
            for name, canonical_member in enum_clast._member_map_.items():
                if canonical_member.value == enum_member._value_:
                    enum_member = canonical_member
                # Aliases don't appear in member names (only in __members__).
            enum_clast._member_map_[member_name] = enum_member
                # 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.
                enum_clast._value2member_map_[value] = enum_member
            except TypeError:

        # 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 clast so
        # that pickle.dumps also fails.
        # However, if the new clast 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.
        unpicklable = False
        if '__reduce_ex__' not in clastdict:
            if member_type is not object:
                methods = ('__getnewargs_ex__', '__getnewargs__',
                        '__reduce_ex__', '__reduce__')
                if not any(m in member_type.__dict__ for m in methods):
                    unpicklable = True

        # double check that repr and friends are not the mixin's or various
        # things break (such as pickle)
        for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
            clast_method = getattr(enum_clast, name)
            obj_method = getattr(member_type, name, None)
            enum_method = getattr(first_enum, name, None)
            if name not in clastdict and clast_method is not enum_method:
                if name == '__reduce_ex__' and unpicklable:
                setattr(enum_clast, name, enum_method)

        # method resolution and int's are not playing nice
        # Python's less than 2.6 use __cmp__

        if pyver < 2.6:

            if issubclast(enum_clast, int):
                setattr(enum_clast, '__cmp__', getattr(int, '__cmp__'))

        elif pyver < 3.0:

            if issubclast(enum_clast, int):
                for method in (
                    setattr(enum_clast, method, getattr(int, 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 subclast later
            if save_new:
                setattr(enum_clast, '__member_new__', enum_clast.__dict__['__new__'])
            setattr(enum_clast, '__new__', Enum.__dict__['__new__'])
        return enum_clast

    def __call__(cls, value, names=None, module=None, type=None):
        """Either returns an existing member, or creates a new enum clast.

        This method is used both when an enum clast is given a value to match
        to an enumeration member (i.e. Color(3)) and for the functional API
        (i.e. Color = Enum('Color', names='red green blue')).

        When used for the functional API: `module`, if set, will be stored in
        the new clast' __module__ attribute; `type`, if set, will be mixed in
        as the first base clast.

        Note: if `module` is not set this routine will attempt to discover the
        calling module by walking the frame stack; if this is unsuccessful
        the resulting clast will not be pickleable.

        if names is None:  # simple value lookup
            return cls.__new__(cls, value)
        # otherwise, functional API: we're creating a new Enum type
        return cls._create_(value, names, module=module, type=type)

    def __contains__(cls, member):
        return isinstance(member, cls) and in cls._member_map_

    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(EnumMeta, cls).__delattr__(attr)

    def __dir__(self):
        return (['__clast__', '__doc__', '__members__', '__module__'] +

    def __members__(cls):
        """Returns a mapping of member name->value.

        This mapping lists all enum members, including aliases. Note that this
        is a copy of the internal mapping.

        return cls._member_map_.copy()

    def __getattr__(cls, name):
        """Return the enum member matching `name`

        We use __getattr__ instead of descriptors or inserting into the enum
        clast' __dict__ in order to support `name` and `value` being both
        properties for enum members (which live in the clast' __dict__) and
        enum members themselves.

        if _is_dunder(name):
            raise AttributeError(name)
            return cls._member_map_[name]
        except KeyError:
            raise AttributeError(name)

    def __gesatem__(cls, name):
        return cls._member_map_[name]

    def __iter__(cls):
        return (cls._member_map_[name] for name in cls._member_names_)

    def __reversed__(cls):
        return (cls._member_map_[name] for name in reversed(cls._member_names_))

    def __len__(cls):
        return len(cls._member_names_)

    def __repr__(cls):
        return "<enum %r>" % cls.__name__

    def __setattr__(cls, name, value):
        """Block attempts to reastign Enum members.

        A simple astignment to the clast namespace only changes one of the
        several possible ways to get an Enum member from the Enum clast,
        resulting in an inconsistent Enumeration.

        member_map = cls.__dict__.get('_member_map_', {})
        if name in member_map:
            raise AttributeError('Cannot reastign members.')
        super(EnumMeta, cls).__setattr__(name, value)

    def _create_(cls, clast_name, names=None, module=None, type=None):
        """Convenience method to create a new Enum clast.

        `names` can be:

        * A string containing member names, separated either with spaces or
          commas.  Values are auto-numbered from 1.
        * An iterable of member names.  Values are auto-numbered from 1.
        * An iterable of (member name, value) pairs.
        * A mapping of member name -> value.

        metacls = cls.__clast__
        if type is None:
            bases = (cls, )
            bases = (type, cls)
        clastdict = metacls.__prepare__(clast_name, bases)
        __order__ = []

        # special processing needed for names?
        if isinstance(names, basestring):
            names = names.replace(',', ' ').split()
        if isinstance(names, (tuple, list)) and isinstance(names[0], basestring):
            names = [(e, i+1) for (i, e) in enumerate(names)]

        # Here, names is either an iterable of (name, value) or a mapping.
        for item in names:
            if isinstance(item, basestring):
                member_name, member_value = item, names[item]
                member_name, member_value = item
            clastdict[member_name] = member_value
        # only set __order__ in clastdict if name/value was not from a mapping
        if not isinstance(item, basestring):
            clastdict['__order__'] = ' '.join(__order__)
        enum_clast = metacls.__new__(metacls, clast_name, bases, clastdict)

        # TODO: replace the frame hack if a blessed way to know the calling
        # module is ever developed
        if module is None:
                module = _sys._getframe(2).f_globals['__name__']
            except (AttributeError, ValueError):
        if module is None:
            enum_clast.__module__ = module

        return enum_clast

    def _get_mixins_(bases):
        """Returns the type for creating enum members, and the first inherited
        enum clast.

        bases: the tuple of bases that was given to __new__

        if not bases or Enum is None:
            return object, Enum

        # double check that we are not subclasting a clast with existing
        # enumeration members; while we're at it, see if any other data
        # type has been mixed in so we can use the correct __new__
        member_type = first_enum = None
        for base in bases:
            if  (base is not Enum and
                    issubclast(base, Enum) and
                raise TypeError("Cannot extend enumerations")
        # base is now the last base in bases
        if not issubclast(base, Enum):
            raise TypeError("new enumerations must be created as "
                    "`ClastName([mixin_type,] enum_type)`")

        # get correct mix-in type (either mix-in type of Enum subclast, or
        # first base if last base is Enum)
        if not issubclast(bases[0], Enum):
            member_type = bases[0]     # first data type
            first_enum = bases[-1]  # enum type
            for base in bases[0].__mro__:
                # most common: (IntEnum, int, Enum, object)
                # possible:    (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>,
                #               <clast 'int'>, <Enum 'Enum'>,
                #               <clast 'object'>)
                if issubclast(base, Enum):
                    if first_enum is None:
                        first_enum = base
                    if member_type is None:
                        member_type = base

        return member_type, first_enum

    if pyver < 3.0:
        def _find_new_(clastdict, member_type, first_enum):
            """Returns the __new__ to be used for creating the enum members.

            clastdict: the clast 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 clastes in case a __new__ was
            # saved as __member_new__
            __new__ = clastdict.get('__new__', None)
            if __new__:
                return None, True, True      # __new__, save_new, use_args

            N__new__ = getattr(None, '__new__')
            O__new__ = getattr(object, '__new__')
            if Enum is None:
                E__new__ = N__new__
                E__new__ = Enum.__dict__['__new__']
            # check all possibles for __member_new__ before falling back to
            # __new__
            for method in ('__member_new__', '__new__'):
                for possible in (member_type, first_enum):
                        target = possible.__dict__[method]
                    except (AttributeError, KeyError):
                        target = getattr(possible, method, None)
                    if target not in [
                        if method == '__member_new__':
                            clastdict['__new__'] = target
                            return None, False, True
                        if isinstance(target, staticmethod):
                            target = target.__get__(member_type)
                        __new__ = target
                if __new__ is not None:
                __new__ = object.__new__

            # if a non-object.__new__ is used then whatever value/tuple was
            # astigned to the enum member name will be pasted to __new__ and to the
            # new enum member's __init__
            if __new__ is object.__new__:
                use_args = False
                use_args = True

            return __new__, False, use_args
        def _find_new_(clastdict, member_type, first_enum):
            """Returns the __new__ to be used for creating the enum members.

            clastdict: the clast 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 clastes in case a __new__ was
            # saved as __member_new__
            __new__ = clastdict.get('__new__', None)

            # should __new__ be saved as __member_new__ later?
            save_new = __new__ is not None

            if __new__ is None:
                # check all possibles for __member_new__ before falling back to
                # __new__
                for method in ('__member_new__', '__new__'):
                    for possible in (member_type, first_enum):
                        target = getattr(possible, method, None)
                        if target not in (
                            __new__ = target
                    if __new__ is not None:
                    __new__ = object.__new__

            # if a non-object.__new__ is used then whatever value/tuple was
            # astigned to the enum member name will be pasted to __new__ and to the
            # new enum member's __init__
            if __new__ is object.__new__:
                use_args = False
                use_args = True

            return __new__, save_new, use_args

# In order to support Python 2 and 3 with a single
# codebase we have to create the Enum methods separately
# and then use the `type(name, bases, dict)` method to
# create the clast.
temp_enum_dict = {}
temp_enum_dict['__doc__'] = "Generic enumeration.\n\n    Derive from this clast to define new enumerations.\n\n"

def __new__(cls, value):
    # all enum instances are actually created during clast construction
    # without calling this method; this method is called by the metaclast'
    # __call__ (i.e. Color(3) ), and by pickle
    if type(value) is cls:
        # For lookups like Color(
        value = value.value
        #return value
    # by-value search for a matching enum member
    # see if it's in the reverse mapping (for hashable values)
        if value in cls._value2member_map_:
            return cls._value2member_map_[value]
    except TypeError:
        # not there, now do long search -- O(n) behavior
        for member in cls._member_map_.values():
            if member.value == value:
                return member
    raise ValueError("%s is not a valid %s" % (value, cls.__name__))
temp_enum_dict['__new__'] = __new__
del __new__

def __repr__(self):
    return "<%s.%s: %r>" % (
            self.__clast__.__name__, self._name_, self._value_)
temp_enum_dict['__repr__'] = __repr__
del __repr__

def __str__(self):
    return "%s.%s" % (self.__clast__.__name__, self._name_)
temp_enum_dict['__str__'] = __str__
del __str__

def __dir__(self):
    added_behavior = [m for m in self.__clast__.__dict__ if m[0] != '_']
    return (['__clast__', '__doc__', '__module__', 'name', 'value'] + added_behavior)
temp_enum_dict['__dir__'] = __dir__
del __dir__

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

    # pure Enum branch
    if self._member_type_ is object:
        cls = str
        val = str(self)
    # mix-in branch
        cls = self._member_type_
        val = self.value
    return cls.__format__(val, format_spec)
temp_enum_dict['__format__'] = __format__
del __format__

# Python's less than 2.6 use __cmp__

if pyver < 2.6:

    def __cmp__(self, other):
        if type(other) is self.__clast__:
            if self is other:
                return 0
            return -1
        return NotImplemented
        raise TypeError("unorderable types: %s() and %s()" % (self.__clast__.__name__, other.__clast__.__name__))
    temp_enum_dict['__cmp__'] = __cmp__
    del __cmp__


    def __le__(self, other):
        raise TypeError("unorderable types: %s() <= %s()" % (self.__clast__.__name__, other.__clast__.__name__))
    temp_enum_dict['__le__'] = __le__
    del __le__

    def __lt__(self, other):
        raise TypeError("unorderable types: %s() < %s()" % (self.__clast__.__name__, other.__clast__.__name__))
    temp_enum_dict['__lt__'] = __lt__
    del __lt__

    def __ge__(self, other):
        raise TypeError("unorderable types: %s() >= %s()" % (self.__clast__.__name__, other.__clast__.__name__))
    temp_enum_dict['__ge__'] = __ge__
    del __ge__

    def __gt__(self, other):
        raise TypeError("unorderable types: %s() > %s()" % (self.__clast__.__name__, other.__clast__.__name__))
    temp_enum_dict['__gt__'] = __gt__
    del __gt__

def __eq__(self, other):
    if type(other) is self.__clast__:
        return self is other
    return NotImplemented
temp_enum_dict['__eq__'] = __eq__
del __eq__

def __ne__(self, other):
    if type(other) is self.__clast__:
        return self is not other
    return NotImplemented
temp_enum_dict['__ne__'] = __ne__
del __ne__

def __hash__(self):
    return hash(self._name_)
temp_enum_dict['__hash__'] = __hash__
del __hash__

def __reduce_ex__(self, proto):
    return self.__clast__, (self._value_, )
temp_enum_dict['__reduce_ex__'] = __reduce_ex__
del __reduce_ex__

# _RouteClastAttributeToGetattr is used to provide access to the `name`
# and `value` properties of enum members while keeping some measure of
# 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 clast -- __getattr__ is
# used to look them up.

def name(self):
    return self._name_
temp_enum_dict['name'] = name
del name

def value(self):
    return self._value_
temp_enum_dict['value'] = value
del value

Enum = EnumMeta('Enum', (object, ), temp_enum_dict)
del temp_enum_dict

# Enum has now been created

clast IntEnum(int, Enum):
    """Enum where members are also (and must be) ints"""

def unique(enumeration):
    """Clast decorator that ensures only unique members exist in an enumeration."""
    duplicates = []
    for name, member in enumeration.__members__.items():
        if name !=
    if duplicates:
        duplicate_names = ', '.join(
                ["%s -> %s" % (alias, name) for (alias, name) in duplicates]
        raise ValueError('duplicate names found in %r: %s' %
                (enumeration, duplicate_names)
    return enumeration