Skip to content

Commit 8a09e1c

Browse files
committed
Saving/Restoring state into ~/.pynche file
1 parent 28e7b4c commit 8a09e1c

10 files changed

Lines changed: 160 additions & 27 deletions

File tree

Tools/pynche/ChipViewer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,6 @@ def __buttonrelease(self, event=None):
110110
colorname = self.__nearest.get_color()
111111
red, green, blue = self.__sb.colordb().find_byname(colorname)
112112
self.__sb.update_views(red, green, blue)
113+
114+
def save_options(self, optiondb):
115+
pass

Tools/pynche/DetailsViewer.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
class DetailsViewer:
6464
def __init__(self, switchboard, parent=None):
6565
self.__sb = switchboard
66+
optiondb = switchboard.optiondb()
6667
self.__red, self.__green, self.__blue = switchboard.current_rgb()
6768
# GUI
6869
root = self.__root = Toplevel(parent, class_='Pynche')
@@ -87,21 +88,21 @@ def __init__(self, switchboard, parent=None):
8788
self.__l1 = Label(frame, text='Move Sliders:')
8889
self.__l1.grid(row=1, column=0, sticky=E)
8990
self.__rvar = IntVar()
90-
self.__rvar.set(4)
91+
self.__rvar.set(optiondb.get('RSLIDER', 4))
9192
self.__radio1 = Checkbutton(frame, text='Red',
9293
variable=self.__rvar,
9394
command=self.__effect,
9495
onvalue=4, offvalue=0)
9596
self.__radio1.grid(row=1, column=1, sticky=W)
9697
self.__gvar = IntVar()
97-
self.__gvar.set(2)
98+
self.__gvar.set(optiondb.get('GSLIDER', 2))
9899
self.__radio2 = Checkbutton(frame, text='Green',
99100
variable=self.__gvar,
100101
command=self.__effect,
101102
onvalue=2, offvalue=0)
102103
self.__radio2.grid(row=2, column=1, sticky=W)
103104
self.__bvar = IntVar()
104-
self.__bvar.set(1)
105+
self.__bvar.set(optiondb.get('BSLIDER', 1))
105106
self.__radio3 = Checkbutton(frame, text='Blue',
106107
variable=self.__bvar,
107108
command=self.__effect,
@@ -115,7 +116,7 @@ def __init__(self, switchboard, parent=None):
115116
self.__l3 = Label(frame, text='At boundary:')
116117
self.__l3.grid(row=5, column=0, sticky=E)
117118
self.__boundvar = StringVar()
118-
self.__boundvar.set(STOP)
119+
self.__boundvar.set(optiondb.get('ATBOUND', STOP))
119120
self.__omenu = OptionMenu(frame, self.__boundvar,
120121
STOP, WRAP, RATIO, GRAV)
121122
self.__omenu.grid(row=5, column=1, sticky=W)
@@ -262,3 +263,9 @@ def update_yourself(self, red, green, blue):
262263
self.__red = red
263264
self.__green = green
264265
self.__blue = blue
266+
267+
def save_options(self, optiondb):
268+
optiondb['RSLIDER'] = self.__rvar.get()
269+
optiondb['GSLIDER'] = self.__gvar.get()
270+
optiondb['BSLIDER'] = self.__bvar.get()
271+
optiondb['ATBOUND'] = self.__boundvar.get()

Tools/pynche/ListViewer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class ListViewer:
2222
def __init__(self, switchboard, parent=None):
2323
self.__sb = switchboard
24+
optiondb = switchboard.optiondb()
2425
self.__lastbox = None
2526
self.__dontcenter = 0
2627
# GUI
@@ -76,7 +77,7 @@ def __init__(self, switchboard, parent=None):
7677
#
7778
# Update on click
7879
self.__uoc = BooleanVar()
79-
self.__uoc.set(1)
80+
self.__uoc.set(optiondb.get('UPONCLICK', 1))
8081
self.__uocbtn = Checkbutton(root,
8182
text='Update on Click',
8283
variable=self.__uoc,
@@ -160,3 +161,6 @@ def update_yourself(self, red, green, blue):
160161
ig, ig, ig, y2 = canvas.coords(self.__bboxes[-1])
161162
h = int(canvas['height']) * 0.5
162163
canvas.yview('moveto', (y1-h) / y2)
164+
165+
def save_options(self, optiondb):
166+
optiondb['UPONCLICK'] = self.__uoc.get()

Tools/pynche/Main.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,26 @@
1212
tested on Solaris 2.6. Feedback is greatly appreciated. Send email to
1313
bwarsaw@python.org
1414
15-
Usage: %(PROGRAM)s [-d file] [-h] [initialcolor]
15+
Usage: %(PROGRAM)s [-d file] [-i file] [-X] [-h] [initialcolor]
1616
1717
Where:
1818
--database file
1919
-d file
2020
Alternate location of a color database file
2121
22+
--initfile file
23+
-i file
24+
Alternate location of the initialization file. This file contains a
25+
persistent database of the current Pynche options and color. This
26+
means that Pynche restores its option settings and current color when
27+
it restarts, using this file (unless the -X option is used). The
28+
default is ~/.pynche
29+
30+
--ignore
31+
-X
32+
Ignore the initialization file when starting up. Pynche will still
33+
write the current option settings to this file when it quits.
34+
2235
--help
2336
-h
2437
print this message
@@ -49,7 +62,7 @@
4962
# Solaris OpenWindows
5063
'/usr/openwin/lib/rgb.txt',
5164
# The X11R6.4 rgb.txt file
52-
os.path.join(sys.path[0], 'rgb.txt'),
65+
os.path.join(sys.path[0], 'X/rgb.txt'),
5366
# add more here
5467
]
5568

@@ -95,23 +108,29 @@ def main():
95108
try:
96109
opts, args = getopt.getopt(
97110
sys.argv[1:],
98-
'hd:',
99-
['database=', 'help'])
111+
'hd:i:X',
112+
['database=', 'initfile=', 'ignore', 'help'])
100113
except getopt.error, msg:
101114
usage(1, msg)
102115

