imap = map
range_type = range
+ cmp = lambda a, b: (a > b) - (a < b)
else:
text_type = unicode
from itertools import izip, imap
range_type = xrange
+ cmp = cmp
+
number_types = integer_types + (float,)
from babel.dates import format_datetime
from babel.messages.plurals import get_plural
from babel.util import odict, distinct, LOCALTZ, FixedOffsetTimezone
-from babel._compat import string_types, number_types, PY2
+from babel._compat import string_types, number_types, PY2, cmp
__all__ = ['Message', 'Catalog', 'TranslationError']
item = item.strip()
if not item:
continue
- if ':' in item:
- key, value = item.split(':', 1)
+ if b':' in item:
+ key, value = item.split(b':', 1)
lastkey = key = key.strip().lower()
headers[key] = value.strip()
elif lastkey:
- headers[lastkey] += '\n' + item
+ headers[lastkey] += b'\n' + item
- if '\x04' in msg: # context
- ctxt, msg = msg.split('\x04')
+ if b'\x04' in msg: # context
+ ctxt, msg = msg.split(b'\x04')
else:
ctxt = None
- if '\x00' in msg: # plural forms
- msg = msg.split('\x00')
- tmsg = tmsg.split('\x00')
+ if b'\x00' in msg: # plural forms
+ msg = msg.split(b'\x00')
+ tmsg = tmsg.split(b'\x00')
if catalog.charset:
msg = [x.decode(catalog.charset) for x in msg]
tmsg = [x.decode(catalog.charset) for x in tmsg]
messages[1:] = [m for m in messages[1:] if not m.fuzzy]
messages.sort()
- ids = strs = ''
+ ids = strs = b''
offsets = []
for message in messages:
# For each string, we need size and file offset. Each string is NUL
# terminated; the NUL does not count into the size.
if message.pluralizable:
- msgid = '\x00'.join([
+ msgid = b'\x00'.join([
msgid.encode(catalog.charset) for msgid in message.id
])
msgstrs = []
msgstrs.append(message.id[min(int(idx), 1)])
else:
msgstrs.append(string)
- msgstr = '\x00'.join([
+ msgstr = b'\x00'.join([
msgstr.encode(catalog.charset) for msgstr in msgstrs
])
else:
else:
msgstr = message.string.encode(catalog.charset)
if message.context:
- msgid = '\x04'.join([message.context.encode(catalog.charset),
+ msgid = b'\x04'.join([message.context.encode(catalog.charset),
msgid])
offsets.append((len(ids), len(msgid), len(strs), len(msgstr)))
- ids += msgid + '\x00'
- strs += msgstr + '\x00'
+ ids += msgid + b'\x00'
+ strs += msgstr + b'\x00'
# The header is 7 32-bit unsigned integers. We don't use hash tables, so
# the keys start right after the index tables.
import unittest
from babel.messages import mofile, Catalog
-from babel._compat import StringIO
+from babel._compat import BytesIO
class ReadMoTestCase(unittest.TestCase):
self.datadir = os.path.join(os.path.dirname(__file__), 'data')
def test_basics(self):
- mo_file = open(os.path.join(self.datadir, 'project', 'i18n', 'de',
- 'LC_MESSAGES', 'messages.mo'))
+ mo_path = os.path.join(self.datadir, 'project', 'i18n', 'de',
+ 'LC_MESSAGES', 'messages.mo')
+ mo_file = open(mo_path, 'rb')
try:
catalog = mofile.read_mo(mo_file)
self.assertEqual(2, len(catalog))
catalog.add((u'There is', u'There are'), (u'Es gibt', u'Es gibt'))
catalog.add(u'Fizz', '')
catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
- buf = StringIO()
+ buf = BytesIO()
mofile.write_mo(buf, catalog)
buf.seek(0)
translations = gettext.GNUTranslations(fp=buf)
def test_more_plural_forms(self):
catalog2 = Catalog(locale='ru_RU')
catalog2.add(('Fuzz', 'Fuzzes'), ('', '', ''))
- buf = StringIO()
+ buf = BytesIO()
mofile.write_mo(buf, catalog2)