1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#
# General parser/loaders for preferences files and such
#
import Res
import macfs
import struct
import MACFS
READ=1
READWRITE=3
Error = "Preferences.Error"
debug = 0
class NullLoader:
def __init__(self, data=None):
self.data = data
def load(self):
if self.data is None:
raise Error, "No default given"
return self.data
def save(self, data):
raise Error, "Cannot save to default value"
def delete(self, deep=0):
if debug:
print 'Attempt to delete default value'
raise Error, "Cannot delete default value"
_defaultdefault = NullLoader()
class ResLoader:
def __init__(self, filename, resid, resnum=None, resname=None, default=_defaultdefault):
self.filename = filename
self.fss = macfs.FSSpec(self.filename)
self.resid = resid
self.resnum = resnum
self.resname = resname
self.default = default
self.data = None
def load(self):
oldrh = Res.CurResFile()
try:
rh = Res.FSpOpenResFile(self.fss, READ)
except Res.Error:
self.data = self.default.load()
return self.data
try:
if self.resname:
handle = Res.Get1NamedResource(self.resid, self.resname)
else:
handle = Res.Get1Resource(self.resid, self.resnum)
except Res.Error:
self.data = self.default.load()
else:
if debug:
print 'Loaded', (self.resid, self.resnum, self.resname), 'from', self.fss.as_pathname()
self.data = handle.data
Res.CloseResFile(rh)
Res.UseResFile(oldrh)
return self.data
def save(self, data):
if self.data is None or self.data != data:
oldrh = Res.CurResFile()
rh = Res.FSpOpenResFile(self.fss, READWRITE)
try:
handle = Res.Get1Resource(self.resid, self.resnum)
except Res.Error:
handle = Res.Resource(data)
handle.AddResource(self.resid, self.resnum, '')
if debug:
print 'Added', (self.resid, self.resnum), 'to', self.fss.as_pathname()
else:
handle.data = data
handle.ChangedResource()
if debug:
print 'Changed', (self.resid, self.resnum), 'in', self.fss.as_pathname()
Res.CloseResFile(rh)
Res.UseResFile(oldrh)
def delete(self, deep=0):
if debug:
print 'Deleting in', self.fss.as_pathname(), `self.data`, deep
oldrh = Res.CurResFile()
rh = Res.FSpOpenResFile(self.fss, READWRITE)
try:
handle = Res.Get1Resource(self.resid, self.resnum)
except Res.Error:
if deep:
if debug: print 'deep in', self.default
self.default.delete(1)
else:
handle.RemoveResource()
if debug:
print 'Deleted', (self.resid, self.resnum), 'from', self.fss.as_pathname()
self.data = None
Res.CloseResFile(rh)
Res.UseResFile(oldrh)
class AnyResLoader:
def __init__(self, resid, resnum=None, resname=None, default=_defaultdefault):
self.resid = resid
self.resnum = resnum
self.resname = resname
self.default = default
self.data = None
def load(self):
try:
if self.resname:
handle = Res.GetNamedResource(self.resid, self.resname)
else:
handle = Res.GetResource(self.resid, self.resnum)
except Res.Error:
self.data = self.default.load()
else:
self.data = handle.data
return self.data
def save(self, data):
raise Error, "Cannot save AnyResLoader preferences"
def delete(self, deep=0):
raise Error, "Cannot delete AnyResLoader preferences"
class StructLoader:
def __init__(self, format, loader):
self.format = format
self.loader = loader
def load(self):
data = self.loader.load()
return struct.unpack(self.format, data)
def save(self, data):
data = apply(struct.pack, (self.format,)+data)
self.loader.save(data)
def delete(self, deep=0):
self.loader.delete(deep)
class PstringLoader:
def __init__(self, loader):
self.loader = loader
def load(self):
data = self.loader.load()
len = ord(data[0])
return data[1:1+len]
def save(self, data):
if len(data) > 255:
raise Error, "String too big for pascal-style"
self.loader.save(chr(len(data))+data)
def delete(self, deep=0):
self.loader.delete(deep)
class VersionLoader(StructLoader):
def load(self):
while 1:
data = self.loader.load()
if debug:
print 'Versionloader:', `data`
try:
rv = struct.unpack(self.format, data)
rv = self.versioncheck(rv)
return rv
except (struct.error, Error):
self.delete(1)
def versioncheck(self, data):
return data
class StrListLoader:
def __init__(self, loader):
self.loader = loader
def load(self):
data = self.loader.load()
num, = struct.unpack('h', data[:2])
data = data[2:]
rv = []
for i in range(num):
strlen = ord(data[0])
if strlen < 0: strlen = strlen + 256
str = data[1:strlen+1]
data = data[strlen+1:]
rv.append(str)
return rv
def save(self, list):
rv = struct.pack('h', len(list))
for str in list:
rv = rv + chr(len(str)) + str
self.loader.save(rv)
def delete(self, deep=0):
self.loader.delete(deep)
def preferencefile(filename, creator=None, type=None):
create = creator != None and type != None
vrefnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk, 'pref', create)
fss = macfs.FSSpec((vrefnum, dirid, filename))
oldrf = Res.CurResFile()
if create:
try:
rh = Res.FSpOpenResFile(fss, READ)
except Res.Error:
Res.FSpCreateResFile(fss, creator, type, MACFS.smAllScripts)
else:
Res.CloseResFile(rh)
Res.UseResFile(oldrf)
return fss
|