103116
if len(args) == 0:
104-
initialcolor = 'grey50'
117+
initialcolor = None
105118
elif len(args) == 1:
106119
initialcolor = args[0]
107120
else:
108121
usage(1)
109122

123+
ignore = 0
124+
initfile = os.path.expanduser('~/.pynche')
110125
for opt, arg in opts:
111126
if opt in ('-h', '--help'):
112127
usage(0)
113128
elif opt in ('-d', '--database'):
114129
RGB_TXT.insert(0, arg)
130+
elif opt in ('-X', '--ignore'):
131+
ignore = 1
132+
elif opt in ('-i', '--initfile'):
133+
initfile = arg
115134

116135
# create the windows and go
117136
for f in RGB_TXT:
@@ -124,11 +143,8 @@ def main():
124143
else:
125144
usage(1, 'No color database file found, see the -d option.')
126145

127-
# get the initial color as components
128-
red, green, blue = initial_color(initialcolor, colordb)
129-
130146
# create all output widgets
131-
s = Switchboard(colordb)
147+
s = Switchboard(colordb, not ignore and initfile)
132148

133149
# create the application window decorations
134150
app = PyncheWidget(__version__, s)
@@ -137,13 +153,30 @@ def main():
137153
s.add_view(StripViewer(s, parent))
138154
s.add_view(ChipViewer(s, parent))
139155
s.add_view(TypeinViewer(s, parent))
156+
157+
# get the initial color as components and set the color on all views. if
158+
# there was no initial color given on the command line, use the one that's
159+
# stored in the option database
160+
if initialcolor is None:
161+
optiondb = s.optiondb()
162+
red = optiondb.get('RED')
163+
green = optiondb.get('GREEN')
164+
blue = optiondb.get('BLUE')
165+
# but if there wasn't any stored in the database, use grey50
166+
if red is None or blue is None or green is None:
167+
red, green, blue = initial_color('grey50', colordb)
168+
else:
169+
red, green, blue = initial_color(initialcolor, colordb)
140170
s.update_views(red, green, blue)
141171

142172
try:
143173
app.start()
144174
except KeyboardInterrupt:
145175
pass
146176

177+
# save the option database
178+
s.save_views(initfile)
179+
147180

148181

149182
if __name__ == '__main__':

