summaryrefslogtreecommitdiffstats
path: root/Mac/scripts/mkestrres.py
blob: ae9114b8b628f6d7e3d461312e6c4fc78cb1a9c3 (plain)
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
#
# Create 'Estr' resource from error dictionary
from Res import *
import Res
from Resources import *
import MacOS
import string

READ = 1
WRITE = 2
smAllScripts = -3

def Pstring(str):
	if len(str) > 255:
		raise ValueError, 'String too large'
	return chr(len(str))+str
	
def writeestr(dst, edict):
	"""Create Estr resource file given a dictionary of errors."""
	

	FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
	output = FSpOpenResFile(dst, WRITE)
	UseResFile(output)
	for num in edict.keys():
		res = Resource(Pstring(edict[num]))
		res.AddResource('Estr', num, '')
		res.WriteResource()
	CloseResFile(output)
	
def parsefile(src):
	fp = open(src)
	lines = []
	while 1:
		x = fp.readline()
		if not x:
			break
		x = x[:-1]
		words = string.split(x)
		if x[0] in (' ', '\t'):
			# continuation line
			x = string.join(words)
			lines[-1] = lines[-1] + ' ' + x
		else:
			x = string.join(words)
			lines.append(x)
	dict = {}
	for line in lines:
		words = string.split(line)
		index = eval(words[0])
		if dict.has_key(index):
			print '** Duplicate key:', index
		x = string.join(words[2:])
		if not x:
			x = words[1]
		dict[index] = x
	return dict
			
	
if __name__ == '__main__':
	dict = parsefile('errors.txt')
	writeestr('errors.rsrc', dict)