--- z/pygettext.py 2024-07-13 16:32:30.459209124 -0400 +++ roundup/pygettext.py 2024-07-13 17:36:33.591419515 -0400 @@ -11,6 +11,10 @@ # directory (including globbing chars, important for Win32). # Made docstring fit in 80 chars wide displays using pydoc. # +# 2024-07-13 John Rouillard (rouilj@users.sourceforge.net) +# Converted from python 2. + +from __future__ import print_function # for selftesting try: @@ -155,7 +159,7 @@ """) import os -import imp +import importlib import sys import glob import time @@ -164,6 +168,8 @@ import tokenize import operator +from functools import reduce + __version__ = '1.5' default_keywords = ['_'] @@ -185,6 +191,7 @@ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n" "Last-Translator: FULL NAME \\n" "Language-Team: LANGUAGE \\n" +"Language: \\n" "MIME-Version: 1.0\\n" "Content-Type: text/plain; charset=CHARSET\\n" "Content-Transfer-Encoding: ENCODING\\n" @@ -193,9 +200,9 @@ ''') def usage(code, msg=''): - print >> sys.stderr, __doc__ % globals() + print(__doc__ % globals(), file=sys.stderr) if msg: - print >> sys.stderr, msg + print(msg, file=sys.stderr) sys.exit(code) @@ -268,7 +275,7 @@ if len(parts) > 1: # we have a dotted path, import top-level package try: - file, pathname, description = imp.find_module(parts[0], pathlist) + file, pathname, description = importlib.find_module(parts[0], pathlist) if file: file.close() except ImportError: return None @@ -349,8 +356,8 @@ def __call__(self, ttype, tstring, stup, etup, line): # dispatch ## import token -## print >> sys.stderr, 'ttype:', token.tok_name[ttype], \ -## 'tstring:', tstring +## print(('ttype:', token.tok_name[ttype], \ +## 'tstring:', tstring), file=sys.stderr) self.__state(ttype, tstring, stup[0]) def __waiting(self, ttype, tstring, lineno): @@ -409,13 +416,13 @@ elif ttype not in [tokenize.COMMENT, token.INDENT, token.DEDENT, token.NEWLINE, tokenize.NL]: # warn if we see anything else than STRING or whitespace - print >> sys.stderr, _( + print(_( '*** %(file)s:%(lineno)s: Seen unexpected token "%(token)s"' ) % { 'token': tstring, 'file': self.__curfile, 'lineno': self.__lineno - } + }, file=sys.stderr) self.__state = self.__waiting def __addentry(self, msg, lineno=None, isdocstring=0): @@ -434,17 +441,17 @@ timestamp = time.strftime('%Y-%m-%d %H:%M+%Z') # The time stamp in the header doesn't have the same format as that # generated by xgettext... - print >> fp, pot_header % {'time': timestamp, 'version': __version__} + print(pot_header % {'time': timestamp, 'version': + __version__}, file=fp) # Sort the entries. First sort each particular entry's keys, then # sort all the entries by their first item. reverse = {} for k, v in self.__messages.items(): keys = v.keys() - keys.sort() + keys = sorted(keys) reverse.setdefault(tuple(keys), []).append((k, v)) rkeys = reverse.keys() - rkeys.sort() - for rkey in rkeys: + for rkey in sorted(rkeys): rentries = reverse[rkey] rentries.sort() for k, v in rentries: @@ -458,15 +465,15 @@ # lineno) tuples. We want to sort the entries in v first by # file name and then by line number. v = v.keys() - v.sort() + v = sorted(v) if not options.writelocations: pass # location comments are different b/w Solaris and GNU: elif options.locationstyle == options.SOLARIS: for filename, lineno in v: d = {'filename': filename, 'lineno': lineno} - print >>fp, _( - '# File: %(filename)s, line: %(lineno)d') % d + print(_( + '# File: %(filename)s, line: %(lineno)d') % d, file=fp) elif options.locationstyle == options.GNU: # fit as many locations on one line, as long as the # resulting line length doesn't exceed 'options.width' @@ -477,14 +484,14 @@ if len(locline) + len(s) <= options.width: locline = locline + s else: - print >> fp, locline + print(locline, file=fp) locline = "#:" + s if len(locline) > 2: - print >> fp, locline + print(locline, file=fp) if isdocstring: - print >> fp, '#, docstring' - print >> fp, 'msgid', normalize(k) - print >> fp, 'msgstr ""\n' + print('#, docstring', file=fp) + print('msgid', normalize(k), file=fp) + print('msgstr ""\n', file=fp) def main(): @@ -499,7 +506,7 @@ 'style=', 'verbose', 'version', 'width=', 'exclude-file=', 'docstrings', 'no-docstrings', ]) - except getopt.error, msg: + except getopt.error as msg: usage(1, msg) # for holding option values @@ -557,7 +564,7 @@ elif opt in ('-v', '--verbose'): options.verbose = 1 elif opt in ('-V', '--version'): - print _('pygettext.py (xgettext for Python) %s') % __version__ + print(_('pygettext.py (xgettext for Python) %s') % __version__) sys.exit(0) elif opt in ('-w', '--width'): try: @@ -590,8 +597,8 @@ options.toexclude = fp.readlines() fp.close() except IOError: - print >> sys.stderr, _( - "Can't read --exclude-file: %s") % options.excludefilename + print(_( + "Can't read --exclude-file: %s") % options.excludefilename, file=sys.stderr) sys.exit(1) else: options.toexclude = [] @@ -610,21 +617,22 @@ for filename in args: if filename == '-': if options.verbose: - print _('Reading standard input') + print(_('Reading standard input')) fp = sys.stdin closep = 0 else: if options.verbose: - print _('Working on %s') % filename + print(_('Working on %s') % filename) fp = open(filename) closep = 1 try: eater.set_filename(filename) try: - tokenize.tokenize(fp.readline, eater) - except tokenize.TokenError, e: - print >> sys.stderr, '%s: %s, line %d, column %d' % ( - e[0], filename, e[1][0], e[1][1]) + for token in tokenize.generate_tokens(fp.readline): + eater(*token) + except tokenize.TokenError as e: + print('%s: %s, line %d, column %d' % ( + e[0], filename, e[1][0], e[1][1]), file=sys.stderr) finally: if closep: fp.close()