blob: 9157a2aeda3f7573cdecfb779240baf63924c356 [file] [log] [blame]
A R Karthicka2e53d62016-02-19 17:38:30 -08001#!python
2import copy
3import pprint
4pf = pprint.pformat
5
6class EnumException(Exception):
7 pass
8class Enumeration(object):
9 def __init__(self, name, enumList, valuesAreUnique=False, startValue=0):
10 self.__doc__ = name
11 self.uniqueVals = valuesAreUnique
12 self.lookup = {}
13 self.reverseLookup = {}
14
15 self._addEnums(enumList, startValue)
16
17 def _addEnums(self, enumList, startValue):
18 i = startValue
19 for x in enumList:
20 if type(x) is tuple:
21 try:
22 x, i = x
23 except ValueError:
24 raise EnumException, "tuple doesn't have 2 items: %r" % (x,)
25 if type(x) is not str:
26 raise EnumException, "enum name is not a string: %r" % (x,)
27 if x in self.lookup:
28 raise EnumException, "enum name is not unique: %r" % (x,)
29 if self.uniqueVals and i in self.reverseLookup:
30 raise EnumException, "enum value %r not unique for %r" % (i, x)
31 self.lookup[x] = i
32 self.reverseLookup[i] = x
33
34 if type(i) is int:
35 i = i + 1
36
37 values = self.lookup.values()
38 self.first_int = min(values)
39 self.last_int = max(values)
40 self.first_name = self.reverseLookup[self.first_int]
41 self.last_name = self.reverseLookup[self.last_int]
42
43 def __str__(self):
44 return pf(self.lookup)
45
46 def __repr__(self):
47 return pf(self.lookup)
48
49 def __eq__(self, other):
50 return isinstance(other, Enumeration) and self.__doc__ == other.self.__doc__ and 0 == cmp(self.lookup, other.lookup)
51
52 def extend(self, enumList):
53 '''
54 Extend an existing enumeration with additional values.
55 '''
56 startValue = self.last_int + 1
57 self._addEnums(enumList, startValue)
58
59 def __getattr__(self, attr):
60 try: return self.lookup[attr]
61 except KeyError: raise AttributeError, attr
62
63 def whatis(self,value):
64 return self.reverseLookup[value]
65
66 def toInt(self, strval):
67 return self.lookup.get(strval)
68
69 def toStr(self,value):
70 return self.reverseLookup.get(value,"Value undefined: %s" % str(value))
71
72 def range(self):
73 keys = copy.copy(self.reverseLookup.keys())
74 keys.sort()
75 return keys
76
77 def valid(self, value):
78 return value in self.reverseLookup.keys()
79
80 def invalid(self, value):
81 return value not in self.reverseLookup.keys()
82
83 def vrange(self):
84 ''' returns an iterator of the enumeration values '''
85 return copy.copy(self.lookup.keys())
86
87 def first_asInt(self):
88 return self.first_int
89
90 def last_asInt(self):
91 return self.last_int
92
93 def first_asName(self):
94 return self.first_name
95
96 def last_asName(self):
97 return self.last_name
98
99if __name__ == '__main__':
100 #lets test things
101
102 testEnum0 = Enumeration("EnumName0",
103 ("Value0","Value1","Value2","Value3","Value4","Value5","Value6"))
104
105 print testEnum0.Value6
106
107 if testEnum0.__getattr__("Value6") == testEnum0.Value6:
108 print "Looks good"
109
110 # This is a bad case, we inserted a non-string value which should case
111 # an exception.
112# testEnum1 = Enumeration("EnumName1",
113# ("Value0","Value1","Value2",1,"Value3","Value4","Value5","Value6"))
114