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