diff options
author | Chris Larson <chris_larson@mentor.com> | 2010-11-09 14:48:13 -0700 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2011-05-20 17:33:02 +0100 |
commit | a04ce490e933fc7534db33f635b025c25329c564 (patch) | |
tree | 38bf814b03c6cb2175e6f149ebbcf5ed92f8e1c4 /meta/lib/oe/types.py | |
parent | 8c6bf9f6775fc5aaa8bc2c77924c95a00f1c1890 (diff) | |
download | openembedded-core-contrib-a04ce490e933fc7534db33f635b025c25329c564.tar.gz |
Implement variable typing (sync from OE)
This implementation consists of two components:
- Type creation python modules, whose job it is to construct objects of the
defined type for a given variable in the metadata
- typecheck.bbclass, which iterates over all configuration variables with a
type defined and uses oe.types to check the validity of the values
This gives us a few benefits:
- Automatic sanity checking of all configuration variables with a defined type
- Avoid duplicating the "how do I make use of the value of this variable"
logic between its users. For variables like PATH, this is simply a split(),
for boolean variables, the duplication can result in confusing, or even
mismatched semantics (is this 0/1, empty/nonempty, what?)
- Make it easier to create a configuration UI, as the type information could
be used to provide a better interface than a text edit box (e.g checkbox for
'boolean', dropdown for 'choice')
This functionality is entirely opt-in right now. To enable the configuration
variable type checking, simply INHERIT += "typecheck". Example of a failing
type check:
BAZ = "foo"
BAZ[type] = "boolean"
$ bitbake -p
FATAL: BAZ: Invalid boolean value 'foo'
$
Examples of leveraging oe.types in a python snippet:
PACKAGES[type] = "list"
python () {
import oe.data
for pkg in oe.data.typed_value("PACKAGES", d):
bb.note("package: %s" % pkg)
}
LIBTOOL_HAS_SYSROOT = "yes"
LIBTOOL_HAS_SYSROOT[type] = "boolean"
python () {
import oe.data
assert(oe.data.typed_value("LIBTOOL_HAS_SYSROOT", d) == True)
}
Signed-off-by: Chris Larson <chris_larson@mentor.com>
Diffstat (limited to 'meta/lib/oe/types.py')
-rw-r--r-- | meta/lib/oe/types.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/meta/lib/oe/types.py b/meta/lib/oe/types.py new file mode 100644 index 0000000000..ea31cf4219 --- /dev/null +++ b/meta/lib/oe/types.py @@ -0,0 +1,104 @@ +import re + +class OEList(list): + """OpenEmbedded 'list' type + + Acts as an ordinary list, but is constructed from a string value and a + separator (optional), and re-joins itself when converted to a string with + str(). Set the variable type flag to 'list' to use this type, and the + 'separator' flag may be specified (defaulting to whitespace).""" + + name = "list" + + def __init__(self, value, separator = None): + if value is not None: + list.__init__(self, value.split(separator)) + else: + list.__init__(self) + + if separator is None: + self.separator = " " + else: + self.separator = separator + + def __str__(self): + return self.separator.join(self) + +def choice(value, choices): + """OpenEmbedded 'choice' type + + Acts as a multiple choice for the user. To use this, set the variable + type flag to 'choice', and set the 'choices' flag to a space separated + list of valid values.""" + if not isinstance(value, basestring): + raise TypeError("choice accepts a string, not '%s'" % type(value)) + + value = value.lower() + choices = choices.lower() + if value not in choices.split(): + raise ValueError("Invalid choice '%s'. Valid choices: %s" % + (value, choices)) + return value + +def regex(value, regexflags=None): + """OpenEmbedded 'regex' type + + Acts as a regular expression, returning the pre-compiled regular + expression pattern object. To use this type, set the variable type flag + to 'regex', and optionally, set the 'regexflags' type to a space separated + list of the flags to control the regular expression matching (e.g. + FOO[regexflags] += 'ignorecase'). See the python documentation on the + 're' module for a list of valid flags.""" + + flagval = 0 + if regexflags: + for flag in regexflags.split(): + flag = flag.upper() + try: + flagval |= getattr(re, flag) + except AttributeError: + raise ValueError("Invalid regex flag '%s'" % flag) + + try: + return re.compile(value, flagval) + except re.error, exc: + raise ValueError("Invalid regex value '%s': %s" % + (value, exc.args[0])) + +def boolean(value): + """OpenEmbedded 'boolean' type + + Valid values for true: 'yes', 'y', 'true', 't', '1' + Valid values for false: 'no', 'n', 'false', 'f', '0' + """ + + if not isinstance(value, basestring): + raise TypeError("boolean accepts a string, not '%s'" % type(value)) + + value = value.lower() + if value in ('yes', 'y', 'true', 't', '1'): + return True + elif value in ('no', 'n', 'false', 'f', '0'): + return False + raise ValueError("Invalid boolean value '%s'" % value) + +def integer(value, numberbase=10): + """OpenEmbedded 'integer' type + + Defaults to base 10, but this can be specified using the optional + 'numberbase' flag.""" + + return int(value, int(numberbase)) + +_float = float +def float(value, fromhex='false'): + """OpenEmbedded floating point type + + To use this type, set the type flag to 'float', and optionally set the + 'fromhex' flag to a true value (obeying the same rules as for the + 'boolean' type) if the value is in base 16 rather than base 10.""" + + if boolean(fromhex): + return _float.fromhex(value) + else: + return _float(value) |