1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

# Copyright 2013 The Distro Tracker Developers 

# See the COPYRIGHT file at the top-level directory of this distribution and 

# at https://deb.li/DTAuthors 

# 

# This file is part of Distro Tracker. It is subject to the license terms 

# in the LICENSE file found in the top-level directory of this 

# distribution and at https://deb.li/DTLicense. No part of Distro Tracker, 

# including this file, may be copied, modified, propagated, or distributed 

# except according to the terms contained in the LICENSE file. 

"""Classes to build a plugin mechanism.""" 

 

 

class PluginRegistry(type): 

""" 

A metaclass which any class that wants to behave as a registry can use. 

 

When classes derived from classes which use this metaclass are 

instantiated, they are added to the list :attr:`plugins`. 

The concrete classes using this metaclass are free to decide how to use 

this list. 

 

This metaclass also adds an :meth:`unregister_plugin` classmethod to all 

concrete classes which removes the class from the list of plugins. 

""" 

def __init__(cls, name, bases, attrs): # noqa 

if not hasattr(cls, 'plugins'): 

cls.plugins = [] 

else: 

cls.plugins.append(cls) 

 

cls.unregister_plugin = classmethod( 

lambda cls: cls.plugins.remove(cls) 

)