1# Copyright 2013 The Distro Tracker Developers
2# See the COPYRIGHT file at the top-level directory of this distribution and
3# at https://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 https://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"""Classes to build a plugin mechanism."""
13class PluginRegistry(type):
14 """
15 A metaclass which any class that wants to behave as a registry can use.
17 When classes derived from classes which use this metaclass are
18 instantiated, they are added to the list :attr:`plugins`.
19 The concrete classes using this metaclass are free to decide how to use
20 this list.
22 This metaclass also adds an :meth:`unregister_plugin` classmethod to all
23 concrete classes which removes the class from the list of plugins.
24 """
25 def __init__(cls, name, bases, attrs): # noqa
26 if not hasattr(cls, 'plugins'):
27 cls.plugins = []
28 else:
29 cls.plugins.append(cls)
31 cls.unregister_plugin = classmethod(
32 lambda cls: cls.plugins.remove(cls)
33 )