Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 4 | """OpenEmbedded variable typing support |
| 5 | |
| 6 | Types are defined in the metadata by name, using the 'type' flag on a |
| 7 | variable. Other flags may be utilized in the construction of the types. See |
| 8 | the arguments of the type's factory for details. |
| 9 | """ |
| 10 | |
| 11 | import inspect |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 12 | import oe.types as types |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 13 | from collections.abc import Callable |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | |
| 15 | available_types = {} |
| 16 | |
| 17 | class MissingFlag(TypeError): |
| 18 | """A particular flag is required to construct the type, but has not been |
| 19 | provided.""" |
| 20 | def __init__(self, flag, type): |
| 21 | self.flag = flag |
| 22 | self.type = type |
| 23 | TypeError.__init__(self) |
| 24 | |
| 25 | def __str__(self): |
| 26 | return "Type '%s' requires flag '%s'" % (self.type, self.flag) |
| 27 | |
| 28 | def factory(var_type): |
| 29 | """Return the factory for a specified type.""" |
| 30 | if var_type is None: |
| 31 | raise TypeError("No type specified. Valid types: %s" % |
| 32 | ', '.join(available_types)) |
| 33 | try: |
| 34 | return available_types[var_type] |
| 35 | except KeyError: |
| 36 | raise TypeError("Invalid type '%s':\n Valid types: %s" % |
| 37 | (var_type, ', '.join(available_types))) |
| 38 | |
| 39 | def create(value, var_type, **flags): |
| 40 | """Create an object of the specified type, given the specified flags and |
| 41 | string value.""" |
| 42 | obj = factory(var_type) |
| 43 | objflags = {} |
| 44 | for flag in obj.flags: |
| 45 | if flag not in flags: |
| 46 | if flag not in obj.optflags: |
| 47 | raise MissingFlag(flag, var_type) |
| 48 | else: |
| 49 | objflags[flag] = flags[flag] |
| 50 | |
| 51 | return obj(value, **objflags) |
| 52 | |
| 53 | def get_callable_args(obj): |
| 54 | """Grab all but the first argument of the specified callable, returning |
| 55 | the list, as well as a list of which of the arguments have default |
| 56 | values.""" |
| 57 | if type(obj) is type: |
| 58 | obj = obj.__init__ |
| 59 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 60 | sig = inspect.signature(obj) |
| 61 | args = list(sig.parameters.keys()) |
| 62 | defaults = list(s for s in sig.parameters.keys() if sig.parameters[s].default != inspect.Parameter.empty) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | flaglist = [] |
| 64 | if args: |
| 65 | if len(args) > 1 and args[0] == 'self': |
| 66 | args = args[1:] |
| 67 | flaglist.extend(args) |
| 68 | |
| 69 | optional = set() |
| 70 | if defaults: |
| 71 | optional |= set(flaglist[-len(defaults):]) |
| 72 | return flaglist, optional |
| 73 | |
| 74 | def factory_setup(name, obj): |
| 75 | """Prepare a factory for use.""" |
| 76 | args, optional = get_callable_args(obj) |
| 77 | extra_args = args[1:] |
| 78 | if extra_args: |
| 79 | obj.flags, optional = extra_args, optional |
| 80 | obj.optflags = set(optional) |
| 81 | else: |
| 82 | obj.flags = obj.optflags = () |
| 83 | |
| 84 | if not hasattr(obj, 'name'): |
| 85 | obj.name = name |
| 86 | |
| 87 | def register(name, factory): |
| 88 | """Register a type, given its name and a factory callable. |
| 89 | |
| 90 | Determines the required and optional flags from the factory's |
| 91 | arguments.""" |
| 92 | factory_setup(name, factory) |
| 93 | available_types[factory.name] = factory |
| 94 | |
| 95 | |
| 96 | # Register all our included types |
| 97 | for name in dir(types): |
| 98 | if name.startswith('_'): |
| 99 | continue |
| 100 | |
| 101 | obj = getattr(types, name) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 102 | if not isinstance(obj, Callable): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | continue |
| 104 | |
| 105 | register(name, obj) |