comparison roundup/msgfmt.py @ 5376:64b05e24dbd8

Python 3 preparation: convert print to a function. Tool-assisted patch. It is possible that some "from __future__ import print_function" are not in fact needed, if a file only uses print() with a single string as an argument and so would work fine in Python 2 without that import.
author Joseph Myers <jsm@polyomino.org.uk>
date Tue, 24 Jul 2018 09:54:52 +0000
parents 198b6e810c67
children 64c4e43fbb84
comparison
equal deleted inserted replaced
5375:1ad46057ae4a 5376:64b05e24dbd8
24 -V 24 -V
25 --version 25 --version
26 Display version information and exit. 26 Display version information and exit.
27 """ 27 """
28 28
29 from __future__ import print_function
29 import sys 30 import sys
30 import os 31 import os
31 import getopt 32 import getopt
32 import struct 33 import struct
33 import array 34 import array
37 MESSAGES = {} 38 MESSAGES = {}
38 39
39 40
40 41
41 def usage(code, msg=''): 42 def usage(code, msg=''):
42 print >> sys.stderr, __doc__ 43 print(__doc__, file=sys.stderr)
43 if msg: 44 if msg:
44 print >> sys.stderr, msg 45 print(msg, file=sys.stderr)
45 sys.exit(code) 46 sys.exit(code)
46 47
47 48
48 49
49 def add(id, str, fuzzy): 50 def add(id, str, fuzzy):
115 outfile = os.path.splitext(infile)[0] + '.mo' 116 outfile = os.path.splitext(infile)[0] + '.mo'
116 117
117 try: 118 try:
118 lines = open(infile).readlines() 119 lines = open(infile).readlines()
119 except IOError as msg: 120 except IOError as msg:
120 print >> sys.stderr, msg 121 print(msg, file=sys.stderr)
121 sys.exit(1) 122 sys.exit(1)
122 123
123 # remove UTF-8 Byte Order Mark, if any. 124 # remove UTF-8 Byte Order Mark, if any.
124 # (UCS2 BOMs are not handled because messages in UCS2 cannot be handled) 125 # (UCS2 BOMs are not handled because messages in UCS2 cannot be handled)
125 if lines[0].startswith('\xEF\xBB\xBF'): 126 if lines[0].startswith('\xEF\xBB\xBF'):
174 if section == ID: 175 if section == ID:
175 msgid += l 176 msgid += l
176 elif section == STR: 177 elif section == STR:
177 msgstr += l 178 msgstr += l
178 else: 179 else:
179 print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \ 180 print('Syntax error on %s:%d' % (infile, lno),
180 'before:' 181 'before:', file=sys.stderr)
181 print >> sys.stderr, l 182 print(l, file=sys.stderr)
182 sys.exit(1) 183 sys.exit(1)
183 # Add last entry 184 # Add last entry
184 if section == STR: 185 if section == STR:
185 add(msgid, msgstr, fuzzy) 186 add(msgid, msgstr, fuzzy)
186 187
188 output = generate() 189 output = generate()
189 190
190 try: 191 try:
191 open(outfile,"wb").write(output) 192 open(outfile,"wb").write(output)
192 except IOError as msg: 193 except IOError as msg:
193 print >> sys.stderr, msg 194 print(msg, file=sys.stderr)
194 195
195 196
196 197
197 def main(): 198 def main():
198 try: 199 try:
205 # parse options 206 # parse options
206 for opt, arg in opts: 207 for opt, arg in opts:
207 if opt in ('-h', '--help'): 208 if opt in ('-h', '--help'):
208 usage(0) 209 usage(0)
209 elif opt in ('-V', '--version'): 210 elif opt in ('-V', '--version'):
210 print >> sys.stderr, "msgfmt.py", __version__ 211 print("msgfmt.py", __version__, file=sys.stderr)
211 sys.exit(0) 212 sys.exit(0)
212 elif opt in ('-o', '--output-file'): 213 elif opt in ('-o', '--output-file'):
213 outfile = arg 214 outfile = arg
214 # do it 215 # do it
215 if not args: 216 if not args:
216 print >> sys.stderr, 'No input file given' 217 print('No input file given', file=sys.stderr)
217 print >> sys.stderr, "Try `msgfmt --help' for more information." 218 print("Try `msgfmt --help' for more information.", file=sys.stderr)
218 return 219 return
219 220
220 for filename in args: 221 for filename in args:
221 make(filename, outfile) 222 make(filename, outfile)
222 223

Roundup Issue Tracker: http://roundup-tracker.org/