Add in an implementation for Enumeration for testing.

This commit is contained in:
Mike Auty
2016-12-15 02:02:30 +00:00
parent 698d1c2b4a
commit de18fe234c
+35 -2
View File
@@ -237,10 +237,43 @@ class BitField(PrimitiveObject, int):
raise NotImplementedError("Writing to BitFields is not yet implemented")
class Enumeration(interfaces.objects.ObjectInterface):
class Enumeration(PrimitiveObject, int):
"""Returns an object made up of choices"""
# FIXME: Add in body for the enumeration object
def __new__(cls, context, type_name, object_info, struct_format, subtype = None, choices = None):
cls._check_class(subtype, Integer)
value = subtype(context = context,
type_name = type_name,
object_info = object_info,
struct_format = struct_format)
return cls._struct_type.__new__(cls, value)
def __init__(self, context, type_name, object_info, struct_format, subtype = None, choices = None):
super().__init__(context, type_name, object_info)
for k, v in self._check_type(choices, dict):
self._check_type(k, str)
self._check_type(v, int)
self._vol['choices'] = choices
self._vol['subtype'] = subtype
@property
def description(self):
"""Returns the chosen entry for the value this object contains"""
return self.lookup(self)
def lookup(self, value):
"""Looks up an individual value and returns the associated entry"""
for k, num in self.vol.constants.items():
if value == num:
return k
raise ValueError("The value of the enumeration is outside the possible choices")
def __getattr__(self, attr):
if attr in self._vol['choices']:
return self._vol['choices'][attr]
raise AttributeError("Unknown attribute {} for Enumeration {}".format(attr, self._vol['type_name']))
def write(self, value):
raise NotImplementedError("Writing to Enumerations is not yet implemented")