comparison tools/pygettext.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 0942fe89e82e
comparison
equal deleted inserted replaced
5375:1ad46057ae4a 5376:64b05e24dbd8
12 # directory (including globbing chars, important for Win32). 12 # directory (including globbing chars, important for Win32).
13 # Made docstring fit in 80 chars wide displays using pydoc. 13 # Made docstring fit in 80 chars wide displays using pydoc.
14 # 14 #
15 15
16 # for selftesting 16 # for selftesting
17 from __future__ import print_function
17 try: 18 try:
18 import fintl 19 import fintl
19 _ = fintl.gettext 20 _ = fintl.gettext
20 except ImportError: 21 except ImportError:
21 _ = lambda s: s 22 _ = lambda s: s
195 196
196 ''') 197 ''')
197 198
198 199
199 def usage(code, msg=''): 200 def usage(code, msg=''):
200 print >> sys.stderr, __doc__ % globals() 201 print(__doc__ % globals(), file=sys.stderr)
201 if msg: 202 if msg:
202 print >> sys.stderr, msg 203 print(msg, file=sys.stderr)
203 sys.exit(code) 204 sys.exit(code)
204 205
205 206
206 207
207 escapes = [] 208 escapes = []
421 elif ttype == tokenize.STRING: 422 elif ttype == tokenize.STRING:
422 self.__data.append(safe_eval(tstring)) 423 self.__data.append(safe_eval(tstring))
423 elif ttype not in [tokenize.COMMENT, token.INDENT, token.DEDENT, 424 elif ttype not in [tokenize.COMMENT, token.INDENT, token.DEDENT,
424 token.NEWLINE, tokenize.NL]: 425 token.NEWLINE, tokenize.NL]:
425 # warn if we see anything else than STRING or whitespace 426 # warn if we see anything else than STRING or whitespace
426 print >> sys.stderr, _( 427 print(_(
427 '*** %(file)s:%(lineno)s: Seen unexpected token "%(token)s"' 428 '*** %(file)s:%(lineno)s: Seen unexpected token "%(token)s"'
428 ) % { 429 ) % {
429 'token': tstring, 430 'token': tstring,
430 'file': self.__curfile, 431 'file': self.__curfile,
431 'lineno': self.__lineno 432 'lineno': self.__lineno
432 } 433 }, file=sys.stderr)
433 self.__state = self.__waiting 434 self.__state = self.__waiting
434 435
435 def __addentry(self, msg, lineno=None, isdocstring=0): 436 def __addentry(self, msg, lineno=None, isdocstring=0):
436 if lineno is None: 437 if lineno is None:
437 lineno = self.__lineno 438 lineno = self.__lineno
446 def write(self, fp): 447 def write(self, fp):
447 options = self.__options 448 options = self.__options
448 timestamp = time.strftime('%Y-%m-%d %H:%M+%Z') 449 timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
449 # The time stamp in the header doesn't have the same format as that 450 # The time stamp in the header doesn't have the same format as that
450 # generated by xgettext... 451 # generated by xgettext...
451 print >> fp, pot_header % {'time': timestamp, 'version': __version__} 452 print(pot_header % {'time': timestamp, 'version': __version__}, file=fp)
452 # Sort the entries. First sort each particular entry's keys, then 453 # Sort the entries. First sort each particular entry's keys, then
453 # sort all the entries by their first item. 454 # sort all the entries by their first item.
454 reverse = {} 455 reverse = {}
455 for k, v in self.__messages.items(): 456 for k, v in self.__messages.items():
456 keys = v.keys() 457 keys = v.keys()
477 pass 478 pass
478 # location comments are different b/w Solaris and GNU: 479 # location comments are different b/w Solaris and GNU:
479 elif options.locationstyle == options.SOLARIS: 480 elif options.locationstyle == options.SOLARIS:
480 for filename, lineno in v: 481 for filename, lineno in v:
481 d = {'filename': filename, 'lineno': lineno} 482 d = {'filename': filename, 'lineno': lineno}
482 print >>fp, _( 483 print(_(
483 '# File: %(filename)s, line: %(lineno)d') % d 484 '# File: %(filename)s, line: %(lineno)d') % d, file=fp)
484 elif options.locationstyle == options.GNU: 485 elif options.locationstyle == options.GNU:
485 # fit as many locations on one line, as long as the 486 # fit as many locations on one line, as long as the
486 # resulting line length doesn't exceeds 'options.width' 487 # resulting line length doesn't exceeds 'options.width'
487 locline = '#:' 488 locline = '#:'
488 for filename, lineno in v: 489 for filename, lineno in v:
489 d = {'filename': filename, 'lineno': lineno} 490 d = {'filename': filename, 'lineno': lineno}
490 s = _(' %(filename)s:%(lineno)d') % d 491 s = _(' %(filename)s:%(lineno)d') % d
491 if len(locline) + len(s) <= options.width: 492 if len(locline) + len(s) <= options.width:
492 locline = locline + s 493 locline = locline + s
493 else: 494 else:
494 print >> fp, locline 495 print(locline, file=fp)
495 locline = "#:" + s 496 locline = "#:" + s
496 if len(locline) > 2: 497 if len(locline) > 2:
497 print >> fp, locline 498 print(locline, file=fp)
498 if isdocstring: 499 if isdocstring:
499 print >> fp, '#, docstring' 500 print('#, docstring', file=fp)
500 print >> fp, 'msgid', normalize(k) 501 print('msgid', normalize(k), file=fp)
501 print >> fp, 'msgstr ""\n' 502 print('msgstr ""\n', file=fp)
502 503
503 504
504 505
505 def main(): 506 def main():
506 global default_keywords 507 global default_keywords
570 elif opt in ('-p', '--output-dir'): 571 elif opt in ('-p', '--output-dir'):
571 options.outpath = arg 572 options.outpath = arg
572 elif opt in ('-v', '--verbose'): 573 elif opt in ('-v', '--verbose'):
573 options.verbose = 1 574 options.verbose = 1
574 elif opt in ('-V', '--version'): 575 elif opt in ('-V', '--version'):
575 print _('pygettext.py (xgettext for Python) %s') % __version__ 576 print(_('pygettext.py (xgettext for Python) %s') % __version__)
576 sys.exit(0) 577 sys.exit(0)
577 elif opt in ('-w', '--width'): 578 elif opt in ('-w', '--width'):
578 try: 579 try:
579 options.width = int(arg) 580 options.width = int(arg)
580 except ValueError: 581 except ValueError:
603 try: 604 try:
604 fp = open(options.excludefilename) 605 fp = open(options.excludefilename)
605 options.toexclude = fp.readlines() 606 options.toexclude = fp.readlines()
606 fp.close() 607 fp.close()
607 except IOError: 608 except IOError:
608 print >> sys.stderr, _( 609 print(_(
609 "Can't read --exclude-file: %s") % options.excludefilename 610 "Can't read --exclude-file: %s") % options.excludefilename, file=sys.stderr)
610 sys.exit(1) 611 sys.exit(1)
611 else: 612 else:
612 options.toexclude = [] 613 options.toexclude = []
613 614
614 # resolve args to module lists 615 # resolve args to module lists
623 # slurp through all the files 624 # slurp through all the files
624 eater = TokenEater(options) 625 eater = TokenEater(options)
625 for filename in args: 626 for filename in args:
626 if filename == '-': 627 if filename == '-':
627 if options.verbose: 628 if options.verbose:
628 print _('Reading standard input') 629 print(_('Reading standard input'))
629 fp = sys.stdin 630 fp = sys.stdin
630 closep = 0 631 closep = 0
631 else: 632 else:
632 if options.verbose: 633 if options.verbose:
633 print _('Working on %s') % filename 634 print(_('Working on %s') % filename)
634 fp = open(filename) 635 fp = open(filename)
635 closep = 1 636 closep = 1
636 try: 637 try:
637 eater.set_filename(filename) 638 eater.set_filename(filename)
638 try: 639 try:
639 tokenize.tokenize(fp.readline, eater) 640 tokenize.tokenize(fp.readline, eater)
640 except tokenize.TokenError as e: 641 except tokenize.TokenError as e:
641 print >> sys.stderr, '%s: %s, line %d, column %d' % ( 642 print('%s: %s, line %d, column %d' % (
642 e[0], filename, e[1][0], e[1][1]) 643 e[0], filename, e[1][0], e[1][1]), file=sys.stderr)
643 finally: 644 finally:
644 if closep: 645 if closep:
645 fp.close() 646 fp.close()
646 647
647 # write the output 648 # write the output

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