mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-04-20 09:40:50 +01:00
Improving agenda validation
- raise an error if an agenda contains duplicate keys (by default PyYAML will silently ignore this) - raise an error if config section in an agenda is not dict-like (before, this was allowed to propagate and relsulted an a traceback further down the line).
This commit is contained in:
parent
698240b6e0
commit
5ceb093f3c
@ -161,13 +161,19 @@ class Agenda(object):
|
|||||||
self._assign_id_if_needed(w, 'workload')
|
self._assign_id_if_needed(w, 'workload')
|
||||||
return AgendaWorkloadEntry(**w)
|
return AgendaWorkloadEntry(**w)
|
||||||
|
|
||||||
def _load(self, source):
|
def _load(self, source): # pylint: disable=too-many-branches
|
||||||
raw = self._load_raw_from_source(source)
|
try:
|
||||||
|
raw = self._load_raw_from_source(source)
|
||||||
|
except ValueError as e:
|
||||||
|
name = getattr(source, 'name', '')
|
||||||
|
raise ConfigError('Error parsing agenda {}: {}'.format(name, e))
|
||||||
if not isinstance(raw, dict):
|
if not isinstance(raw, dict):
|
||||||
message = '{} does not contain a valid agenda structure; top level must be a dict.'
|
message = '{} does not contain a valid agenda structure; top level must be a dict.'
|
||||||
raise ConfigError(message.format(self.filepath))
|
raise ConfigError(message.format(self.filepath))
|
||||||
for k, v in raw.iteritems():
|
for k, v in raw.iteritems():
|
||||||
if k == 'config':
|
if k == 'config':
|
||||||
|
if not isinstance(v, dict):
|
||||||
|
raise ConfigError('Invalid agenda: "config" entry must be a dict')
|
||||||
self.config = v
|
self.config = v
|
||||||
elif k == 'global':
|
elif k == 'global':
|
||||||
self.global_ = AgendaGlobalEntry(**v)
|
self.global_ = AgendaGlobalEntry(**v)
|
||||||
@ -237,7 +243,13 @@ def dict_representer(dumper, data):
|
|||||||
|
|
||||||
|
|
||||||
def dict_constructor(loader, node):
|
def dict_constructor(loader, node):
|
||||||
return OrderedDict(loader.construct_pairs(node))
|
pairs = loader.construct_pairs(node)
|
||||||
|
seen_keys = set()
|
||||||
|
for k, _ in pairs:
|
||||||
|
if k in seen_keys:
|
||||||
|
raise ValueError('Duplicate entry: {}'.format(k))
|
||||||
|
seen_keys.add(k)
|
||||||
|
return OrderedDict(pairs)
|
||||||
|
|
||||||
|
|
||||||
yaml.add_representer(OrderedDict, dict_representer)
|
yaml.add_representer(OrderedDict, dict_representer)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user