/usr/lib/python3/dist-packages/pyutilib/enum/enum.py is in python3-pyutilib 5.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | # -*- coding: utf-8 -*-
# enum.py
# Part of enum, a package providing enumerated types for Python.
#
# Copyright © 2007 Ben Finney
# This is free software; you may copy, modify and/or distribute this work
# under the terms of the GNU General Public License, version 2 or later
# or, at your option, the terms of the Python license.
#
# _________________________________________________________________________
#
# PyUtilib: A Python utility library.
# Copyright (c) 2008 Sandia Corporation.
# This software is distributed under the BSD License.
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
# _________________________________________________________________________
#
# William Hart
# Edits to the PyPI enum package to support __call__ and pickling.
#
""" Robust enumerated type support in Python.
This package provides a module for robust enumerations in Python.
An enumeration object is created with a sequence of string arguments
to the Enum() constructor::
>>> from enum import Enum
>>> Colours = Enum('red', 'blue', 'green')
>>> Weekdays = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
The return value is an immutable sequence object with a value for each
of the string arguments. Each value is also available as an attribute
named from the corresponding string argument::
>>> pizza_night = Weekdays[4]
>>> shirt_colour = Colours.green
The values are constants that can be compared only with values from
the same enumeration; comparison with other values will invoke
Python's fallback comparisons::
>>> pizza_night == Weekdays.fri
True
>>> shirt_colour > Colours.red
True
>>> shirt_colour == "green"
False
Each value from an enumeration exports its sequence index
as an integer, and can be coerced to a simple string matching the
original arguments used to create the enumeration::
>>> str(pizza_night)
'fri'
>>> shirt_colour.index
2
"""
import six
class EnumException(Exception):
""" Base class for all exceptions in this module. """
def __init__(self):
if self.__class__ is EnumException:
raise NotImplementedError("%s is an abstract class for subclassing" % self.__class__)
class EnumEmptyError(AssertionError, EnumException):
""" Raised when attempting to create an empty enumeration. """
def __str__(self):
return "Enumerations cannot be empty"
class EnumBadKeyError(TypeError, EnumException):
""" Raised when creating an Enum with non-string keys. """
def __init__(self, key):
self.key = key
def __str__(self):
return ("Enumeration keys must be strings or "
"instances of EnumValue: %s" % (self.key,))
class EnumBadIndexError(AssertionError, EnumException):
""" Raised when creating an Enum with with an invalid index."""
def __init__(self, index, reason):
self.index = index
self.reason = reason
def __str__(self):
return ("Enumeration index (%s) is not valid. Reason: %s"
% (self.index, self.reason))
class EnumBadTypeError(TypeError, EnumException):
""" Raised when creating an Enum with a bad type value."""
def __init__(self, type_, reason):
self.type_ = type_
self.reason = reason
def __str__(self):
return ("Enumeration type=%r is not valid. Reason: %s"
% (self.type_, self.reason))
class EnumImmutableError(TypeError, EnumException):
""" Raised when attempting to modify an Enum. """
def __init__(self, *args):
self.args = args
def __str__(self):
return "Enumeration does not allow modification"
class EnumValue(object):
""" A specific value of an enumerated type. """
def __init__(self, enumtype, index, key):
""" Set up a new instance. """
self.__enumtype = enumtype
self.__index = index
self.__key = key
def __get_enumtype(self):
return self.__enumtype
enumtype = property(__get_enumtype)
def __get_key(self):
return self.__key
key = property(__get_key)
def __str__(self):
return "%s" % (self.key)
def __get_index(self):
return self.__index
index = property(__get_index)
def __repr__(self):
return "EnumValue(%s, %s, %s)" % (
repr(self.__enumtype),
repr(self.__index),
repr(self.__key),
)
def __hash__(self):
return hash(self.__index)
def __lt__(self,other):
"""Less than operator
(Called in response to 'self < other' or 'other > self'.)
"""
try:
return self.index < other.index
except Exception:
return NotImplemented
def __gt__(self,other):
"""Greater than operator
(Called in response to 'self > other' or 'other < self'.)
"""
try:
return self.index > other.index
except Exception:
return NotImplemented
def __le__(self,other):
"""Less than or equal operator
(Called in response to 'self <= other' or 'other >= self'.)
"""
try:
return self.index <= other.index
except Exception:
return NotImplemented
def __ge__(self,other):
"""Greater than or equal operator
(Called in response to 'self >= other' or 'other <= self'.)
"""
try:
return self.index >= other.index
except Exception:
return NotImplemented
def __eq__(self,other):
"""Equal to operator
(Called in response to 'self == other'.)
"""
try:
return self.index == other.index
except Exception:
return NotImplemented
def __ne__(self,other):
"""Equal to operator
(Called in response to 'self != other'.)
"""
try:
return self.index != other.index
except Exception:
return NotImplemented
class Enum(object):
""" Enumerated type. """
def __init__(self, *keys, **kwargs):
""" Create an enumeration instance. """
if not keys:
raise EnumEmptyError()
keys = tuple(keys)
values = [None] * len(keys)
value_type = kwargs.get('value_type', EnumValue)
for i, key in enumerate(keys):
if isinstance(key, six.string_types):
value = value_type(self, i, key)
else:
if not isinstance(key, EnumValue):
raise EnumBadKeyError(key)
value = key
key = value.key
if value.index != i:
raise EnumBadIndexError(
value.index,
"Assigned index for argument with key %s, "
"does not match location in the initialization list"
% (key))
values[i] = value
if value.enumtype != values[0].enumtype:
raise EnumBadTypeError(
value.enumtype,
"Type assigned to positional argument %s (key=%s) "
"does not match the type assigned to the first "
"positional argument (key=%s): %r"
% (i, key, values[0].key, values[0].enumtype))
try:
super(Enum, self).__setattr__(key, value)
except TypeError:
raise EnumBadKeyError(key)
super(Enum, self).__setattr__('_keys', keys)
super(Enum, self).__setattr__('_values', values)
def __call__(self, index):
#
# If the index is an integer, get the item
# with this index
#
if isinstance(index, int):
return self[index]
#
# If the index is not a string, then try coercing it
#
if not isinstance(index, six.string_types):
tmp = str(index)
else:
tmp=index
return getattr(self,tmp)
def __setattr__(self, name, value):
raise EnumImmutableError(name)
def __delattr__(self, name):
raise EnumImmutableError(name)
def __len__(self):
return len(self._values)
def __getitem__(self, index):
return self._values[index]
def __setitem__(self, index, value):
raise EnumImmutableError(index)
def __delitem__(self, index):
raise EnumImmutableError(index)
def __iter__(self):
return iter(self._values)
def __contains__(self, value):
is_member = False
if isinstance(value, six.string_types):
is_member = (value in self._keys)
else:
try:
is_member = (value in self._values)
except Exception:
is_member = False
return is_member
|