tools.py

Sun, 03 Feb 2008 23:39:27 +0100

author
David Douard <david.douard@logilab.fr>
date
Sun, 03 Feb 2008 23:39:27 +0100
changeset 35
3b7a38af5c42
child 36
cb4124e3e75b
permissions
-rw-r--r--

Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.

35
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 class AbstractRegister(object):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
2 _instance = None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
3 _registered_type = None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
4 def __new__(cls):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5 # implements a singleton
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
6 if cls._instance is None:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
7 #debug('Instanciating %s', cls.__name__)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
8 cls._instance = super(AbstractRegister, cls).__new__(cls)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
9 cls._instance.registered = {}
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
10 cls._instance.accepts = set()
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
11 return cls._instance
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
12
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
13 def add(self, cls):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
14 assert issubclass(cls, self._registered_type)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
15 if cls is self._registered_type:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
16 return
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
17 if cls._accepts is None:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18 return
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
19 #assert isinstance(cls._accepts, (basestring, tuple))
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21 #debug("Registerered %s for %s", cls.__name__)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
22 if not isinstance(cls._accepts, tuple):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
23 cls._accepts = (cls._accepts,)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
24 for key in cls._accepts:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
25 key = self._get_typ(key)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
26 #debug(" new key = %s", key)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
27 self.registered.setdefault(key, []).append(cls)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
28 self.accepts.add(key)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
29
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
30 def _get_typ(self, typ):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
31 if not isinstance(typ, basestring):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
32 if not isinstance(typ, type):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
33 return None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
34 typ = typ.__name__
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
35 return typ
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
36
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
37 def __contains__(self, val):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
38 val = self._get_typ(val)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
39 return val in self.accepts
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
40
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
41 def get_class(self, typ):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
42 item = typ
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
43 if not isinstance(typ, type):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
44 typ = typ.__class__
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
45 name = typ.__name__
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
46 #debug("Looking a widget for %s", typ.__name__)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
47 orig_typ = typ
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
48 while typ is not None:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
49 if typ.__name__ in self.registered:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
50 for w in self.registered[typ.__name__]:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
51 if w._filter is None or w._filter(item):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
52 #debug("Widget for %s is %s", typ.__name__, w)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53 return w#self.registered[typ.__name__]
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
54 if typ.__bases__:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
55 typ = typ.__bases__[0]
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
56 if typ == object:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
57 typ = None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
58 elif typ.__name__ in ("ObjectItem_", "AbstractListItem_"):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
59 typ = orig_typ._type
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
60 else:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
61 typ = None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
62 raise ValueError('No registered class for %s' % (name))

mercurial