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