ยปCore Development>Code coverage>Tools/pynche/Switchboard.py

Python code coverage for Tools/pynche/Switchboard.py

#countcontent
1n/a"""Switchboard class.
2n/a
3n/aThis class is used to coordinate updates among all Viewers. Every Viewer must
4n/aconform to the following interface:
5n/a
6n/a - it must include a method called update_yourself() which takes three
7n/a arguments; the red, green, and blue values of the selected color.
8n/a
9n/a - When a Viewer selects a color and wishes to update all other Views, it
10n/a should call update_views() on the Switchboard object. Note that the
11n/a Viewer typically does *not* update itself before calling update_views(),
12n/a since this would cause it to get updated twice.
13n/a
14n/aOptionally, Viewers can also implement:
15n/a
16n/a - save_options() which takes an optiondb (a dictionary). Store into this
17n/a dictionary any values the Viewer wants to save in the persistent
18n/a ~/.pynche file. This dictionary is saved using marshal. The namespace
19n/a for the keys is ad-hoc; make sure you don't clobber some other Viewer's
20n/a keys!
21n/a
22n/a - withdraw() which takes no arguments. This is called when Pynche is
23n/a unmapped. All Viewers should implement this.
24n/a
25n/a - colordb_changed() which takes a single argument, an instance of
26n/a ColorDB. This is called whenever the color name database is changed and
27n/a gives a chance for the Viewers to do something on those events. See
28n/a ListViewer for details.
29n/a
30n/aExternal Viewers are found dynamically. Viewer modules should have names such
31n/aas FooViewer.py. If such a named module has a module global variable called
32n/aADDTOVIEW and this variable is true, the Viewer will be added dynamically to
33n/athe `View' menu. ADDTOVIEW contains a string which is used as the menu item
34n/ato display the Viewer (one kludge: if the string contains a `%', this is used
35n/ato indicate that the next character will get an underline in the menu,
36n/aotherwise the first character is underlined).
37n/a
38n/aFooViewer.py should contain a class called FooViewer, and its constructor
39n/ashould take two arguments, an instance of Switchboard, and optionally a Tk
40n/amaster window.
41n/a
42n/a"""
43n/a
44n/aimport sys
45n/aimport marshal
46n/a
47n/a
48n/a
49n/aclass Switchboard:
50n/a def __init__(self, initfile):
51n/a self.__initfile = initfile
52n/a self.__colordb = None
53n/a self.__optiondb = {}
54n/a self.__views = []
55n/a self.__red = 0
56n/a self.__green = 0
57n/a self.__blue = 0
58n/a self.__canceled = 0
59n/a # read the initialization file
60n/a fp = None
61n/a if initfile:
62n/a try:
63n/a try:
64n/a fp = open(initfile, 'rb')
65n/a self.__optiondb = marshal.load(fp)
66n/a if not isinstance(self.__optiondb, dict):
67n/a print('Problem reading options from file:', initfile,
68n/a file=sys.stderr)
69n/a self.__optiondb = {}
70n/a except (IOError, EOFError, ValueError):
71n/a pass
72n/a finally:
73n/a if fp:
74n/a fp.close()
75n/a
76n/a def add_view(self, view):
77n/a self.__views.append(view)
78n/a
79n/a def update_views(self, red, green, blue):
80n/a self.__red = red
81n/a self.__green = green
82n/a self.__blue = blue
83n/a for v in self.__views:
84n/a v.update_yourself(red, green, blue)
85n/a
86n/a def update_views_current(self):
87n/a self.update_views(self.__red, self.__green, self.__blue)
88n/a
89n/a def current_rgb(self):
90n/a return self.__red, self.__green, self.__blue
91n/a
92n/a def colordb(self):
93n/a return self.__colordb
94n/a
95n/a def set_colordb(self, colordb):
96n/a self.__colordb = colordb
97n/a for v in self.__views:
98n/a if hasattr(v, 'colordb_changed'):
99n/a v.colordb_changed(colordb)
100n/a self.update_views_current()
101n/a
102n/a def optiondb(self):
103n/a return self.__optiondb
104n/a
105n/a def save_views(self):
106n/a # save the current color
107n/a self.__optiondb['RED'] = self.__red
108n/a self.__optiondb['GREEN'] = self.__green
109n/a self.__optiondb['BLUE'] = self.__blue
110n/a for v in self.__views:
111n/a if hasattr(v, 'save_options'):
112n/a v.save_options(self.__optiondb)
113n/a # save the name of the file used for the color database. we'll try to
114n/a # load this first.
115n/a self.__optiondb['DBFILE'] = self.__colordb.filename()
116n/a fp = None
117n/a try:
118n/a try:
119n/a fp = open(self.__initfile, 'wb')
120n/a except IOError:
121n/a print('Cannot write options to file:', \
122n/a self.__initfile, file=sys.stderr)
123n/a else:
124n/a marshal.dump(self.__optiondb, fp)
125n/a finally:
126n/a if fp:
127n/a fp.close()
128n/a
129n/a def withdraw_views(self):
130n/a for v in self.__views:
131n/a if hasattr(v, 'withdraw'):
132n/a v.withdraw()
133n/a
134n/a def canceled(self, flag=1):
135n/a self.__canceled = flag
136n/a
137n/a def canceled_p(self):
138n/a return self.__canceled