blob: b748fd3c2f8f94220bfd5565660563d52f87d9c5 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Chetan Gaonkercfcce782016-05-10 10:10:42 -070017#
18# Copyright 2016-present Ciena Corporation
19#
20# Licensed under the Apache License, Version 2.0 (the "License");
21# you may not use this file except in compliance with the License.
22# You may obtain a copy of the License at
23#
24# http://www.apache.org/licenses/LICENSE-2.0
25#
26# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS,
28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
31#
A R Karthicka2e53d62016-02-19 17:38:30 -080032#!python
33import copy
34import pprint
35pf = pprint.pformat
36
37class EnumException(Exception):
38 pass
39class Enumeration(object):
40 def __init__(self, name, enumList, valuesAreUnique=False, startValue=0):
41 self.__doc__ = name
42 self.uniqueVals = valuesAreUnique
43 self.lookup = {}
44 self.reverseLookup = {}
45
46 self._addEnums(enumList, startValue)
47
48 def _addEnums(self, enumList, startValue):
49 i = startValue
50 for x in enumList:
51 if type(x) is tuple:
52 try:
53 x, i = x
54 except ValueError:
55 raise EnumException, "tuple doesn't have 2 items: %r" % (x,)
56 if type(x) is not str:
57 raise EnumException, "enum name is not a string: %r" % (x,)
58 if x in self.lookup:
59 raise EnumException, "enum name is not unique: %r" % (x,)
60 if self.uniqueVals and i in self.reverseLookup:
61 raise EnumException, "enum value %r not unique for %r" % (i, x)
62 self.lookup[x] = i
63 self.reverseLookup[i] = x
64
65 if type(i) is int:
66 i = i + 1
67
68 values = self.lookup.values()
69 self.first_int = min(values)
70 self.last_int = max(values)
71 self.first_name = self.reverseLookup[self.first_int]
72 self.last_name = self.reverseLookup[self.last_int]
73
74 def __str__(self):
75 return pf(self.lookup)
76
77 def __repr__(self):
78 return pf(self.lookup)
79
80 def __eq__(self, other):
81 return isinstance(other, Enumeration) and self.__doc__ == other.self.__doc__ and 0 == cmp(self.lookup, other.lookup)
82
83 def extend(self, enumList):
84 '''
85 Extend an existing enumeration with additional values.
86 '''
87 startValue = self.last_int + 1
88 self._addEnums(enumList, startValue)
89
90 def __getattr__(self, attr):
91 try: return self.lookup[attr]
92 except KeyError: raise AttributeError, attr
93
94 def whatis(self,value):
95 return self.reverseLookup[value]
96
97 def toInt(self, strval):
98 return self.lookup.get(strval)
99
100 def toStr(self,value):
101 return self.reverseLookup.get(value,"Value undefined: %s" % str(value))
102
103 def range(self):
104 keys = copy.copy(self.reverseLookup.keys())
105 keys.sort()
106 return keys
107
108 def valid(self, value):
109 return value in self.reverseLookup.keys()
110
111 def invalid(self, value):
112 return value not in self.reverseLookup.keys()
113
114 def vrange(self):
115 ''' returns an iterator of the enumeration values '''
116 return copy.copy(self.lookup.keys())
117
118 def first_asInt(self):
119 return self.first_int
120
121 def last_asInt(self):
122 return self.last_int
123
124 def first_asName(self):
125 return self.first_name
126
127 def last_asName(self):
128 return self.last_name
129
130if __name__ == '__main__':
131 #lets test things
132
133 testEnum0 = Enumeration("EnumName0",
134 ("Value0","Value1","Value2","Value3","Value4","Value5","Value6"))
135
136 print testEnum0.Value6
137
138 if testEnum0.__getattr__("Value6") == testEnum0.Value6:
139 print "Looks good"
140
141 # This is a bad case, we inserted a non-string value which should case
142 # an exception.
143# testEnum1 = Enumeration("EnumName1",
144# ("Value0","Value1","Value2",1,"Value3","Value4","Value5","Value6"))
145