diff options
author | Chris Larson <chris_larson@mentor.com> | 2011-02-09 10:28:42 -0700 |
---|---|---|
committer | Chris Larson <chris_larson@mentor.com> | 2011-02-09 12:10:45 -0700 |
commit | 2fbd56c0ef7df1b45746dbf9390805f2e67c590b (patch) | |
tree | a549ae6d6eb2783e5d2390f4bb7e007af551464f | |
parent | f41d2fdfa6fc443353aec43eec0bfdf5c4dbb5f8 (diff) | |
download | openembedded-2fbd56c0ef7df1b45746dbf9390805f2e67c590b.tar.gz |
More sane naming for the variable typing code
oe.types.value -> oe.data.typed_value
This name has been bugging me. This function is the primary interface to
the module for OE metadata, as it takes a variable name and datastore and
returns an object of the correct type. While this function is part of the
variable typing implementation, in reality it's more about giving you a
useful object from the metadata, so I think oe.data is a more appropriate
place for it.
oe.types -> oe.maketype
These are the functions which construct types, not the types themselves,
so it was somewhat misleading.
oe._types -> oe.types
These are the actual types, any callable in this module becomes an OE
type, using its arguments to determine the variable flags (optional and
required) to obey. Will use __init__'s args in the case of an actual
python class.
Signed-off-by: Chris Larson <chris_larson@mentor.com>
-rw-r--r-- | classes/base.bbclass | 14 | ||||
-rw-r--r-- | classes/typecheck.bbclass | 3 | ||||
-rw-r--r-- | lib/oe/_types.py | 104 | ||||
-rw-r--r-- | lib/oe/data.py | 13 | ||||
-rw-r--r-- | lib/oe/maketype.py | 97 | ||||
-rw-r--r-- | lib/oe/test_types.py | 2 | ||||
-rw-r--r-- | lib/oe/types.py | 176 | ||||
-rw-r--r-- | recipes/editline/editline_20100424-3.0.bb | 2 |
8 files changed, 206 insertions, 205 deletions
diff --git a/classes/base.bbclass b/classes/base.bbclass index b550874431..45febc95a7 100644 --- a/classes/base.bbclass +++ b/classes/base.bbclass @@ -10,7 +10,7 @@ inherit utils inherit utility-tasks inherit metadata_scm -OE_IMPORTS += "oe.path oe.utils oe.packagegroup oe.types sys os time" +OE_IMPORTS += "oe.path oe.utils oe.packagegroup oe.data sys os time" OE_IMPORTS[type] = "list" python oe_import () { @@ -86,7 +86,7 @@ python base_scenefunction () { } python base_do_setscene () { - for func in oe.types.value('SCENEFUNCS', d): + for func in oe.data.typed_value('SCENEFUNCS', d): bb.build.exec_func(func, d) if not os.path.exists(bb.data.getVar('STAMP', d, 1) + ".do_setscene"): bb.build.make_stamp("do_setscene", d) @@ -102,7 +102,7 @@ python base_do_fetch() { localdata = bb.data.createCopy(d) bb.data.update_data(localdata) - src_uri = oe.types.value('SRC_URI', localdata) + src_uri = oe.data.typed_value('SRC_URI', localdata) if not src_uri: return 1 try: @@ -192,11 +192,11 @@ do_unpack[dirs] = "${WORKDIR}" python base_do_unpack() { from glob import glob - src_uri = oe.types.value("SRC_URI", d) + src_uri = oe.data.typed_value("SRC_URI", d) if not src_uri: return srcurldata = bb.fetch.init(src_uri, d, True) - filespath = oe.types.value("FILESPATH", d) + filespath = oe.data.typed_value("FILESPATH", d) for url in src_uri: urldata = srcurldata[url] @@ -263,7 +263,7 @@ python build_summary() { else: print statusmsg - needed_vars = oe.types.value("BUILDCFG_NEEDEDVARS", e.data) + needed_vars = oe.data.typed_value("BUILDCFG_NEEDEDVARS", e.data) pesteruser = [] for v in needed_vars: val = bb.data.getVar(v, e.data, 1) @@ -328,7 +328,7 @@ def set_multimach_arch(d): multiarch = pkg_arch - for pkg in oe.types.value('PACKAGES', d): + for pkg in oe.data.typed_value('PACKAGES', d): pkgarch = bb.data.getVar("PACKAGE_ARCH_%s" % pkg, d, 1) # We could look for != PACKAGE_ARCH here but how to choose diff --git a/classes/typecheck.bbclass b/classes/typecheck.bbclass index 646cd4eed2..bacaad8691 100644 --- a/classes/typecheck.bbclass +++ b/classes/typecheck.bbclass @@ -3,10 +3,9 @@ # See oe.types for details. python check_types() { - import oe.types if isinstance(e, bb.event.ConfigParsed): for key in e.data.keys(): if e.data.getVarFlag(key, "type"): - oe.types.value(key, e.data) + oe.data.typed_value(key, e.data) } addhandler check_types diff --git a/lib/oe/_types.py b/lib/oe/_types.py deleted file mode 100644 index ea31cf4219..0000000000 --- a/lib/oe/_types.py +++ /dev/null @@ -1,104 +0,0 @@ -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) diff --git a/lib/oe/data.py b/lib/oe/data.py new file mode 100644 index 0000000000..8b7c3cd789 --- /dev/null +++ b/lib/oe/data.py @@ -0,0 +1,13 @@ +import oe.maketype +import bb.msg + +def typed_value(key, d): + """Construct a value for the specified metadata variable, using its flags + to determine the type and parameters for construction.""" + var_type = d.getVarFlag(key, 'type') + flags = d.getVarFlags(key) + + try: + return oe.maketype.create(d.getVar(key, True) or '', var_type, **flags) + except (TypeError, ValueError), exc: + bb.msg.fatal(bb.msg.domain.Data, "%s: %s" % (key, str(exc))) diff --git a/lib/oe/maketype.py b/lib/oe/maketype.py new file mode 100644 index 0000000000..04a0e7cd98 --- /dev/null +++ b/lib/oe/maketype.py @@ -0,0 +1,97 @@ +"""OpenEmbedded variable typing support + +Types are defined in the metadata by name, using the 'type' flag on a +variable. Other flags may be utilized in the construction of the types. See +the arguments of the type's factory for details. +""" + +import bb +import inspect +import types + +available_types = {} + +class MissingFlag(TypeError): + """A particular flag is required to construct the type, but has not been + provided.""" + def __init__(self, flag, type): + self.flag = flag + self.type = type + TypeError.__init__(self) + + def __str__(self): + return "Type '%s' requires flag '%s'" % (self.type, self.flag) + +def factory(var_type): + """Return the factory for a specified type.""" + try: + return available_types[var_type] + except KeyError: + raise TypeError("Invalid type '%s':\n Valid types: %s" % + (var_type, ', '.join(types))) + +def create(value, var_type, **flags): + """Create an object of the specified type, given the specified flags and + string value.""" + obj = factory(var_type) + objflags = {} + for flag in obj.flags: + if flag not in flags: + if flag not in obj.optflags: + raise MissingFlag(flag, var_type) + else: + objflags[flag] = flags[flag] + + return obj(value, **objflags) + +def get_callable_args(obj): + """Grab all but the first argument of the specified callable, returning + the list, as well as a list of which of the arguments have default + values.""" + if type(obj) is type: + obj = obj.__init__ + + args, varargs, keywords, defaults = inspect.getargspec(obj) + flaglist = [] + if args: + if len(args) > 1 and args[0] == 'self': + args = args[1:] + flaglist.extend(args) + + optional = set() + if defaults: + optional |= set(flaglist[-len(defaults):]) + return flaglist, optional + +def factory_setup(name, obj): + """Prepare a factory for use.""" + args, optional = get_callable_args(obj) + extra_args = args[1:] + if extra_args: + obj.flags, optional = extra_args, optional + obj.optflags = set(optional) + else: + obj.flags = obj.optflags = () + + if not hasattr(obj, 'name'): + obj.name = name + +def register(name, factory): + """Register a type, given its name and a factory callable. + + Determines the required and optional flags from the factory's + arguments.""" + factory_setup(name, factory) + available_types[factory.name] = factory + + +# Register all our included types +for name in dir(types): + if name.startswith('_'): + continue + + obj = getattr(types, name) + if not callable(obj): + continue + + register(name, obj) diff --git a/lib/oe/test_types.py b/lib/oe/test_types.py index 494abfae43..d842c54470 100644 --- a/lib/oe/test_types.py +++ b/lib/oe/test_types.py @@ -1,5 +1,5 @@ import unittest -from oe.types import create, factory +from oe.maketype import create, factory class TestTypes(unittest.TestCase): def assertIsInstance(self, obj, cls): diff --git a/lib/oe/types.py b/lib/oe/types.py index 86de4b084b..ea31cf4219 100644 --- a/lib/oe/types.py +++ b/lib/oe/types.py @@ -1,108 +1,104 @@ -"""OpenEmbedded variable typing support +import re -Types are defined in the metadata by name, using the 'type' flag on a -variable. Other flags may be utilized in the construction of the types. See -the arguments of the type's factory for details. -""" +class OEList(list): + """OpenEmbedded 'list' type -import bb -import inspect -import _types + 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).""" -types = {} + name = "list" -class MissingFlag(TypeError): - """A particular flag is required to construct the type, but has not been - provided.""" - def __init__(self, flag, type): - self.flag = flag - self.type = type - TypeError.__init__(self) + 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 "Type '%s' requires flag '%s'" % (self.type, self.flag) + 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) -def factory(var_type): - """Return the factory for a specified type.""" try: - return types[var_type] - except KeyError: - raise TypeError("Invalid type '%s':\n Valid types: %s" % - (var_type, ', '.join(types))) - -def create(value, var_type, **flags): - """Create an object of the specified type, given the specified flags and - string value.""" - obj = factory(var_type) - objflags = {} - for flag in obj.flags: - if flag not in flags: - if flag not in obj.optflags: - raise MissingFlag(flag, var_type) - else: - objflags[flag] = flags[flag] + return re.compile(value, flagval) + except re.error, exc: + raise ValueError("Invalid regex value '%s': %s" % + (value, exc.args[0])) - return obj(value, **objflags) +def boolean(value): + """OpenEmbedded 'boolean' type -def value(key, d): - """Construct a value for the specified metadata variable, using its flags - to determine the type and parameters for construction.""" - var_type = d.getVarFlag(key, 'type') - flags = d.getVarFlags(key) + Valid values for true: 'yes', 'y', 'true', 't', '1' + Valid values for false: 'no', 'n', 'false', 'f', '0' + """ - try: - return create(d.getVar(key, True) or '', var_type, **flags) - except (TypeError, ValueError), exc: - bb.fatal("%s: %s" % (key, str(exc))) - -def get_callable_args(obj): - """Grab all but the first argument of the specified callable, returning - the list, as well as a list of which of the arguments have default - values.""" - if type(obj) is type: - obj = obj.__init__ - - args, varargs, keywords, defaults = inspect.getargspec(obj) - flaglist = [] - if args: - if len(args) > 1 and args[0] == 'self': - args = args[1:] - flaglist.extend(args) - - optional = set() - if defaults: - optional |= set(flaglist[-len(defaults):]) - return flaglist, optional - -def factory_setup(name, obj): - """Prepare a factory for use.""" - args, optional = get_callable_args(obj) - extra_args = args[1:] - if extra_args: - obj.flags, optional = extra_args, optional - obj.optflags = set(optional) - else: - obj.flags = obj.optflags = () + if not isinstance(value, basestring): + raise TypeError("boolean accepts a string, not '%s'" % type(value)) - if not hasattr(obj, 'name'): - obj.name = name + 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 register(name, factory): - """Register a type, given its name and a factory callable. +def integer(value, numberbase=10): + """OpenEmbedded 'integer' type - Determines the required and optional flags from the factory's - arguments.""" - factory_setup(name, factory) - types[factory.name] = factory + Defaults to base 10, but this can be specified using the optional + 'numberbase' flag.""" + return int(value, int(numberbase)) -# Register all our included types -for name in dir(_types): - if name.startswith('_'): - continue +_float = float +def float(value, fromhex='false'): + """OpenEmbedded floating point type - obj = getattr(_types, name) - if not callable(obj): - continue + 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.""" - register(name, obj) + if boolean(fromhex): + return _float.fromhex(value) + else: + return _float(value) diff --git a/recipes/editline/editline_20100424-3.0.bb b/recipes/editline/editline_20100424-3.0.bb index 9f9686b9ee..ff5a7e5b34 100644 --- a/recipes/editline/editline_20100424-3.0.bb +++ b/recipes/editline/editline_20100424-3.0.bb @@ -20,6 +20,6 @@ ENABLE_WIDEC[type] = "boolean" EXTRA_OECONF += "\ --with-gnu-ld \ - ${@oe.utils.ifelse(oe.types.value('ENABLE_WIDEC', d), \ + ${@oe.utils.ifelse(oe.data.typed_value('ENABLE_WIDEC', d), \ '--enable-widec', '')} \ " |