annotate Tools/pynche/Switchboard.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 9253fe86b922
children 46625e6122e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9228
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
1 """Switchboard class.
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
2
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
3 This class is used to coordinate updates among all Viewers. Every Viewer must
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
4 conform to the following interface:
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
5
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
6 - it must include a method called update_yourself() which takes three
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
7 arguments; the red, green, and blue values of the selected color.
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
8
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
9 - When a Viewer selects a color and wishes to update all other Views, it
25875
9253fe86b922 Fix a typo, update (only one) style improvement, whitespace
Barry Warsaw <barry@python.org>
parents: 18463
diff changeset
10 should call update_views() on the Switchboard object. Note that the
9253fe86b922 Fix a typo, update (only one) style improvement, whitespace
Barry Warsaw <barry@python.org>
parents: 18463
diff changeset
11 Viewer typically does *not* update itself before calling update_views(),
9228
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
12 since this would cause it to get updated twice.
10759
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
13
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
14 Optionally, Viewers can also implement:
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
15
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
16 - save_options() which takes an optiondb (a dictionary). Store into this
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
17 dictionary any values the Viewer wants to save in the persistent
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
18 ~/.pynche file. This dictionary is saved using marshal. The namespace
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
19 for the keys is ad-hoc; make sure you don't clobber some other Viewer's
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
20 keys!
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
21
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
22 - withdraw() which takes no arguments. This is called when Pynche is
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
23 unmapped. All Viewers should implement this.
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
24
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
25 - colordb_changed() which takes a single argument, an instance of
25875
9253fe86b922 Fix a typo, update (only one) style improvement, whitespace
Barry Warsaw <barry@python.org>
parents: 18463
diff changeset
26 ColorDB. This is called whenever the color name database is changed and
10759
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
27 gives a chance for the Viewers to do something on those events. See
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
28 ListViewer for details.
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
29
25875
9253fe86b922 Fix a typo, update (only one) style improvement, whitespace
Barry Warsaw <barry@python.org>
parents: 18463
diff changeset
30 External Viewers are found dynamically. Viewer modules should have names such
10759
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
31 as FooViewer.py. If such a named module has a module global variable called
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
32 ADDTOVIEW and this variable is true, the Viewer will be added dynamically to
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
33 the `View' menu. ADDTOVIEW contains a string which is used as the menu item
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
34 to display the Viewer (one kludge: if the string contains a `%', this is used
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
35 to indicate that the next character will get an underline in the menu,
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
36 otherwise the first character is underlined).
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
37
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
38 FooViewer.py should contain a class called FooViewer, and its constructor
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
39 should take two arguments, an instance of Switchboard, and optionally a Tk
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
40 master window.
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
41
9228
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
42 """
a03c6fd81d4d Docstring
Barry Warsaw <barry@python.org>
parents: 9197
diff changeset
43
9435
63fe85735bdc Error message goes to stderr
Barry Warsaw <barry@python.org>
parents: 9427
diff changeset
44 import sys
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
45 from types import DictType
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
46 import marshal
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
47
18463
939bbc51cd21 __init__(), save_views(): Catch ValueError along with IOError and
Barry Warsaw <barry@python.org>
parents: 10759
diff changeset
48
939bbc51cd21 __init__(), save_views(): Catch ValueError along with IOError and
Barry Warsaw <barry@python.org>
parents: 10759
diff changeset
49
9153
46830e5a0351 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
50 class Switchboard:
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9435
diff changeset
51 def __init__(self, initfile):
9427
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
52 self.__initfile = initfile
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9435
diff changeset
53 self.__colordb = None
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
54 self.__optiondb = {}
9153
46830e5a0351 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
55 self.__views = []
9197
47ccdb7fb623 update_views_current(): New method
Barry Warsaw <barry@python.org>
parents: 9175
diff changeset
56 self.__red = 0
47ccdb7fb623 update_views_current(): New method
Barry Warsaw <barry@python.org>
parents: 9175
diff changeset
57 self.__green = 0
47ccdb7fb623 update_views_current(): New method
Barry Warsaw <barry@python.org>
parents: 9175
diff changeset
58 self.__blue = 0
9427
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
59 self.__canceled = 0
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
60 # read the initialization file
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
61 fp = None
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
62 if initfile:
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
63 try:
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
64 try:
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
65 fp = open(initfile)
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
66 self.__optiondb = marshal.load(fp)
25875
9253fe86b922 Fix a typo, update (only one) style improvement, whitespace
Barry Warsaw <barry@python.org>
parents: 18463
diff changeset
67 if not isinstance(self.__optiondb, DictType):
18463
939bbc51cd21 __init__(), save_views(): Catch ValueError along with IOError and
Barry Warsaw <barry@python.org>
parents: 10759
diff changeset
68 print >> sys.stderr, \
939bbc51cd21 __init__(), save_views(): Catch ValueError along with IOError and
Barry Warsaw <barry@python.org>
parents: 10759
diff changeset
69 'Problem reading options from file:', initfile
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
70 self.__optiondb = {}
18463
939bbc51cd21 __init__(), save_views(): Catch ValueError along with IOError and
Barry Warsaw <barry@python.org>
parents: 10759
diff changeset
71 except (IOError, EOFError, ValueError):
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
72 pass
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
73 finally:
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
74 if fp:
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
75 fp.close()
9153
46830e5a0351 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
76
46830e5a0351 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
77 def add_view(self, view):
46830e5a0351 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
78 self.__views.append(view)
46830e5a0351 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
79
9166
6a559dc78c91 simplify
Barry Warsaw <barry@python.org>
parents: 9153
diff changeset
80 def update_views(self, red, green, blue):
9197
47ccdb7fb623 update_views_current(): New method
Barry Warsaw <barry@python.org>
parents: 9175
diff changeset
81 self.__red = red
47ccdb7fb623 update_views_current(): New method
Barry Warsaw <barry@python.org>
parents: 9175
diff changeset
82 self.__green = green
47ccdb7fb623 update_views_current(): New method
Barry Warsaw <barry@python.org>
parents: 9175
diff changeset
83 self.__blue = blue
9153
46830e5a0351 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
84 for v in self.__views:
9166
6a559dc78c91 simplify
Barry Warsaw <barry@python.org>
parents: 9153
diff changeset
85 v.update_yourself(red, green, blue)
9175
68aad8ecfd70 Added interface to get/set colordb
Barry Warsaw <barry@python.org>
parents: 9166
diff changeset
86
9197
47ccdb7fb623 update_views_current(): New method
Barry Warsaw <barry@python.org>
parents: 9175
diff changeset
87 def update_views_current(self):
47ccdb7fb623 update_views_current(): New method
Barry Warsaw <barry@python.org>
parents: 9175
diff changeset
88 self.update_views(self.__red, self.__green, self.__blue)
47ccdb7fb623 update_views_current(): New method
Barry Warsaw <barry@python.org>
parents: 9175
diff changeset
89
9236
39c41220f7ed current_rgb(): New method
Barry Warsaw <barry@python.org>
parents: 9228
diff changeset
90 def current_rgb(self):
39c41220f7ed current_rgb(): New method
Barry Warsaw <barry@python.org>
parents: 9228
diff changeset
91 return self.__red, self.__green, self.__blue
39c41220f7ed current_rgb(): New method
Barry Warsaw <barry@python.org>
parents: 9228
diff changeset
92
9175
68aad8ecfd70 Added interface to get/set colordb
Barry Warsaw <barry@python.org>
parents: 9166
diff changeset
93 def colordb(self):
68aad8ecfd70 Added interface to get/set colordb
Barry Warsaw <barry@python.org>
parents: 9166
diff changeset
94 return self.__colordb
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
95
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9435
diff changeset
96 def set_colordb(self, colordb):
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9435
diff changeset
97 self.__colordb = colordb
10759
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
98 for v in self.__views:
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
99 if hasattr(v, 'colordb_changed'):
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
100 v.colordb_changed(colordb)
aaa5f98cb2d6 Added docstring describing Viewer interface.
Barry Warsaw <barry@python.org>
parents: 10744
diff changeset
101 self.update_views_current()
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9435
diff changeset
102
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
103 def optiondb(self):
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
104 return self.__optiondb
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
105
9427
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
106 def save_views(self):
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
107 # save the current color
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
108 self.__optiondb['RED'] = self.__red
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
109 self.__optiondb['GREEN'] = self.__green
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
110 self.__optiondb['BLUE'] = self.__blue
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
111 for v in self.__views:
9427
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
112 if hasattr(v, 'save_options'):
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
113 v.save_options(self.__optiondb)
10744
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9435
diff changeset
114 # save the name of the file used for the color database. we'll try to
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9435
diff changeset
115 # load this first.
4c9de6fd267b Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents: 9435
diff changeset
116 self.__optiondb['DBFILE'] = self.__colordb.filename()
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
117 fp = None
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
118 try:
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
119 try:
9427
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
120 fp = open(self.__initfile, 'w')
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
121 except IOError:
18463
939bbc51cd21 __init__(), save_views(): Catch ValueError along with IOError and
Barry Warsaw <barry@python.org>
parents: 10759
diff changeset
122 print >> sys.stderr, 'Cannot write options to file:', \
939bbc51cd21 __init__(), save_views(): Catch ValueError along with IOError and
Barry Warsaw <barry@python.org>
parents: 10759
diff changeset
123 self.__initfile
9414
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
124 else:
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
125 marshal.dump(self.__optiondb, fp)
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
126 finally:
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
127 if fp:
c03ffff387f8 Saving/Restoring state into ~/.pynche file
Barry Warsaw <barry@python.org>
parents: 9236
diff changeset
128 fp.close()
9427
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
129
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
130 def withdraw_views(self):
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
131 for v in self.__views:
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
132 if hasattr(v, 'withdraw'):
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
133 v.withdraw()
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
134
9435
63fe85735bdc Error message goes to stderr
Barry Warsaw <barry@python.org>
parents: 9427
diff changeset
135 def canceled(self, flag=1):
63fe85735bdc Error message goes to stderr
Barry Warsaw <barry@python.org>
parents: 9427
diff changeset
136 self.__canceled = flag
9427
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
137
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
138 def canceled_p(self):
7567ef40de77 Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents: 9414
diff changeset
139 return self.__canceled