Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | |
| 2 | class ClassRegistryMeta(type): |
| 3 | """Give each ClassRegistry their own registry""" |
| 4 | def __init__(cls, name, bases, attrs): |
| 5 | cls.registry = {} |
| 6 | type.__init__(cls, name, bases, attrs) |
| 7 | |
| 8 | class ClassRegistry(type, metaclass=ClassRegistryMeta): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | """Maintain a registry of classes, indexed by name. |
| 10 | |
| 11 | Note that this implementation requires that the names be unique, as it uses |
| 12 | a dictionary to hold the classes by name. |
| 13 | |
| 14 | The name in the registry can be overridden via the 'name' attribute of the |
| 15 | class, and the 'priority' attribute controls priority. The prioritized() |
| 16 | method returns the registered classes in priority order. |
| 17 | |
| 18 | Subclasses of ClassRegistry may define an 'implemented' property to exert |
| 19 | control over whether the class will be added to the registry (e.g. to keep |
| 20 | abstract base classes out of the registry).""" |
| 21 | priority = 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | def __init__(cls, name, bases, attrs): |
| 23 | super(ClassRegistry, cls).__init__(name, bases, attrs) |
| 24 | try: |
| 25 | if not cls.implemented: |
| 26 | return |
| 27 | except AttributeError: |
| 28 | pass |
| 29 | |
| 30 | try: |
| 31 | cls.name |
| 32 | except AttributeError: |
| 33 | cls.name = name |
| 34 | cls.registry[cls.name] = cls |
| 35 | |
| 36 | @classmethod |
| 37 | def prioritized(tcls): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 38 | return sorted(list(tcls.registry.values()), |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 39 | key=lambda v: (v.priority, v.name), reverse=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | |
| 41 | def unregister(cls): |
| 42 | for key in cls.registry.keys(): |
| 43 | if cls.registry[key] is cls: |
| 44 | del cls.registry[key] |