codekingpro/portable-devtools
114k
1"""2 babel.messages.mofile3 ~~~~~~~~~~~~~~~~~~~~~4 5 Writing of files in the ``gettext`` MO (machine object) format.6 7 :copyright: (c) 2013-2024 by the Babel Team.8 :license: BSD, see LICENSE for more details.9"""10from __future__ import annotations11 12import array13import struct14from typing import TYPE_CHECKING15 16from babel.messages.catalog import Catalog, Message17 18if TYPE_CHECKING:19 from _typeshed import SupportsRead, SupportsWrite20 21LE_MAGIC: int = 0x950412de22BE_MAGIC: int = 0xde12049523 24 25def read_mo(fileobj: SupportsRead[bytes]) -> Catalog:26 """Read a binary MO file from the given file-like object and return a27 corresponding `Catalog` object.28 29 :param fileobj: the file-like object to read the MO file from30 31 :note: The implementation of this function is heavily based on the32 ``GNUTranslations._parse`` method of the ``gettext`` module in the33 standard library.34 """35 catalog = Catalog()36 headers = {}37 38 filename = getattr(fileobj, 'name', '')39 40 buf = fileobj.read()41 buflen = len(buf)42 unpack = struct.unpack43 44 # Parse the .mo file header, which consists of 5 little endian 3245 # bit words.46 magic = unpack('<I', buf[:4])[0] # Are we big endian or little endian?47 if magic == LE_MAGIC:48 version, msgcount, origidx, transidx = unpack('<4I', buf[4:20])49 ii = '<II'50 elif magic == BE_MAGIC:51 version, msgcount, origidx, transidx = unpack('>4I', buf[4:20])52 ii = '>II'53 else:54 raise OSError(0, 'Bad magic number', filename)55 56 # Now put all messages from the .mo file buffer into the catalog57 # dictionary58 for _i in range(msgcount):59 mlen, moff = unpack(ii, buf[origidx:origidx + 8])60 mend = moff + mlen61 tlen, toff = unpack(ii, buf[transidx:transidx + 8])62 tend = toff + tlen63 if mend < buflen and tend < buflen:64 msg = buf[moff:mend]65 tmsg = buf[toff:tend]66 else:67 raise OSError(0, 'File is corrupt', filename)68 69 # See if we're looking at GNU .mo conventions for metadata70 if mlen == 0:71 # Catalog description72 lastkey = key = None73 for item in tmsg.splitlines():74 item = item.strip()75 if not item:76 continue77 if b':' in item:78 key, value = item.split(b':', 1)79 lastkey = key = key.strip().lower()80 headers[key] = value.strip()81 elif lastkey:82 headers[lastkey] += b'\n' + item83 84 if b'\x04' in msg: # context85 ctxt, msg = msg.split(b'\x04')86 else:87 ctxt = None88 89 if b'\x00' in msg: # plural forms90 msg = msg.split(b'\x00')91 tmsg = tmsg.split(b'\x00')92 if catalog.charset:93 msg = [x.decode(catalog.charset) for x in msg]94 tmsg = [x.decode(catalog.charset) for x in tmsg]95 else:96 if catalog.charset:97 msg = msg.decode(catalog.charset)98 tmsg = tmsg.decode(catalog.charset)99 catalog[msg] = Message(msg, tmsg, context=ctxt)100 101 # advance to next entry in the seek tables102 origidx += 8103 transidx += 8104 105 catalog.mime_headers = headers.items()106 return catalog107 108 109def write_mo(fileobj: SupportsWrite[bytes], catalog: Catalog, use_fuzzy: bool = False) -> None:110 """Write a catalog to the specified file-like object using the GNU MO file111 format.112 113 >>> import sys114 >>> from babel.messages import Catalog115 >>> from gettext import GNUTranslations116 >>> from io import BytesIO117 118 >>> catalog = Catalog(locale='en_US')119 >>> catalog.add('foo', 'Voh')120 <Message ...>121 >>> catalog.add((u'bar', u'baz'), (u'Bahr', u'Batz'))122 <Message ...>123 >>> catalog.add('fuz', 'Futz', flags=['fuzzy'])124 <Message ...>125 >>> catalog.add('Fizz', '')126 <Message ...>127 >>> catalog.add(('Fuzz', 'Fuzzes'), ('', ''))128 <Message ...>129 >>> buf = BytesIO()130 131 >>> write_mo(buf, catalog)132 >>> x = buf.seek(0)133 >>> translations = GNUTranslations(fp=buf)134 >>> if sys.version_info[0] >= 3:135 ... translations.ugettext = translations.gettext136 ... translations.ungettext = translations.ngettext137 >>> translations.ugettext('foo')138 u'Voh'139 >>> translations.ungettext('bar', 'baz', 1)140 u'Bahr'141 >>> translations.ungettext('bar', 'baz', 2)142 u'Batz'143 >>> translations.ugettext('fuz')144 u'fuz'145 >>> translations.ugettext('Fizz')146 u'Fizz'147 >>> translations.ugettext('Fuzz')148 u'Fuzz'149 >>> translations.ugettext('Fuzzes')150 u'Fuzzes'151 152 :param fileobj: the file-like object to write to153 :param catalog: the `Catalog` instance154 :param use_fuzzy: whether translations marked as "fuzzy" should be included155 in the output156 """157 messages = list(catalog)158 messages[1:] = [m for m in messages[1:]159 if m.string and (use_fuzzy or not m.fuzzy)]160 messages.sort()161 162 ids = strs = b''163 offsets = []164 165 for message in messages:166 # For each string, we need size and file offset. Each string is NUL167 # terminated; the NUL does not count into the size.168 if message.pluralizable:169 msgid = b'\x00'.join([170 msgid.encode(catalog.charset) for msgid in message.id171 ])172 msgstrs = []173 for idx, string in enumerate(message.string):174 if not string:175 msgstrs.append(message.id[min(int(idx), 1)])176 else:177 msgstrs.append(string)178 msgstr = b'\x00'.join([179 msgstr.encode(catalog.charset) for msgstr in msgstrs180 ])181 else:182 msgid = message.id.encode(catalog.charset)183 msgstr = message.string.encode(catalog.charset)184 if message.context:185 msgid = b'\x04'.join([message.context.encode(catalog.charset),186 msgid])187 offsets.append((len(ids), len(msgid), len(strs), len(msgstr)))188 ids += msgid + b'\x00'189 strs += msgstr + b'\x00'190 191 # The header is 7 32-bit unsigned integers. We don't use hash tables, so192 # the keys start right after the index tables.193 keystart = 7 * 4 + 16 * len(messages)194 valuestart = keystart + len(ids)195 196 # The string table first has the list of keys, then the list of values.197 # Each entry has first the size of the string, then the file offset.198 koffsets = []199 voffsets = []200 for o1, l1, o2, l2 in offsets:201 koffsets += [l1, o1 + keystart]202 voffsets += [l2, o2 + valuestart]203 offsets = koffsets + voffsets204 205 fileobj.write(struct.pack('Iiiiiii',206 LE_MAGIC, # magic207 0, # version208 len(messages), # number of entries209 7 * 4, # start of key index210 7 * 4 + len(messages) * 8, # start of value index211 0, 0, # size and offset of hash table212 ) + array.array.tobytes(array.array("i", offsets)) + ids + strs)213 