Fix msgfmt for Python 3 and unicode_literal

This commit is contained in:
Calum Lind 2017-02-12 11:26:31 +00:00
commit 5a9784ff4d

View file

@ -25,7 +25,7 @@ Options:
--version --version
Display version information and exit. Display version information and exit.
""" """
from __future__ import print_function from __future__ import print_function, unicode_literals
import array import array
import ast import ast
@ -34,7 +34,7 @@ import os
import struct import struct
import sys import sys
__version__ = '1.1' __version__ = '1.2'
MESSAGES = {} MESSAGES = {}
@ -61,9 +61,8 @@ def generate():
""" """
Return the generated output. Return the generated output.
""" """
keys = MESSAGES.keys()
# the keys are sorted in the .mo file # the keys are sorted in the .mo file
keys.sort() keys = sorted(MESSAGES.keys())
offsets = [] offsets = []
ids = strs = '' ids = strs = ''
for _id in keys: for _id in keys:
@ -72,7 +71,7 @@ def generate():
offsets.append((len(ids), len(_id), len(strs), len(MESSAGES[_id]))) offsets.append((len(ids), len(_id), len(strs), len(MESSAGES[_id])))
ids += _id + '\0' ids += _id + '\0'
strs += MESSAGES[_id] + '\0' strs += MESSAGES[_id] + '\0'
output = ''
# The header is 7 32-bit unsigned integers. We don't use hash tables, so # The header is 7 32-bit unsigned integers. We don't use hash tables, so
# the keys start right after the index tables. # the keys start right after the index tables.
# translated string. # translated string.
@ -94,9 +93,12 @@ def generate():
7 * 4, # start of key index 7 * 4, # start of key index
7 * 4 + len(keys) * 8, # start of value index 7 * 4 + len(keys) * 8, # start of value index
0, 0) # size and offset of hash table 0, 0) # size and offset of hash table
output += array.array('i', offsets).tostring() if sys.version_info.major == 2:
output += ids output += array.array(b'i', offsets).tostring()
output += strs else:
output += array.array('i', offsets).tobytes()
output += ids.encode('utf-8')
output += strs.encode('utf-8')
return output return output
@ -115,7 +117,8 @@ def make(filename, outfile):
outfile = os.path.splitext(infile)[0] + '.mo' outfile = os.path.splitext(infile)[0] + '.mo'
try: try:
with open(infile) as _file: import io
with io.open(infile, encoding='utf-8') as _file:
lines = _file.readlines() lines = _file.readlines()
except IOError as msg: except IOError as msg:
print(msg, file=sys.stderr) print(msg, file=sys.stderr)
@ -167,6 +170,9 @@ def make(filename, outfile):
if not l: if not l:
continue continue
l = ast.literal_eval(l) l = ast.literal_eval(l)
# Python 2 ast.literal_eval returns bytes.
if isinstance(l, bytes):
l = l.decode('utf-8')
if section == section_id: if section == section_id:
msgid += l msgid += l
elif section == section_str: elif section == section_str: