Saving/Restoring state into ~/.pynche file

This commit is contained in:
Barry Warsaw 1998-10-20 20:45:46 +00:00
parent 28e7b4cce1
commit 8a09e1ccda
10 changed files with 160 additions and 27 deletions

View file

@ -12,13 +12,32 @@ conform to the following interface:
since this would cause it to get updated twice.
"""
from types import DictType
import marshal
class Switchboard:
def __init__(self, colordb):
self.__views = []
def __init__(self, colordb, initfile):
self.__colordb = colordb
self.__optiondb = {}
self.__views = []
self.__red = 0
self.__green = 0
self.__blue = 0
# read the initialization file
fp = None
if initfile:
try:
try:
fp = open(initfile)
self.__optiondb = marshal.load(fp)
if type(self.__optiondb) <> DictType:
print 'Problem reading options from file:', initfile
self.__optiondb = {}
except (IOError, EOFError):
pass
finally:
if fp:
fp.close()
def add_view(self, view):
self.__views.append(view)
@ -38,3 +57,25 @@ class Switchboard:
def colordb(self):
return self.__colordb
def optiondb(self):
return self.__optiondb
def save_views(self, file):
# save the current color
self.__optiondb['RED'] = self.__red
self.__optiondb['GREEN'] = self.__green
self.__optiondb['BLUE'] = self.__blue
for v in self.__views:
v.save_options(self.__optiondb)
fp = None
try:
try:
fp = open(file, 'w')
except IOError:
print 'Cannot write options to file:', file
else:
marshal.dump(self.__optiondb, fp)
finally:
if fp:
fp.close()