From 606441e5c4f4a979231fb943b1fa39ea5371a727 Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Tue, 9 Nov 2010 14:48:13 -0700 Subject: Implement variable typing This implementation consists of two components: - oe.types python module, 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.types for pkg in oe.types.value("PACKAGES", d): bb.note("package: %s" % pkg) } LIBTOOL_HAS_SYSROOT = "yes" LIBTOOL_HAS_SYSROOT[type] = "boolean" python () { import oe.types assert(oe.types.value("LIBTOOL_HAS_SYSROOT", d) == True) } Signed-off-by: Chris Larson --- lib/oe/types.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 lib/oe/types.py (limited to 'lib/oe/types.py') diff --git a/lib/oe/types.py b/lib/oe/types.py new file mode 100644 index 0000000000..963e964212 --- /dev/null +++ b/lib/oe/types.py @@ -0,0 +1,97 @@ +# Constructs objects of the specified type for a given variable in the +# metadata. The 'type' flag of the variable defines which of the factory +# functions in this module will be called. +# +# If no type is defined, the value() function will simply return the expanded +# string value as is. + +import bb +import inspect +import _types + +types = {} + +class MissingFlag(TypeError): + 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): + try: + return types[var_type] + except KeyError: + raise TypeError("Invalid type '%s'" % var_type) + +def create(value, var_type, **flags): + 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 value(key, d): + """Construct a value for a metadata variable, based upon its flags""" + + var_type = d.getVarFlag(key, 'type') + flags = d.getVarFlags(key) + + 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 by oe.types""" + 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): + factory_setup(name, factory) + types[factory.name] = factory + +# Set the 'flags' and 'optflags' attributes of all our types +for name in dir(_types): + if name.startswith('_'): + continue + + obj = getattr(_types, name) + if not callable(obj): + continue + + register(name, obj) -- cgit 1.2.3-korg