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