Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Martin v. Löwis | 8f0bd56 | 2003-05-09 08:59:17 +0000 | [diff] [blame] | 2 | # -*- coding: iso-8859-1 -*- |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 3 | # Written by Martin v. Löwis <loewis@informatik.hu-berlin.de> |
| 4 | |
| 5 | """Generate binary message catalog from textual translation description. |
| 6 | |
| 7 | This program converts a textual Uniforum-style message catalog (.po file) into |
| 8 | a binary GNU catalog (.mo file). This is essentially the same function as the |
| 9 | GNU msgfmt program, however, it is a simpler implementation. |
| 10 | |
| 11 | Usage: msgfmt.py [OPTIONS] filename.po |
| 12 | |
| 13 | Options: |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 14 | -o file |
| 15 | --output-file=file |
| 16 | Specify the output file to write to. If omitted, output will go to a |
| 17 | file named filename.mo (based off the input file name). |
| 18 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 19 | -h |
| 20 | --help |
| 21 | Print this message and exit. |
| 22 | |
| 23 | -V |
| 24 | --version |
| 25 | Display version information and exit. |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 26 | """ |
| 27 | |
| 28 | import sys |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 29 | import os |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 30 | import getopt |
| 31 | import struct |
| 32 | import array |
| 33 | |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 34 | __version__ = "1.1" |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 35 | |
| 36 | MESSAGES = {} |
| 37 | |
| 38 | |
| 39 | |
| 40 | def usage(code, msg=''): |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 41 | print(__doc__, file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 42 | if msg: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 43 | print(msg, file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 44 | sys.exit(code) |
| 45 | |
| 46 | |
| 47 | |
| 48 | def add(id, str, fuzzy): |
| 49 | "Add a non-fuzzy translation to the dictionary." |
| 50 | global MESSAGES |
| 51 | if not fuzzy and str: |
| 52 | MESSAGES[id] = str |
| 53 | |
| 54 | |
| 55 | |
| 56 | def generate(): |
| 57 | "Return the generated output." |
| 58 | global MESSAGES |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 59 | # the keys are sorted in the .mo file |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 60 | keys = sorted(MESSAGES.keys()) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 61 | offsets = [] |
| 62 | ids = strs = '' |
| 63 | for id in keys: |
| 64 | # For each string, we need size and file offset. Each string is NUL |
| 65 | # terminated; the NUL does not count into the size. |
| 66 | offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id]))) |
| 67 | ids += id + '\0' |
| 68 | strs += MESSAGES[id] + '\0' |
| 69 | output = '' |
| 70 | # The header is 7 32-bit unsigned integers. We don't use hash tables, so |
| 71 | # the keys start right after the index tables. |
| 72 | # translated string. |
| 73 | keystart = 7*4+16*len(keys) |
| 74 | # and the values start after the keys |
| 75 | valuestart = keystart + len(ids) |
| 76 | koffsets = [] |
| 77 | voffsets = [] |
| 78 | # The string table first has the list of keys, then the list of values. |
| 79 | # Each entry has first the size of the string, then the file offset. |
| 80 | for o1, l1, o2, l2 in offsets: |
| 81 | koffsets += [l1, o1+keystart] |
| 82 | voffsets += [l2, o2+valuestart] |
| 83 | offsets = koffsets + voffsets |
Martin v. Löwis | 8f0bd56 | 2003-05-09 08:59:17 +0000 | [diff] [blame] | 84 | output = struct.pack("Iiiiiii", |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 85 | 0x950412de, # Magic |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 86 | 0, # Version |
| 87 | len(keys), # # of entries |
| 88 | 7*4, # start of key index |
| 89 | 7*4+len(keys)*8, # start of value index |
| 90 | 0, 0) # size and offset of hash table |
| 91 | output += array.array("i", offsets).tostring() |
| 92 | output += ids |
| 93 | output += strs |
| 94 | return output |
| 95 | |
| 96 | |
| 97 | |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 98 | def make(filename, outfile): |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 99 | ID = 1 |
| 100 | STR = 2 |
| 101 | |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 102 | # Compute .mo name from .po name and arguments |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 103 | if filename.endswith('.po'): |
| 104 | infile = filename |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 105 | else: |
| 106 | infile = filename + '.po' |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 107 | if outfile is None: |
| 108 | outfile = os.path.splitext(infile)[0] + '.mo' |
| 109 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 110 | try: |
| 111 | lines = open(infile).readlines() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 112 | except IOError as msg: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 113 | print(msg, file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 114 | sys.exit(1) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 115 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 116 | section = None |
| 117 | fuzzy = 0 |
| 118 | |
| 119 | # Parse the catalog |
| 120 | lno = 0 |
| 121 | for l in lines: |
| 122 | lno += 1 |
| 123 | # If we get a comment line after a msgstr, this is a new entry |
| 124 | if l[0] == '#' and section == STR: |
| 125 | add(msgid, msgstr, fuzzy) |
| 126 | section = None |
| 127 | fuzzy = 0 |
| 128 | # Record a fuzzy mark |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 129 | if l[:2] == '#,' and 'fuzzy' in l: |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 130 | fuzzy = 1 |
| 131 | # Skip comments |
| 132 | if l[0] == '#': |
| 133 | continue |
| 134 | # Now we are in a msgid section, output previous section |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame^] | 135 | if l.startswith('msgid') and not l.startswith('msgid_plural'): |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 136 | if section == STR: |
| 137 | add(msgid, msgstr, fuzzy) |
| 138 | section = ID |
| 139 | l = l[5:] |
| 140 | msgid = msgstr = '' |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame^] | 141 | is_plural = False |
| 142 | # This is a message with plural forms |
| 143 | elif l.startswith('msgid_plural'): |
| 144 | if section != ID: |
| 145 | print('msgid_plural not preceeded by msgid on %s:%d' % (infile, lno), |
| 146 | file=sys.stderr) |
| 147 | sys.exit(1) |
| 148 | l = l[12:] |
| 149 | msgid += '\0' # separator of singular and plural |
| 150 | is_plural = True |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 151 | # Now we are in a msgstr section |
| 152 | elif l.startswith('msgstr'): |
| 153 | section = STR |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame^] | 154 | if l.startswith('msgstr['): |
| 155 | if not is_plural: |
| 156 | print(sys.stderr, 'plural without msgid_plural on %s:%d' % (infile, lno), |
| 157 | file=sys.stderr) |
| 158 | sys.exit(1) |
| 159 | l = l.split(']', 1)[1] |
| 160 | if msgstr: |
| 161 | msgstr += '\0' # Separator of the various plural forms |
| 162 | else: |
| 163 | if is_plural: |
| 164 | print(sys.stderr, 'indexed msgstr required for plural on %s:%d' % (infile, lno), |
| 165 | file=sys.stderr) |
| 166 | sys.exit(1) |
| 167 | l = l[6:] |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 168 | # Skip empty lines |
| 169 | l = l.strip() |
| 170 | if not l: |
| 171 | continue |
| 172 | # XXX: Does this always follow Python escape semantics? |
| 173 | l = eval(l) |
| 174 | if section == ID: |
| 175 | msgid += l |
| 176 | elif section == STR: |
| 177 | msgstr += l |
| 178 | else: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 179 | print('Syntax error on %s:%d' % (infile, lno), \ |
| 180 | 'before:', file=sys.stderr) |
| 181 | print(l, file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 182 | sys.exit(1) |
| 183 | # Add last entry |
| 184 | if section == STR: |
| 185 | add(msgid, msgstr, fuzzy) |
| 186 | |
| 187 | # Compute output |
| 188 | output = generate() |
| 189 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 190 | try: |
| 191 | open(outfile,"wb").write(output) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 192 | except IOError as msg: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 193 | print(msg, file=sys.stderr) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 194 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 195 | |
| 196 | |
| 197 | def main(): |
| 198 | try: |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 199 | opts, args = getopt.getopt(sys.argv[1:], 'hVo:', |
| 200 | ['help', 'version', 'output-file=']) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 201 | except getopt.error as msg: |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 202 | usage(1, msg) |
| 203 | |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 204 | outfile = None |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 205 | # parse options |
| 206 | for opt, arg in opts: |
| 207 | if opt in ('-h', '--help'): |
| 208 | usage(0) |
| 209 | elif opt in ('-V', '--version'): |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 210 | print("msgfmt.py", __version__, file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 211 | sys.exit(0) |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 212 | elif opt in ('-o', '--output-file'): |
| 213 | outfile = arg |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 214 | # do it |
| 215 | if not args: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 216 | print('No input file given', file=sys.stderr) |
| 217 | print("Try `msgfmt --help' for more information.", file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 218 | return |
| 219 | |
| 220 | for filename in args: |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 221 | make(filename, outfile) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 222 | |
| 223 | |
| 224 | if __name__ == '__main__': |
| 225 | main() |