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