diff -r 022e881b758e -r 3b7a38af5c42 tools.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools.py Sun Feb 03 23:39:27 2008 +0100 @@ -0,0 +1,62 @@ +class AbstractRegister(object): + _instance = None + _registered_type = None + def __new__(cls): + # implements a singleton + if cls._instance is None: + #debug('Instanciating %s', cls.__name__) + cls._instance = super(AbstractRegister, cls).__new__(cls) + cls._instance.registered = {} + cls._instance.accepts = set() + return cls._instance + + def add(self, cls): + assert issubclass(cls, self._registered_type) + if cls is self._registered_type: + return + if cls._accepts is None: + return + #assert isinstance(cls._accepts, (basestring, tuple)) + + #debug("Registerered %s for %s", cls.__name__) + if not isinstance(cls._accepts, tuple): + cls._accepts = (cls._accepts,) + for key in cls._accepts: + key = self._get_typ(key) + #debug(" new key = %s", key) + self.registered.setdefault(key, []).append(cls) + self.accepts.add(key) + + def _get_typ(self, typ): + if not isinstance(typ, basestring): + if not isinstance(typ, type): + return None + typ = typ.__name__ + return typ + + def __contains__(self, val): + val = self._get_typ(val) + return val in self.accepts + + def get_class(self, typ): + item = typ + if not isinstance(typ, type): + typ = typ.__class__ + name = typ.__name__ + #debug("Looking a widget for %s", typ.__name__) + orig_typ = typ + while typ is not None: + if typ.__name__ in self.registered: + for w in self.registered[typ.__name__]: + if w._filter is None or w._filter(item): + #debug("Widget for %s is %s", typ.__name__, w) + return w#self.registered[typ.__name__] + if typ.__bases__: + typ = typ.__bases__[0] + if typ == object: + typ = None + elif typ.__name__ in ("ObjectItem_", "AbstractListItem_"): + typ = orig_typ._type + else: + typ = None + raise ValueError('No registered class for %s' % (name))