maketype.py 2.8 KB

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