Tools/pynche/PyncheWidget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(self, version, switchboard):
6060
# Help menu
6161
#
6262
helpmenu = Menu(menubar, name='help', tearoff=0)
63-
helpmenu.add_command(label='About...',
63+
helpmenu.add_command(label='About Pynche...',
6464
command=self.__popup_about,
6565
underline=0)
6666
#
@@ -111,7 +111,7 @@ def __popup_about(self, event=None):
111111
Copyright (C) 1998 CNRI
112112
All rights reserved
113113
114-
For information about Pynche contact
114+
For information contact
115115
author: Barry A. Warsaw
116116
email : bwarsaw@python.org''' % __version__)
117117

Tools/pynche/StripViewer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,14 @@ def update_yourself(self, red, green, blue):
298298
class StripViewer:
299299
def __init__(self, switchboard, parent=None):
300300
self.__sb = switchboard
301+
optiondb = switchboard.optiondb()
301302
# create a frame inside the parent
302303
self.__frame = Frame(parent) #, relief=GROOVE, borderwidth=2)
303304
self.__frame.grid(row=1, column=0, columnspan=2, sticky='EW')
304-
uwd = BooleanVar()
305-
hexp = BooleanVar()
305+
uwd = self.__uwdvar = BooleanVar()
306+
uwd.set(optiondb.get('UPWHILEDRAG', 0))
307+
hexp = self.__hexpvar = BooleanVar()
308+
hexp.set(optiondb.get('HEXSTRIP', 0))
306309
self.__reds = StripWidget(switchboard, self.__frame,
307310
generator=constant_cyan_generator,
308311
axis=0,
@@ -349,3 +352,7 @@ def update_yourself(self, red, green, blue):
349352
def __togglehex(self, event=None):
350353
red, green, blue = self.__sb.current_rgb()
351354
self.update_yourself(red, green, blue)
355+
356+
def save_options(self, optiondb):
357+
optiondb['UPWHILEDRAG'] = self.__uwdvar.get()
358+
optiondb['HEXSTRIP'] = self.__hexpvar.get()

Tools/pynche/Switchboard.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,32 @@
1212
since this would cause it to get updated twice.
1313
"""
1414

15+
from types import DictType
16+
import marshal
17+
1518
class Switchboard:
16-
def __init__(self, colordb):
17-
self.__views = []
19+
def __init__(self, colordb, initfile):
1820
self.__colordb = colordb
21+
self.__optiondb = {}
22+
self.__views = []
1923
self.__red = 0
2024
self.__green = 0
2125
self.__blue = 0
26+
# read the initialization file
27+
fp = None
28+
if initfile:
29+
try:
30+
try:
31+
fp = open(initfile)
32+
self.__optiondb = marshal.load(fp)
33+
if type(self.__optiondb) <> DictType:
34+
print 'Problem reading options from file:', initfile
35+
self.__optiondb = {}
36+
except (IOError, EOFError):
37+
pass
38+
finally:
39+
if fp:
40+
fp.close()
2241

2342
def add_view(self, view):
2443
self.__views.append(view)
@@ -38,3 +57,25 @@ def current_rgb(self):
3857

3958
def colordb(self):
4059
return self.__colordb
60+
61+
def optiondb(self):
62+
return self.__optiondb
63+
64+
def save_views(self, file):
65+
# save the current color
66+
self.__optiondb['RED'] = self.__red
67+
self.__optiondb['GREEN'] = self.__green
68+
self.__optiondb['BLUE'] = self.__blue
69+
for v in self.__views:
70+
v.save_options(self.__optiondb)
71+
fp = None
72+
try:
73+
try:
74+
fp = open(file, 'w')
75+
except IOError:
76+
print 'Cannot write options to file:', file
77+
else:
78+
marshal.dump(self.__optiondb, fp)
79+
finally:
80+
if fp:
81+
fp.close()

Tools/pynche/TextViewer.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class TextViewer:
2222
def __init__(self, switchboard, parent=None):
2323
self.__sb = switchboard
24+
optiondb = switchboard.optiondb()
2425
root = self.__root = Toplevel(parent, class_='Pynche')
2526
root.protocol('WM_DELETE_WINDOW', self.__withdraw)
2627
root.title('Pynche Text Window')
@@ -33,26 +34,44 @@ def __init__(self, switchboard, parent=None):
3334
# create the text widget
3435
#
3536
self.__text = Text(root, relief=SUNKEN,
36-
background='black',
37-
foreground='white',
37+
background=optiondb.get('TEXTBG', 'black'),
38+
foreground=optiondb.get('TEXTFG', 'white'),
3839
width=35, height=15)
40+
sfg = optiondb.get('TEXT_SFG')
41+
if sfg:
42+
self.__text.configure(selectforeground=sfg)
43+
sbg = optiondb.get('TEXT_SBG')
44+
if sbg:
45+
self.__text.configure(selectbackground=sbg)
46+
ibg = optiondb.get('TEXT_IBG')
47+
if ibg:
48+
self.__text.configure(insertbackground=ibg)
3949
self.__text.pack()
40-
self.__text.insert(0.0, '''\
50+
self.__text.insert(0.0, optiondb.get('TEXT', '''\
4151
Insert some stuff here and play
4252
with the buttons below to see
4353
how the colors interact in
4454
textual displays.
4555
4656
See how the selection can also
4757
be affected by tickling the buttons
48-
and choosing a color.''')
49-
self.__text.tag_add(SEL, 6.0, END)
58+
and choosing a color.'''))
59+
insert = optiondb.get('TEXTINS')
60+
if insert:
61+
self.__text.mark_set(INSERT, insert)
62+
try:
63+
start, end = optiondb.get('TEXTSEL', (6.0, END))
64+
self.__text.tag_add(SEL, start, end)
65+
except ValueError:
66+
# selection wasn't set
67+
pass
68+
self.__text.focus_set()
5069
#
5170
# variables
5271
self.__trackp = BooleanVar()
53-
self.__trackp.set(0)
72+
self.__trackp.set(optiondb.get('TRACKP', 0))
5473
self.__which = IntVar()
55-
self.__which.set(0)
74+
self.__which.set(optiondb.get('WHICH', 0))
5675
#
5776
# track toggle
5877
self.__t = Checkbutton(root, text='Track color changes',
@@ -130,3 +149,15 @@ def update_yourself(self, red, green, blue):
130149
self.__text.configure(selectbackground=colorname)
131150
elif which == 5:
132151
self.__text.configure(insertbackground=colorname)
152+
153+
def save_options(self, optiondb):
154+
optiondb['TRACKP'] = self.__trackp.get()
155+
optiondb['WHICH'] = self.__which.get()
156+
optiondb['TEXT'] = self.__text.get(0.0, 'end - 1c')
157+
optiondb['TEXTSEL'] = self.__text.tag_ranges(SEL)[0:2]
158+
optiondb['TEXTINS'] = self.__text.index(INSERT)
159+
optiondb['TEXTFG'] = self.__text['foreground']
160+
optiondb['TEXTBG'] = self.__text['background']
161+
optiondb['TEXT_SFG'] = self.__text['selectforeground']
162+
optiondb['TEXT_SBG'] = self.__text['selectbackground']
163+
optiondb['TEXT_IBG'] = self.__text['insertbackground']

Tools/pynche/TypeinViewer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ class TypeinViewer:
2020
def __init__(self, switchboard, parent=None):
2121
# non-gui ivars
2222
self.__sb = switchboard
23+
optiondb = switchboard.optiondb()
2324
self.__hexp = BooleanVar()
25+
self.__hexp.set(optiondb.get('HEXTYPE', 0))
2426
self.__uwtyping = BooleanVar()
27+
self.__uwtyping.set(optiondb.get('UPWHILETYPE', 0))
2528
# create the gui
2629
self.__frame = Frame(parent) #, relief=GROOVE, borderwidth=2)
2730
self.__frame.grid(row=3, column=1, sticky='NS')
@@ -130,3 +133,7 @@ def update_yourself(self, red, green, blue):
130133

131134
def hexp_var(self):
132135
return self.__hexp
136+
137+
def save_options(self, optiondb):
138+
optiondb['HEXTYPE'] = self.__hexp.get()
139+
optiondb['UPWHILETYPE'] = self.__uwtyping.get()

Tools/pynche/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)