mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-01-31 10:11:17 +00:00
Utils/Serializer: Adds serializer support for level
type
This commit is contained in:
parent
de3301312f
commit
2cc60b77cb
@ -52,7 +52,7 @@ import dateutil.parser
|
|||||||
|
|
||||||
from wa.framework.exception import SerializerSyntaxError
|
from wa.framework.exception import SerializerSyntaxError
|
||||||
from wa.utils.misc import isiterable
|
from wa.utils.misc import isiterable
|
||||||
from wa.utils.types import regex_type, none_type
|
from wa.utils.types import regex_type, none_type, level
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
@ -78,6 +78,7 @@ POD_TYPES = [
|
|||||||
datetime,
|
datetime,
|
||||||
regex_type,
|
regex_type,
|
||||||
none_type,
|
none_type,
|
||||||
|
level,
|
||||||
]
|
]
|
||||||
|
|
||||||
class WAJSONEncoder(_json.JSONEncoder):
|
class WAJSONEncoder(_json.JSONEncoder):
|
||||||
@ -87,6 +88,8 @@ class WAJSONEncoder(_json.JSONEncoder):
|
|||||||
return 'REGEX:{}:{}'.format(obj.flags, obj.pattern)
|
return 'REGEX:{}:{}'.format(obj.flags, obj.pattern)
|
||||||
elif isinstance(obj, datetime):
|
elif isinstance(obj, datetime):
|
||||||
return 'DATET:{}'.format(obj.isoformat())
|
return 'DATET:{}'.format(obj.isoformat())
|
||||||
|
elif isinstance(obj, level):
|
||||||
|
return 'LEVEL:{}:{}'.format(obj.name, obj.value)
|
||||||
else:
|
else:
|
||||||
return _json.JSONEncoder.default(self, obj)
|
return _json.JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
@ -97,13 +100,17 @@ class WAJSONDecoder(_json.JSONDecoder):
|
|||||||
d = _json.JSONDecoder.decode(self, s, **kwargs)
|
d = _json.JSONDecoder.decode(self, s, **kwargs)
|
||||||
|
|
||||||
def try_parse_object(v):
|
def try_parse_object(v):
|
||||||
if isinstance(v, basestring) and v.startswith('REGEX:'):
|
if isinstance(v, basestring):
|
||||||
|
if v.startswith('REGEX:'):
|
||||||
_, flags, pattern = v.split(':', 2)
|
_, flags, pattern = v.split(':', 2)
|
||||||
return re.compile(pattern, int(flags or 0))
|
return re.compile(pattern, int(flags or 0))
|
||||||
elif isinstance(v, basestring) and v.startswith('DATET:'):
|
elif v.startswith('DATET:'):
|
||||||
_, pattern = v.split(':', 1)
|
_, pattern = v.split(':', 1)
|
||||||
return dateutil.parser.parse(pattern)
|
return dateutil.parser.parse(pattern)
|
||||||
else:
|
elif v.startswith('LEVEL:'):
|
||||||
|
_, name, value = v.split(':', 2)
|
||||||
|
return level(name, value)
|
||||||
|
|
||||||
return v
|
return v
|
||||||
|
|
||||||
def load_objects(d):
|
def load_objects(d):
|
||||||
@ -147,6 +154,7 @@ class json(object):
|
|||||||
|
|
||||||
_mapping_tag = _yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
|
_mapping_tag = _yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
|
||||||
_regex_tag = u'tag:wa:regex'
|
_regex_tag = u'tag:wa:regex'
|
||||||
|
_level_tag = u'tag:wa:level'
|
||||||
|
|
||||||
|
|
||||||
def _wa_dict_representer(dumper, data):
|
def _wa_dict_representer(dumper, data):
|
||||||
@ -157,6 +165,9 @@ def _wa_regex_representer(dumper, data):
|
|||||||
text = '{}:{}'.format(data.flags, data.pattern)
|
text = '{}:{}'.format(data.flags, data.pattern)
|
||||||
return dumper.represent_scalar(_regex_tag, text)
|
return dumper.represent_scalar(_regex_tag, text)
|
||||||
|
|
||||||
|
def _wa_level_representer(dumper, data):
|
||||||
|
text = '{}:{}'.format(data.name, data.level)
|
||||||
|
return dumper.represent_scalar(_level_tag, text)
|
||||||
|
|
||||||
def _wa_dict_constructor(loader, node):
|
def _wa_dict_constructor(loader, node):
|
||||||
pairs = loader.construct_pairs(node)
|
pairs = loader.construct_pairs(node)
|
||||||
@ -173,11 +184,18 @@ def _wa_regex_constructor(loader, node):
|
|||||||
flags, pattern = value.split(':', 1)
|
flags, pattern = value.split(':', 1)
|
||||||
return re.compile(pattern, int(flags or 0))
|
return re.compile(pattern, int(flags or 0))
|
||||||
|
|
||||||
|
def _wa_level_constructor(loader, node):
|
||||||
|
value = loader.construct_scalar(node)
|
||||||
|
name, value = value.split(':', 1)
|
||||||
|
return level(name, value)
|
||||||
|
|
||||||
|
|
||||||
_yaml.add_representer(OrderedDict, _wa_dict_representer)
|
_yaml.add_representer(OrderedDict, _wa_dict_representer)
|
||||||
_yaml.add_representer(regex_type, _wa_regex_representer)
|
_yaml.add_representer(regex_type, _wa_regex_representer)
|
||||||
|
_yaml.add_representer(level, _wa_level_representer)
|
||||||
_yaml.add_constructor(_mapping_tag, _wa_dict_constructor)
|
_yaml.add_constructor(_mapping_tag, _wa_dict_constructor)
|
||||||
_yaml.add_constructor(_regex_tag, _wa_regex_constructor)
|
_yaml.add_constructor(_regex_tag, _wa_regex_constructor)
|
||||||
|
_yaml.add_constructor(_level_tag, _wa_level_constructor)
|
||||||
|
|
||||||
|
|
||||||
class yaml(object):
|
class yaml(object):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user