Guido van Rossum | 3c3eda2 | 1995-02-05 17:01:45 +0000 | [diff] [blame] | 1 | # |
| 2 | # Create 'Estr' resource from error dictionary |
| 3 | from Res import * |
| 4 | import Res |
| 5 | from Resources import * |
| 6 | import MacOS |
| 7 | import string |
| 8 | |
| 9 | READ = 1 |
| 10 | WRITE = 2 |
| 11 | smAllScripts = -3 |
| 12 | |
| 13 | def Pstring(str): |
| 14 | if len(str) > 255: |
| 15 | raise ValueError, 'String too large' |
| 16 | return chr(len(str))+str |
| 17 | |
| 18 | def writeestr(dst, edict): |
| 19 | """Create Estr resource file given a dictionary of errors.""" |
| 20 | |
| 21 | |
| 22 | FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts) |
| 23 | output = FSpOpenResFile(dst, WRITE) |
| 24 | UseResFile(output) |
| 25 | for num in edict.keys(): |
| 26 | res = Resource(Pstring(edict[num])) |
| 27 | res.AddResource('Estr', num, '') |
| 28 | res.WriteResource() |
| 29 | CloseResFile(output) |
| 30 | |
| 31 | def parsefile(src): |
| 32 | fp = open(src) |
| 33 | lines = [] |
| 34 | while 1: |
| 35 | x = fp.readline() |
| 36 | if not x: |
| 37 | break |
| 38 | x = x[:-1] |
| 39 | words = string.split(x) |
| 40 | if x[0] in (' ', '\t'): |
| 41 | # continuation line |
| 42 | x = string.join(words) |
| 43 | lines[-1] = lines[-1] + ' ' + x |
| 44 | else: |
| 45 | x = string.join(words) |
| 46 | lines.append(x) |
| 47 | dict = {} |
| 48 | for line in lines: |
| 49 | words = string.split(line) |
| 50 | index = eval(words[0]) |
| 51 | if dict.has_key(index): |
| 52 | print '** Duplicate key:', index |
| 53 | x = string.join(words[2:]) |
| 54 | if not x: |
| 55 | x = words[1] |
| 56 | dict[index] = x |
| 57 | return dict |
| 58 | |
| 59 | |
| 60 | if __name__ == '__main__': |
| 61 | dict = parsefile('errors.txt') |
| 62 | writeestr('errors.rsrc', dict) |