diff options
Diffstat (limited to 'meta/lib/oe/classutils.py')
| -rw-r--r-- | meta/lib/oe/classutils.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/meta/lib/oe/classutils.py b/meta/lib/oe/classutils.py new file mode 100644 index 0000000000..58188fdd6e --- /dev/null +++ b/meta/lib/oe/classutils.py | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | class ClassRegistry(type): | ||
| 2 | """Maintain a registry of classes, indexed by name. | ||
| 3 | |||
| 4 | Note that this implementation requires that the names be unique, as it uses | ||
| 5 | a dictionary to hold the classes by name. | ||
| 6 | |||
| 7 | The name in the registry can be overridden via the 'name' attribute of the | ||
| 8 | class, and the 'priority' attribute controls priority. The prioritized() | ||
| 9 | method returns the registered classes in priority order. | ||
| 10 | |||
| 11 | Subclasses of ClassRegistry may define an 'implemented' property to exert | ||
| 12 | control over whether the class will be added to the registry (e.g. to keep | ||
| 13 | abstract base classes out of the registry).""" | ||
| 14 | priority = 0 | ||
| 15 | class __metaclass__(type): | ||
| 16 | """Give each ClassRegistry their own registry""" | ||
| 17 | def __init__(cls, name, bases, attrs): | ||
| 18 | cls.registry = {} | ||
| 19 | type.__init__(cls, name, bases, attrs) | ||
| 20 | |||
| 21 | def __init__(cls, name, bases, attrs): | ||
| 22 | super(ClassRegistry, cls).__init__(name, bases, attrs) | ||
| 23 | try: | ||
| 24 | if not cls.implemented: | ||
| 25 | return | ||
| 26 | except AttributeError: | ||
| 27 | pass | ||
| 28 | |||
| 29 | try: | ||
| 30 | cls.name | ||
| 31 | except AttributeError: | ||
| 32 | cls.name = name | ||
| 33 | cls.registry[cls.name] = cls | ||
| 34 | |||
| 35 | @classmethod | ||
| 36 | def prioritized(tcls): | ||
| 37 | return sorted(tcls.registry.values(), | ||
| 38 | key=lambda v: v.priority, reverse=True) | ||
| 39 | |||
| 40 | def unregister(cls): | ||
| 41 | for key in cls.registry.keys(): | ||
| 42 | if cls.registry[key] is cls: | ||
| 43 | del cls.registry[key] | ||
