From 9905bb3162b4c4ea1717602a304e3e068fe5e9c7 Mon Sep 17 00:00:00 2001 From: Sebastian Goscik Date: Mon, 26 Sep 2016 15:17:23 +0100 Subject: [PATCH] ConfigurationPoints: Added check that default values are POD's Default config values will now need to be a Plain Old Data type. This commit includes some ultility functions to help with this check --- wlauto/core/configuration/configuration.py | 6 +++++- wlauto/utils/serializer.py | 24 ++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/wlauto/core/configuration/configuration.py b/wlauto/core/configuration/configuration.py index 6e5cede3..184830c9 100644 --- a/wlauto/core/configuration/configuration.py +++ b/wlauto/core/configuration/configuration.py @@ -24,6 +24,7 @@ from wlauto.utils.types import (identifier, integer, boolean, list_of_strings, toggle_set, obj_dict) from wlauto.core.configuration.tree import SectionNode +from wlauto.utils.serializer import is_pod # Mapping for kind conversion; see docs for convert_types below KIND_MAP = { @@ -228,6 +229,9 @@ class ConfigurationPoint(object): raise ValueError('Kind must be callable.') self.kind = kind self.mandatory = mandatory + if not is_pod(default): + msg = "The default for '{}' must be a Plain Old Data type, but it is of type '{}' instead." + raise TypeError(msg.format(self.name, type(default))) self.default = default self.override = override self.allowed_values = allowed_values @@ -798,7 +802,7 @@ class RunConfiguration(Configuration): the connected device. Obviously, this must match your setup. '''), ConfigurationPoint('retry_on_status', kind=status_list, - default=status_list(['FAILED', 'PARTIAL']), + default=['FAILED', 'PARTIAL'], allowed_values=ITERATION_STATUS, description=''' This is list of statuses on which a job will be cosidered to have failed and diff --git a/wlauto/utils/serializer.py b/wlauto/utils/serializer.py index a8da7beb..821e01b1 100644 --- a/wlauto/utils/serializer.py +++ b/wlauto/utils/serializer.py @@ -7,7 +7,7 @@ structures and Python class instances). The modifications to standard serilization procedures are: - - mappings are deserialized as ``OrderedDict``\ 's are than standard + - mappings are deserialized as ``OrderedDict``\ 's rather than standard Python ``dict``\ 's. This allows for cleaner syntax in certain parts of WA configuration (e.g. values to be written to files can be specified as a dict, and they will be written in the order specified in the config). @@ -61,15 +61,27 @@ __all__ = [ 'read_pod', 'dump', 'load', + 'is_pod', + 'POD_TYPES', ] +POD_TYPES = [ + list, + tuple, + dict, + set, + basestring, + int, + float, + bool, + datetime, + regex_type +] class WAJSONEncoder(_json.JSONEncoder): def default(self, obj): # pylint: disable=method-hidden - if hasattr(obj, 'to_pod'): - return obj.to_pod() - elif isinstance(obj, regex_type): + if isinstance(obj, regex_type): return 'REGEX:{}:{}'.format(obj.flags, obj.pattern) elif isinstance(obj, datetime): return 'DATET:{}'.format(obj.isoformat()) @@ -241,3 +253,7 @@ def _read_pod(fh, fmt=None): return python.load(fh) else: raise ValueError('Unknown format "{}": {}'.format(fmt, getattr(fh, 'name', ''))) + + +def is_pod(obj): + return type(obj) in POD_TYPES