meta.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """Cement core meta functionality."""
  2. class Meta(object):
  3. """
  4. Model that acts as a container class for a meta attributes for a larger
  5. class. It stuffs any kwarg it gets in it's init as an attribute of itself.
  6. """
  7. def __init__(self, **kwargs):
  8. self._merge(kwargs)
  9. def _merge(self, dict_obj):
  10. for key in dict_obj.keys():
  11. setattr(self, key, dict_obj[key])
  12. class MetaMixin(object):
  13. """
  14. Mixin that provides the Meta class support to add settings to instances
  15. of slumber objects. Meta settings cannot start with a _.
  16. """
  17. def __init__(self, *args, **kwargs):
  18. # Get a List of all the Classes we in our MRO, find any attribute named
  19. # Meta on them, and then merge them together in order of MRO
  20. metas = reversed([x.Meta for x in self.__class__.mro()
  21. if hasattr(x, "Meta")])
  22. final_meta = {}
  23. # Merge the Meta classes into one dict
  24. for meta in metas:
  25. final_meta.update(dict([x for x in meta.__dict__.items()
  26. if not x[0].startswith("_")]))
  27. # Update the final Meta with any kwargs passed in
  28. for key in final_meta.keys():
  29. if key in kwargs:
  30. final_meta[key] = kwargs.pop(key)
  31. self._meta = Meta(**final_meta)
  32. # FIX ME: object.__init__() doesn't take params without exception
  33. super(MetaMixin, self).__init__()