mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-01-18 12:06:08 +00:00
utils/types: better enum class member setting
- What used to be enum.values is now enum.levels. - Add enum.names and enum.values that are lists of enum's levels' names and values respectively. - Add a check on creation to make sure that provided level names do not conflict with the atomatically created members.
This commit is contained in:
parent
8d27b50a7c
commit
a9959550af
@ -107,6 +107,14 @@ class TestEnumLevel(TestCase):
|
||||
e = enum(['one', 'two', 'three'], start=-10, step=10)
|
||||
assert_list_equal(e.values, [-10, 0, 10])
|
||||
|
||||
def test_enum_name_conflicts(self):
|
||||
assert_raises(ValueError, enum, ['names', 'one', 'two'])
|
||||
|
||||
e = enum(['NAMES', 'one', 'two'])
|
||||
assert_in('names', e.levels)
|
||||
assert_list_equal(e.names, ['names', 'one', 'two'])
|
||||
assert_equal(e.NAMES, 'names')
|
||||
|
||||
def test_enum_behavior(self):
|
||||
e = enum(['one', 'two', 'three'])
|
||||
|
||||
@ -118,9 +126,9 @@ class TestEnumLevel(TestCase):
|
||||
assert_not_equal(e.one, '0')
|
||||
|
||||
# ditto for enum membership tests
|
||||
assert_in('one', e.values)
|
||||
assert_in(2, e.values)
|
||||
assert_not_in('five', e.values)
|
||||
assert_in('one', e.levels)
|
||||
assert_in(2, e.levels)
|
||||
assert_not_in('five', e.levels)
|
||||
|
||||
# The same level object returned, only when
|
||||
# passing in a valid level name/value.
|
||||
|
@ -741,7 +741,7 @@ class RunConfiguration(Configuration):
|
||||
'retry_on_status',
|
||||
kind=list_of(Status),
|
||||
default=['FAILED', 'PARTIAL'],
|
||||
allowed_values=Status.values[Status.RUNNING.value:],
|
||||
allowed_values=Status.levels[Status.RUNNING.value:],
|
||||
description='''
|
||||
This is list of statuses on which a job will be considered to have
|
||||
failed and will be automatically retried up to ``max_retries``
|
||||
|
@ -186,7 +186,7 @@ def priority(priority):
|
||||
def wrapper(*args, **kwargs):
|
||||
return func(*args, **kwargs)
|
||||
wrapper.func_name = func.func_name
|
||||
if priority in Priority.values:
|
||||
if priority in Priority.levels:
|
||||
wrapper.priority = Priority(priority)
|
||||
else:
|
||||
if not isinstance(priority, int):
|
||||
|
@ -557,7 +557,7 @@ def enum(args, start=0, step=1):
|
||||
@classmethod
|
||||
def from_pod(cls, pod):
|
||||
lv = level.from_pod(pod)
|
||||
for enum_level in cls.values:
|
||||
for enum_level in cls.levels:
|
||||
if enum_level == lv:
|
||||
return enum_level
|
||||
msg = 'Unexpected value "{}" for enum.'
|
||||
@ -574,16 +574,24 @@ def enum(args, start=0, step=1):
|
||||
|
||||
raise ValueError('Invalid enum value: {}'.format(repr(name)))
|
||||
|
||||
reserved = ['values', 'levels', 'names']
|
||||
|
||||
levels = []
|
||||
n = start
|
||||
for v in args:
|
||||
name = caseless_string(identifier(v))
|
||||
id_v = identifier(v)
|
||||
if id_v in reserved:
|
||||
message = 'Invalid enum level name "{}"; must not be in {}'
|
||||
raise ValueError(message.format(v, reserved))
|
||||
name = caseless_string(id_v)
|
||||
lv = level(v, n)
|
||||
setattr(Enum, name, lv)
|
||||
levels.append(lv)
|
||||
n += step
|
||||
|
||||
setattr(Enum, 'values', levels)
|
||||
setattr(Enum, 'levels', levels)
|
||||
setattr(Enum, 'values', [lv.value for lv in levels])
|
||||
setattr(Enum, 'names', [lv.name for lv in levels])
|
||||
|
||||
return Enum
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user