Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 1 | from preferences import * |
| 2 | |
| 3 | # Names of Python resources |
| 4 | PREFNAME_NAME="PythonPreferenceFileName" |
| 5 | |
| 6 | # Resource IDs in the preferences file |
Jack Jansen | 6b02ac2 | 1998-01-06 16:48:49 +0000 | [diff] [blame] | 7 | PATH_ID = 228 |
| 8 | DIR_ID = 228 |
| 9 | POPT_ID = 228 |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 10 | GUSI_ID = 10240 |
| 11 | |
| 12 | # Override IDs (in the applet) |
Jack Jansen | 6b02ac2 | 1998-01-06 16:48:49 +0000 | [diff] [blame] | 13 | OVERRIDE_PATH_ID = 229 |
| 14 | OVERRIDE_DIR_ID = 229 |
| 15 | OVERRIDE_POPT_ID = 229 |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 16 | OVERRIDE_GUSI_ID = 10241 |
| 17 | |
| 18 | # version |
Jack Jansen | a5ffeb6 | 2001-09-01 22:36:29 +0000 | [diff] [blame] | 19 | CUR_VERSION=8 |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 20 | |
| 21 | preffilename = PstringLoader(AnyResLoader('STR ', resname=PREFNAME_NAME)).load() |
| 22 | pref_fss = preferencefile(preffilename, 'Pyth', 'pref') |
| 23 | |
| 24 | class PoptLoader(VersionLoader): |
| 25 | def __init__(self, loader): |
Jack Jansen | a5ffeb6 | 2001-09-01 22:36:29 +0000 | [diff] [blame] | 26 | VersionLoader.__init__(self, "bbbbbbbbbbbbbbbb", loader) |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 27 | |
| 28 | def versioncheck(self, data): |
| 29 | if data[0] == CUR_VERSION: |
| 30 | return data |
| 31 | print 'old resource' |
| 32 | raise Error, "old resource" |
| 33 | |
| 34 | class 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 Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 47 | return cr, tp |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 48 | |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 49 | def save(self, (cr, tp)): |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 50 | flags = ord(self.data[9]) |
Jack Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 51 | newdata = tp + cr + self.data[8:] |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 52 | self.loader.save(newdata) |
| 53 | |
Jack Jansen | a5ffeb6 | 2001-09-01 22:36:29 +0000 | [diff] [blame] | 54 | popt_default_default = NullLoader(chr(CUR_VERSION) + 14*'\0' + '\001') |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 55 | popt_default = AnyResLoader('Popt', POPT_ID, default=popt_default_default) |
| 56 | popt_loader = ResLoader(pref_fss, 'Popt', POPT_ID, default=popt_default) |
| 57 | popt = PoptLoader(popt_loader) |
| 58 | |
| 59 | dir_default = AnyResLoader('alis', DIR_ID) |
| 60 | dir = ResLoader(pref_fss, 'alis', DIR_ID, default=dir_default) |
| 61 | |
| 62 | gusi_default = AnyResLoader('GU\267I', GUSI_ID) |
| 63 | gusi_loader = ResLoader(pref_fss, 'GU\267I', GUSI_ID, default=gusi_default) |
| 64 | gusi = GusiLoader(gusi_loader) |
| 65 | |
| 66 | path_default = AnyResLoader('STR#', PATH_ID) |
| 67 | path_loader = ResLoader(pref_fss, 'STR#', PATH_ID, default=path_default) |
| 68 | path = StrListLoader(path_loader) |
| 69 | |
| 70 | class 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 Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 83 | dict['creator'], dict['type'] = self.gusi.load() |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 84 | flags = self.popt.load() |
| 85 | dict['version'], dict['inspect'], dict['verbose'], dict['optimize'], \ |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 86 | dict['unbuffered'], dict['debugging'], dummy, dict['keep_console'], \ |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 87 | dict['nointopt'], dict['noargs'], dict['tabwarn'], \ |
Jack Jansen | a5ffeb6 | 2001-09-01 22:36:29 +0000 | [diff] [blame] | 88 | dict['nosite'], dict['nonavservice'], dict['delayconsole'], \ |
| 89 | dict['divisionwarn'], dict['unixnewlines'] = flags |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 90 | 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 Jansen | 8a38714 | 2001-02-11 01:08:04 +0000 | [diff] [blame] | 96 | self.gusi.save((dict['creator'], dict['type'])) |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 97 | flags = dict['version'], dict['inspect'], dict['verbose'], dict['optimize'], \ |
Jack Jansen | 4a5eb96 | 2000-09-22 21:50:11 +0000 | [diff] [blame] | 98 | dict['unbuffered'], dict['debugging'], 0, dict['keep_console'], \ |
Jack Jansen | 0c6d037 | 2000-05-05 23:11:14 +0000 | [diff] [blame] | 99 | dict['nointopt'], dict['noargs'], dict['tabwarn'], \ |
Jack Jansen | a5ffeb6 | 2001-09-01 22:36:29 +0000 | [diff] [blame] | 100 | dict['nosite'], dict['nonavservice'], dict['delayconsole'], \ |
| 101 | dict['divisionwarn'], dict['unixnewlines'] |
Jack Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 102 | self.popt.save(flags) |
| 103 | |
| 104 | def 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 Jansen | e86d287 | 1998-01-06 12:11:48 +0000 | [diff] [blame] | 108 | 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 Jansen | 8a99a8a | 1997-09-08 13:26:36 +0000 | [diff] [blame] | 112 | return PythonOptions(a_popt, a_dir, a_gusi, a_path) |
| 113 | |
| 114 | def _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 | |
| 121 | if __name__ == '__main__': |
| 122 | _test() |
| 123 | |