Comments
Patch
new file mode 100644
@@ -0,0 +1,24 @@
+class ClassRegistry(type):
+ """Maintain a registry of classes, indexed by name.
+
+ The name in the registry can be overridden via the 'name' attribute of the
+ class, and the 'priority' attribute controls priority. The prioritized()
+ method returns the registered classes in priority order."""
+ registry = {}
+ priority = 0
+
+ def __init__(cls, name, bases, attrs):
+ super(ClassRegistry, cls).__init__(name, bases, attrs)
+ if not hasattr(cls, name):
+ cls.name = name
+ cls.registry[cls.name] = cls
+
+ @classmethod
+ def prioritized(tcls):
+ return sorted(tcls.registry.values(),
+ key=lambda v: v.priority, reverse=True)
+
+ def unregister(cls):
+ for key in cls.registry.keys():
+ if cls.registry[key] is cls:
+ del cls.registry[key]