plugins.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright 2013 The Distro Tracker Developers
  2. # See the COPYRIGHT file at the top-level directory of this distribution and
  3. # at http://deb.li/DTAuthors
  4. #
  5. # This file is part of Distro Tracker. It is subject to the license terms
  6. # in the LICENSE file found in the top-level directory of this
  7. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  8. # including this file, may be copied, modified, propagated, or distributed
  9. # except according to the terms contained in the LICENSE file.
  10. from __future__ import unicode_literals
  11. class PluginRegistry(type):
  12. """
  13. A metaclass which any class that wants to behave as a registry can use.
  14. When classes derived from classes which use this metaclass are
  15. instantiated, they are added to the list :attr:`plugins`.
  16. The concrete classes using this metaclass are free to decide how to use
  17. this list.
  18. This metaclass also adds an :meth:`unregister_plugin` classmethod to all
  19. concrete classes which removes the class from the list of plugins.
  20. """
  21. def __init__(cls, name, bases, attrs): # noqa
  22. if not hasattr(cls, 'plugins'):
  23. cls.plugins = []
  24. else:
  25. cls.plugins.append(cls)
  26. cls.unregister_plugin = classmethod(
  27. lambda cls: cls.plugins.remove(cls)
  28. )