diff -r 8becd52c2171 -r 1bbea188a7e5 tools.py --- a/tools.py Mon Feb 04 19:49:48 2008 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -# -""" -Helper functions and classes -""" -import re - -def str_num_cmp(s1,s2): - """ - string comparator function that will put 'toto_10' after 'toto_2' - Also works for strings like 'toto_1_et_23er25'. - """ - r = re.compile(r'((?<=\d)\D+|(?<=\D)\d+)') - r1= r.split(s1) - r2= r.split(s2) - r1=[not x.isdigit() and x or int(x) for x in r1 if x!=''] - r2=[not x.isdigit() and x or int(x) for x in r2 if x!=''] - return cmp(r1,r2) - - -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))