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