1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2025-09-02 03:12:34 +01:00

types: make enum levels serializable

Add to/from_pod implementations to level, so that it may be serialized
properly. In order to make sure that serialization is a reversible
operation, ensure that the level value is numeric.
This commit is contained in:
Sergei Trofimov
2017-06-23 17:34:17 +01:00
parent 399efb8474
commit 5b3d7af62e
2 changed files with 36 additions and 2 deletions

View File

@@ -488,9 +488,17 @@ class level(object):
"""
@staticmethod
def from_pod(pod):
name, value_part = pod.split('(')
return level(name, numeric(value_part.rstrip(')')))
def __init__(self, name, value):
self.name = caseless_string(name)
self.value = value
self.value = numeric(value)
def to_pod(self):
return repr(self)
def __str__(self):
return self.name
@@ -545,6 +553,15 @@ def enum(args, start=0, step=1):
class Enum(object):
@classmethod
def from_pod(cls, pod):
lv = level.from_pod(pod)
for enum_level in cls.values:
if enum_level == lv:
return enum_level
msg = 'Unexpected value "{}" for enum.'
raise ValueError(msg.format(pod))
def __new__(cls, name):
for attr_name in dir(cls):
if attr_name.startswith('__'):