1 """Parse sys/errno.h and Errors.h and create Estr resource"""
13 ERRNO_PROG
="#define[ \t]+" \
21 ERRORS_PROG
="[ \t]*" \
29 ERRORS_PROG_2
="[ \t]*" \
37 raise ValueError, 'String too large'
38 return chr(len(str))+str
40 def writeestr(dst
, edict
):
41 """Create Estr resource file given a dictionary of errors."""
43 os
.unlink(dst
.as_pathname())
44 Res
.FSpCreateResFile(dst
, 'RSED', 'rsrc', smAllScripts
)
45 output
= Res
.FSpOpenResFile(dst
, WRITE
)
46 Res
.UseResFile(output
)
47 for num
in edict
.keys():
48 res
= Res
.Resource(Pstring(edict
[num
][0]))
49 res
.AddResource('Estr', num
, '')
51 Res
.CloseResFile(output
)
53 def writepython(fp
, dict):
57 fp
.write("%s\t=\t%d\t#%s\n"%(dict[i
][1], i
, dict[i
][0]))
60 def parse_errno_h(fp
, dict):
61 errno_prog
= regex
.compile(ERRNO_PROG
)
62 for line
in fp
.readlines():
63 if errno_prog
.match(line
) > 0:
64 number
= string
.atoi(errno_prog
.group(2))
65 name
= errno_prog
.group(1)
66 desc
= string
.strip(errno_prog
.group(3))
68 if not dict.has_key(number
):
69 dict[number
] = desc
, name
71 print 'DUPLICATE', number
72 print '\t', dict[number
]
73 print '\t', (desc
, name
)
75 def parse_errors_h(fp
, dict):
76 errno_prog
= regex
.compile(ERRORS_PROG
)
77 errno_prog_2
= regex
.compile(ERRORS_PROG_2
)
78 for line
in fp
.readlines():
80 if errno_prog
.match(line
) > 0:
81 number
= string
.atoi(errno_prog
.group(2))
82 name
= errno_prog
.group(1)
83 desc
= string
.strip(errno_prog
.group(3))
85 elif errno_prog_2
.match(line
) > 0:
86 number
= string
.atoi(errno_prog_2
.group(2))
87 name
= errno_prog_2
.group(1)
91 if number
> 0: continue
93 if not dict.has_key(number
):
94 dict[number
] = desc
, name
96 print 'DUPLICATE', number
97 print '\t', dict[number
]
98 print '\t', (desc
, name
)
99 if len(desc
) > len(dict[number
][0]):
100 print 'Pick second one'
101 dict[number
] = desc
, name
105 fss
, ok
= macfs
.PromptGetFile("Where is errno.h?")
107 fp
= open(fss
.as_pathname())
108 parse_errno_h(fp
, dict)
111 fss
, ok
= macfs
.PromptGetFile("Select 2nd errno.h or cancel")
113 fp
= open(fss
.as_pathname())
114 parse_errno_h(fp
, dict)
117 fss
, ok
= macfs
.PromptGetFile("Where is Errors.h?")
119 fp
= open(fss
.as_pathname())
120 parse_errors_h(fp
, dict)
126 fss
, ok
= macfs
.StandardPutFile("Resource output file?", "errors.rsrc")
130 fss
, ok
= macfs
.StandardPutFile("Python output file?", "macerrors.py")
132 fp
= open(fss
.as_pathname(), "w")
133 writepython(fp
, dict)
135 fss
.SetCreatorType('Pyth', 'TEXT')
137 fss
, ok
= macfs
.StandardPutFile("Text output file?", "errors.txt")
139 fp
= open(fss
.as_pathname(), "w")
144 fp
.write("%d\t%s\t%s\n"%(i
, dict[i
][1], dict[i
][0]))
148 if __name__
== '__main__':