1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2025-02-07 13:41:24 +00:00

Adding new types and updating device parameters to use them.

- added caseless_string type. This behaves exactly like a string, except
  this ignores case in comparisons. It does, however, preserve case. E.g.

	>>> s = caseless_string('Test')
	>>> s == 'test'
	True
	>>> print s
	Test

- added list_of type generating function. This allows to dynamically
  generate type-safe list types based on an existing type. E.g.

	>>> list_of_bool = list_of(bool)
	>>> list_of_bool(['foo', 0, 1, '', True])
	[True, False, True, False, True]

- Update core_names Device Parameter to be of type caseless_string
This commit is contained in:
Sergei Trofimov 2015-04-09 11:32:06 +01:00
parent 070b2230c9
commit 3112eb0a9b
2 changed files with 56 additions and 2 deletions

View File

@ -36,7 +36,7 @@ from contextlib import contextmanager
from wlauto.core.extension import Extension, ExtensionMeta, AttributeCollection, Parameter from wlauto.core.extension import Extension, ExtensionMeta, AttributeCollection, Parameter
from wlauto.exceptions import DeviceError, ConfigError from wlauto.exceptions import DeviceError, ConfigError
from wlauto.utils.types import list_of_strings, list_of_integers from wlauto.utils.types import list_of_integers, list_of, caseless_string
__all__ = ['RuntimeParameter', 'CoreParameter', 'Device', 'DeviceMeta'] __all__ = ['RuntimeParameter', 'CoreParameter', 'Device', 'DeviceMeta']
@ -137,7 +137,7 @@ class Device(Extension):
__metaclass__ = DeviceMeta __metaclass__ = DeviceMeta
parameters = [ parameters = [
Parameter('core_names', kind=list_of_strings, mandatory=True, default=None, Parameter('core_names', kind=list_of(caseless_string), mandatory=True, default=None,
description=""" description="""
This is a list of all cpu cores on the device with each This is a list of all cpu cores on the device with each
element being the core type, e.g. ``['a7', 'a7', 'a15']``. The element being the core type, e.g. ``['a7', 'a7', 'a15']``. The

View File

@ -53,6 +53,14 @@ def boolean(value):
return bool(value) return bool(value)
def integer(value):
"""Handles conversions for string respresentations of binary, octal and hex."""
if isinstance(value, basestring):
return int(value, 0)
else:
return int(value)
def numeric(value): def numeric(value):
""" """
Returns the value as number (int if possible, or float otherwise), or Returns the value as number (int if possible, or float otherwise), or
@ -140,6 +148,31 @@ def list_of_bools(value, interpret_strings=True):
return map(bool, value) return map(bool, value)
def list_of(type_):
"""Generates a "list of" callable for the specified type. The callable
attempts to convert all elements in the passed value to the specifed
``type_``, raising ``ValueError`` on error."""
def __init__(self, values):
list.__init__(self, map(type_, values))
def append(self, value):
list.append(self, type_(value))
def extend(self, other):
list.extend(self, map(type_, other))
def __setitem__(self, idx, value):
list.__setitem__(self, idx, type_(value))
return type('list_of_{}s'.format(type_.__name__),
(list, ), {
"__init__": __init__,
"__setitem__": __setitem__,
"append": append,
"extend": extend,
})
regex_type = type(re.compile('')) regex_type = type(re.compile(''))
@ -174,3 +207,24 @@ def counter(name=None):
__counters[name] += 1 __counters[name] += 1
value = __counters[name] value = __counters[name]
return value return value
class caseless_string(str):
"""
Just like built-in Python string except case-insensitive on comparisons. However, the
case is preserved otherwise.
"""
def __eq__(self, other):
if isinstance(other, basestring):
other = other.lower()
return self.lower() == other
def __ne__(self, other):
return not self.__eq__(other)
def __cmp__(self, other):
if isinstance(basestring, other):
other = other.lower()
return cmp(self.lower(), other)