/usr/share/arm/util/enum.py is in tor-arm 1.4.5.0-1.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 | """
Basic enumeration, providing ordered types for collections. These can be
constructed as simple type listings, ie:
>>> insects = Enum("ANT", "WASP", "LADYBUG", "FIREFLY")
>>> insects.ANT
'Ant'
>>> insects.values()
['Ant', 'Wasp', 'Ladybug', 'Firefly']
with overwritten string counterparts:
>>> pets = Enum(("DOG", "Skippy"), "CAT", ("FISH", "Nemo"))
>>> pets.DOG
'Skippy'
>>> pets.CAT
'Cat'
or with entirely custom string components as an unordered enum with:
>>> pets = LEnum(DOG="Skippy", CAT="Kitty", FISH="Nemo")
>>> pets.CAT
'Kitty'
"""
def toCamelCase(label):
"""
Converts the given string to camel case, ie:
>>> toCamelCase("I_LIKE_PEPPERJACK!")
'I Like Pepperjack!'
Arguments:
label - input string to be converted
"""
words = []
for entry in label.split("_"):
if len(entry) == 0: words.append("")
elif len(entry) == 1: words.append(entry.upper())
else: words.append(entry[0].upper() + entry[1:].lower())
return " ".join(words)
class Enum:
"""
Basic enumeration.
"""
def __init__(self, *args):
self.orderedValues = []
for entry in args:
if isinstance(entry, str):
key, val = entry, toCamelCase(entry)
elif isinstance(entry, tuple) and len(entry) == 2:
key, val = entry
else: raise ValueError("Unrecognized input: %s" % args)
self.__dict__[key] = val
self.orderedValues.append(val)
def values(self):
"""
Provides an ordered listing of the enumerations in this set.
"""
return list(self.orderedValues)
def indexOf(self, value):
"""
Provides the index of the given value in the collection. This raises a
ValueError if no such element exists.
Arguments:
value - entry to be looked up
"""
return self.orderedValues.index(value)
def next(self, value):
"""
Provides the next enumeration after the given value, raising a ValueError
if no such enum exists.
Arguments:
value - enumeration for which to get the next entry
"""
if not value in self.orderedValues:
raise ValueError("No such enumeration exists: %s (options: %s)" % (value, ", ".join(self.orderedValues)))
nextIndex = (self.orderedValues.index(value) + 1) % len(self.orderedValues)
return self.orderedValues[nextIndex]
def previous(self, value):
"""
Provides the previous enumeration before the given value, raising a
ValueError if no such enum exists.
Arguments:
value - enumeration for which to get the previous entry
"""
if not value in self.orderedValues:
raise ValueError("No such enumeration exists: %s (options: %s)" % (value, ", ".join(self.orderedValues)))
prevIndex = (self.orderedValues.index(value) - 1) % len(self.orderedValues)
return self.orderedValues[prevIndex]
class LEnum(Enum):
"""
Enumeration that accepts custom string mappings.
"""
def __init__(self, **args):
Enum.__init__(self)
self.__dict__.update(args)
self.orderedValues = sorted(args.values())
|