blob: da28d02e83ceef1f691a90317c8c2aeb36fec2d1 [file] [log] [blame]
Jack Jansen8a99a8a1997-09-08 13:26:36 +00001from preferences import *
2
3# Names of Python resources
4PREFNAME_NAME="PythonPreferenceFileName"
5
6# Resource IDs in the preferences file
Jack Jansen6b02ac21998-01-06 16:48:49 +00007PATH_ID = 228
8DIR_ID = 228
9POPT_ID = 228
Jack Jansen8a99a8a1997-09-08 13:26:36 +000010GUSI_ID = 10240
11
12# Override IDs (in the applet)
Jack Jansen6b02ac21998-01-06 16:48:49 +000013OVERRIDE_PATH_ID = 229
14OVERRIDE_DIR_ID = 229
15OVERRIDE_POPT_ID = 229
Jack Jansen8a99a8a1997-09-08 13:26:36 +000016OVERRIDE_GUSI_ID = 10241
17
18# version
Jack Jansena5ffeb62001-09-01 22:36:29 +000019CUR_VERSION=8
Jack Jansen8a99a8a1997-09-08 13:26:36 +000020
21preffilename = PstringLoader(AnyResLoader('STR ', resname=PREFNAME_NAME)).load()
22pref_fss = preferencefile(preffilename, 'Pyth', 'pref')
23
24class PoptLoader(VersionLoader):
25 def __init__(self, loader):
Jack Jansena5ffeb62001-09-01 22:36:29 +000026 VersionLoader.__init__(self, "bbbbbbbbbbbbbbbb", loader)
Jack Jansen8a99a8a1997-09-08 13:26:36 +000027
28 def versioncheck(self, data):
29 if data[0] == CUR_VERSION:
30 return data
31 print 'old resource'
32 raise Error, "old resource"
33
34class GusiLoader:
35 def __init__(self, loader):
36 self.loader = loader
37 self.data = None
38
39 def load(self):
40 self.data = self.loader.load()
41 while self.data[10:14] != '0181':
42 self.loader.delete(1)
43 self.loader.load()
44 tp = self.data[0:4]
45 cr = self.data[4:8]
46 flags = ord(self.data[9])
Jack Jansen8a387142001-02-11 01:08:04 +000047 return cr, tp
Jack Jansen8a99a8a1997-09-08 13:26:36 +000048
Jack Jansen8a387142001-02-11 01:08:04 +000049 def save(self, (cr, tp)):
Jack Jansen8a99a8a1997-09-08 13:26:36 +000050 flags = ord(self.data[9])
Jack Jansen8a387142001-02-11 01:08:04 +000051 newdata = tp + cr + self.data[8:]
Jack Jansen8a99a8a1997-09-08 13:26:36 +000052 self.loader.save(newdata)
53
Jack Jansena5ffeb62001-09-01 22:36:29 +000054popt_default_default = NullLoader(chr(CUR_VERSION) + 14*'\0' + '\001')
Jack Jansen8a99a8a1997-09-08 13:26:36 +000055popt_default = AnyResLoader('Popt', POPT_ID, default=popt_default_default)
56popt_loader = ResLoader(pref_fss, 'Popt', POPT_ID, default=popt_default)
57popt = PoptLoader(popt_loader)
58
59dir_default = AnyResLoader('alis', DIR_ID)
60dir = ResLoader(pref_fss, 'alis', DIR_ID, default=dir_default)
61
62gusi_default = AnyResLoader('GU\267I', GUSI_ID)
63gusi_loader = ResLoader(pref_fss, 'GU\267I', GUSI_ID, default=gusi_default)
64gusi = GusiLoader(gusi_loader)
65
66path_default = AnyResLoader('STR#', PATH_ID)
67path_loader = ResLoader(pref_fss, 'STR#', PATH_ID, default=path_default)
68path = StrListLoader(path_loader)
69
70class PythonOptions:
71 def __init__(self, popt=popt, dir=dir, gusi=gusi, path=path):
72 self.popt = popt
73 self.dir = dir
74 self.gusi = gusi
75 self.path = path
76
77 def load(self):
78 dict = {}
79 dict['path'] = self.path.load()
80 diralias = self.dir.load()
81 dirfss, dummy = macfs.RawAlias(diralias).Resolve()
82 dict['dir'] = dirfss
Jack Jansen8a387142001-02-11 01:08:04 +000083 dict['creator'], dict['type'] = self.gusi.load()
Jack Jansen8a99a8a1997-09-08 13:26:36 +000084 flags = self.popt.load()
85 dict['version'], dict['inspect'], dict['verbose'], dict['optimize'], \
Jack Jansen4a5eb962000-09-22 21:50:11 +000086 dict['unbuffered'], dict['debugging'], dummy, dict['keep_console'], \
Jack Jansen0c6d0372000-05-05 23:11:14 +000087 dict['nointopt'], dict['noargs'], dict['tabwarn'], \
Jack Jansena5ffeb62001-09-01 22:36:29 +000088 dict['nosite'], dict['nonavservice'], dict['delayconsole'], \
89 dict['divisionwarn'], dict['unixnewlines'] = flags
Jack Jansen8a99a8a1997-09-08 13:26:36 +000090 return dict
91
92 def save(self, dict):
93 self.path.save(dict['path'])
94 diralias = macfs.FSSpec(dict['dir']).NewAlias().data
95 self.dir.save(diralias)
Jack Jansen8a387142001-02-11 01:08:04 +000096 self.gusi.save((dict['creator'], dict['type']))
Jack Jansen8a99a8a1997-09-08 13:26:36 +000097 flags = dict['version'], dict['inspect'], dict['verbose'], dict['optimize'], \
Jack Jansen4a5eb962000-09-22 21:50:11 +000098 dict['unbuffered'], dict['debugging'], 0, dict['keep_console'], \
Jack Jansen0c6d0372000-05-05 23:11:14 +000099 dict['nointopt'], dict['noargs'], dict['tabwarn'], \
Jack Jansena5ffeb62001-09-01 22:36:29 +0000100 dict['nosite'], dict['nonavservice'], dict['delayconsole'], \
101 dict['divisionwarn'], dict['unixnewlines']
Jack Jansen8a99a8a1997-09-08 13:26:36 +0000102 self.popt.save(flags)
103
104def AppletOptions(file):
105 fss = macfs.FSSpec(file)
106 a_popt = PoptLoader(ResLoader(fss, 'Popt', OVERRIDE_POPT_ID, default=popt_loader))
107 a_dir = ResLoader(fss, 'alis', OVERRIDE_DIR_ID, default=dir)
Jack Jansene86d2871998-01-06 12:11:48 +0000108 a_gusi = GusiLoader(
109 ResLoader(fss, 'GU\267I', OVERRIDE_GUSI_ID, default=gusi_loader))
110 a_path = StrListLoader(
111 ResLoader(fss, 'STR#', OVERRIDE_PATH_ID, default=path_loader))
Jack Jansen8a99a8a1997-09-08 13:26:36 +0000112 return PythonOptions(a_popt, a_dir, a_gusi, a_path)
113
114def _test():
115 import preferences
116 preferences.debug = 1
117 dict = PythonOptions().load()
118 for k in dict.keys():
119 print k, '\t', dict[k]
120
121if __name__ == '__main__':
122 _test()
123