From patchwork Tue Apr 5 19:08:05 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [1/3] oe.classutils: add module Date: Tue, 05 Apr 2011 19:08:05 -0000 From: Christopher Larson X-Patchwork-Id: 1951 Message-Id: <8109d803d93d3101593825d3ee1abe4d7ac84ac4.1302030219.git.chris_larson@mentor.com> To: openembedded-core@lists.openembedded.org Cc: Chris Larson From: Chris Larson This adds a ClassRegistry utility metaclass, as maintaining a class registry is a fairly common thing to do. Signed-off-by: Chris Larson --- meta/lib/oe/classutils.py | 24 ++++++++++++++++++++++++ 1 files changed, 24 insertions(+), 0 deletions(-) create mode 100644 meta/lib/oe/classutils.py diff --git a/meta/lib/oe/classutils.py b/meta/lib/oe/classutils.py new file mode 100644 index 0000000..855d2fa --- /dev/null +++ b/meta/lib/oe/classutils.py @@ -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]