1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

Convert scripts/pygettext.py to python 3

This commit is contained in:
Roderik Ploszek 2018-03-19 18:37:23 +01:00
parent f1161cdae0
commit 92a5a341c5

View file

@ -1,4 +1,4 @@
#! /usr/bin/env python #! /usr/bin/env python3
# -*- coding: iso-8859-1 -*- # -*- coding: iso-8859-1 -*-
# Originally written by Barry Warsaw <barry@zope.com> # Originally written by Barry Warsaw <barry@zope.com>
# #
@ -170,9 +170,6 @@ Options:
If `inputfile' is -, standard input is read. If `inputfile' is -, standard input is read.
""") """)
if sys.version_info > (3,):
basestring = str
__version__ = '1.6con' __version__ = '1.6con'
default_keywords = ['_'] default_keywords = ['_']
@ -204,9 +201,9 @@ msgstr ""
def usage(code, msg=''): def usage(code, msg=''):
print >> sys.stderr, __doc__ % globals() print(__doc__ % globals(), file=sys.stderr)
if msg: if msg:
print >> sys.stderr, msg print(msg, file=sys.stderr)
sys.exit(code) sys.exit(code)
@ -438,12 +435,12 @@ class TokenEater:
if len(data) == 2 and data[0] and data[1]: if len(data) == 2 and data[0] and data[1]:
self.__addentry(tuple(data)) self.__addentry(tuple(data))
elif self.__options.verbose: elif self.__options.verbose:
print >> sys.stderr, _( print(_(
'*** %(file)s:%(lineno)s: incorrect ' '*** %(file)s:%(lineno)s: incorrect '
'ngettext format' 'ngettext format'
) % { ) % {
'file': self.__curfile, 'file': self.__curfile,
'lineno': self.__lineno} 'lineno': self.__lineno}, file=sys.stderr)
else: else:
self.__addentry(EMPTYSTRING.join(self.__data)) self.__addentry(EMPTYSTRING.join(self.__data))
self.__state = self.__waiting self.__state = self.__waiting
@ -458,14 +455,14 @@ class TokenEater:
else: else:
# warn if we see anything else than STRING or whitespace # warn if we see anything else than STRING or whitespace
if self.__options.verbose: if self.__options.verbose:
print >> sys.stderr, _( print(_(
'*** %(file)s:%(lineno)s: Seen unexpected ' '*** %(file)s:%(lineno)s: Seen unexpected '
'token "%(token)s"' 'token "%(token)s"'
) % { ) % {
'token': tstring, 'token': tstring,
'file': self.__curfile, 'file': self.__curfile,
'lineno': self.__lineno 'lineno': self.__lineno
} }, file=sys.stderr)
self.__state = self.__waiting self.__state = self.__waiting
def __addentry(self, msg, lineno=None, isdocstring=0): def __addentry(self, msg, lineno=None, isdocstring=0):
@ -484,15 +481,15 @@ class TokenEater:
timestamp = time.ctime(time.time()) timestamp = time.ctime(time.time())
# The time stamp in the header doesn't have the same format as that # The time stamp in the header doesn't have the same format as that
# generated by xgettext... # 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 the entries. First sort each particular entry's keys, then
# sort all the entries by their first item. # sort all the entries by their first item.
reverse = {} reverse = {}
for k, v in self.__messages.items(): for k, v in self.__messages.items():
keys = v.keys() keys = list(v.keys())
keys.sort() keys.sort()
reverse.setdefault(tuple(keys), []).append((k, v)) reverse.setdefault(tuple(keys), []).append((k, v))
rkeys = reverse.keys() rkeys = list(reverse.keys())
rkeys.sort() rkeys.sort()
for rkey in rkeys: for rkey in rkeys:
rentries = reverse[rkey] rentries = reverse[rkey]
@ -507,7 +504,7 @@ class TokenEater:
# k is the message string, v is a dictionary-set of (filename, # k is the message string, v is a dictionary-set of (filename,
# lineno) tuples. We want to sort the entries in v first by # lineno) tuples. We want to sort the entries in v first by
# file name and then by line number. # file name and then by line number.
v = v.keys() v = list(v.keys())
v.sort() v.sort()
if not options.writelocations: if not options.writelocations:
pass pass
@ -515,8 +512,8 @@ class TokenEater:
elif options.locationstyle == options.SOLARIS: elif options.locationstyle == options.SOLARIS:
for filename, lineno in v: for filename, lineno in v:
d = {'filename': filename, 'lineno': lineno} d = {'filename': filename, 'lineno': lineno}
print >>fp, _( print(_(
'# File: %(filename)s, line: %(lineno)d') % d '# File: %(filename)s, line: %(lineno)d') % d, file=fp)
elif options.locationstyle == options.GNU: elif options.locationstyle == options.GNU:
# fit as many locations on one line, as long as the # fit as many locations on one line, as long as the
# resulting line length doesn't exceeds 'options.width' # resulting line length doesn't exceeds 'options.width'
@ -527,23 +524,23 @@ class TokenEater:
if len(locline) + len(s) <= options.width: if len(locline) + len(s) <= options.width:
locline = locline + s locline = locline + s
else: else:
print >> fp, locline print(locline, file=fp)
locline = "#:" + s locline = "#:" + s
if len(locline) > 2: if len(locline) > 2:
print >> fp, locline print(locline, file=fp)
if isdocstring: if isdocstring:
print >> fp, '#, docstring' print('#, docstring', file=fp)
if isinstance(k, basestring): if isinstance(k, str):
print >> fp, 'msgid', normalize(k) print('msgid', normalize(k), file=fp)
print >> fp, 'msgstr ""\n' print('msgstr ""\n', file=fp)
else: else:
# ngettext # ngettext
assert isinstance(k, tuple) assert isinstance(k, tuple)
assert len(k) == 2 assert len(k) == 2
print >> fp, 'msgid', normalize(k[0]) print('msgid', normalize(k[0]), file=fp)
print >> fp, 'msgid_plural', normalize(k[1]) print('msgid_plural', normalize(k[1]), file=fp)
print >> fp, 'msgstr[0] ""' print('msgstr[0] ""', file=fp)
print >> fp, 'msgstr[1] ""\n' print('msgstr[1] ""\n', file=fp)
def main(): def main():
@ -655,8 +652,8 @@ def main():
options.toexclude = fp.readlines() options.toexclude = fp.readlines()
fp.close() fp.close()
except IOError: except IOError:
print >> sys.stderr, _( print(_(
"Can't read --exclude-file: %s") % options.excludefilename "Can't read --exclude-file: %s") % options.excludefilename, file=sys.stderr)
sys.exit(1) sys.exit(1)
else: else:
options.toexclude = [] options.toexclude = []
@ -681,15 +678,18 @@ def main():
else: else:
if options.verbose: if options.verbose:
print(_('Working on %s') % filename) print(_('Working on %s') % filename)
fp = open(filename) fp = open(filename, 'rb')
closep = 1 closep = 1
try: try:
eater.set_filename(filename) eater.set_filename(filename)
try: try:
tokenize.tokenize(fp.readline, eater) for token_info in tokenize.tokenize(fp.readline):
eater(*token_info)
except tokenize.TokenError as e: except tokenize.TokenError as e:
print >> sys.stderr, '%s: %s, line %d, column %d' % ( print('%s: %s, line %d, column %d' % (
e[0], filename, e[1][0], e[1][1]) e[0], filename, e[1][0], e[1][1]), file=sys.stderr)
except StopTokenizing:
pass
finally: finally:
if closep: if closep:
fp.close() fp.close()
@ -713,7 +713,7 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()
# some more test strings # some more test strings
_(u'a unicode string') _('a unicode string')
# this one creates a warning # this one creates a warning
_('*** Seen unexpected token "%(token)s"') % {'token': 'test'} _('*** Seen unexpected token "%(token)s"') % {'token': 'test'}
_('more' 'than' 'one' 'string') _('more' 'than' 'one' 'string')