annotate Tools/i18n/msgfmt.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents fc8c90a44e68
children c160af09c5b0 6b18a095c2e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
1 #! /usr/bin/env python
29042
0559877ada5c Declare encoding. Use unsigned long constants. Fixes #728277.
Martin v. Löwis <martin@v.loewis.de>
parents: 16964
diff changeset
2 # -*- coding: iso-8859-1 -*-
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
3 # Written by Martin v. Löwis <loewis@informatik.hu-berlin.de>
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
4
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
5 """Generate binary message catalog from textual translation description.
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
6
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
7 This program converts a textual Uniforum-style message catalog (.po file) into
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
8 a binary GNU catalog (.mo file). This is essentially the same function as the
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
9 GNU msgfmt program, however, it is a simpler implementation.
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
10
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
11 Usage: msgfmt.py [OPTIONS] filename.po
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
12
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
13 Options:
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
14 -o file
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
15 --output-file=file
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
16 Specify the output file to write to. If omitted, output will go to a
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
17 file named filename.mo (based off the input file name).
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
18
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
19 -h
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
20 --help
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
21 Print this message and exit.
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
22
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
23 -V
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
24 --version
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
25 Display version information and exit.
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
26 """
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
27
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
28 import sys
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
29 import os
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
30 import getopt
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
31 import struct
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
32 import array
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
33
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
34 __version__ = "1.1"
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
35
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
36 MESSAGES = {}
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
37
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
38
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
39
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
40 def usage(code, msg=''):
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
41 print >> sys.stderr, __doc__
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
42 if msg:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
43 print >> sys.stderr, msg
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
44 sys.exit(code)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
45
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
46
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
47
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
48 def add(id, str, fuzzy):
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
49 "Add a non-fuzzy translation to the dictionary."
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
50 global MESSAGES
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
51 if not fuzzy and str:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
52 MESSAGES[id] = str
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
53
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
54
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
55
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
56 def generate():
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
57 "Return the generated output."
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
58 global MESSAGES
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
59 keys = MESSAGES.keys()
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
60 # the keys are sorted in the .mo file
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
61 keys.sort()
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
62 offsets = []
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
63 ids = strs = ''
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
64 for id in keys:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
65 # For each string, we need size and file offset. Each string is NUL
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
66 # terminated; the NUL does not count into the size.
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
67 offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id])))
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
68 ids += id + '\0'
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
69 strs += MESSAGES[id] + '\0'
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
70 output = ''
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
71 # The header is 7 32-bit unsigned integers. We don't use hash tables, so
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
72 # the keys start right after the index tables.
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
73 # translated string.
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
74 keystart = 7*4+16*len(keys)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
75 # and the values start after the keys
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
76 valuestart = keystart + len(ids)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
77 koffsets = []
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
78 voffsets = []
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
79 # The string table first has the list of keys, then the list of values.
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
80 # Each entry has first the size of the string, then the file offset.
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
81 for o1, l1, o2, l2 in offsets:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
82 koffsets += [l1, o1+keystart]
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
83 voffsets += [l2, o2+valuestart]
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
84 offsets = koffsets + voffsets
29042
0559877ada5c Declare encoding. Use unsigned long constants. Fixes #728277.
Martin v. Löwis <martin@v.loewis.de>
parents: 16964
diff changeset
85 output = struct.pack("Iiiiiii",
0559877ada5c Declare encoding. Use unsigned long constants. Fixes #728277.
Martin v. Löwis <martin@v.loewis.de>
parents: 16964
diff changeset
86 0x950412deL, # Magic
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
87 0, # Version
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
88 len(keys), # # of entries
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
89 7*4, # start of key index
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
90 7*4+len(keys)*8, # start of value index
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
91 0, 0) # size and offset of hash table
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
92 output += array.array("i", offsets).tostring()
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
93 output += ids
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
94 output += strs
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
95 return output
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
96
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
97
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
98
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
99 def make(filename, outfile):
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
100 ID = 1
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
101 STR = 2
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
102
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
103 # Compute .mo name from .po name and arguments
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
104 if filename.endswith('.po'):
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
105 infile = filename
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
106 else:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
107 infile = filename + '.po'
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
108 if outfile is None:
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
109 outfile = os.path.splitext(infile)[0] + '.mo'
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
110
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
111 try:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
112 lines = open(infile).readlines()
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
113 except IOError, msg:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
114 print >> sys.stderr, msg
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
115 sys.exit(1)
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 29042
diff changeset
116
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
117 section = None
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
118 fuzzy = 0
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
119
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
120 # Parse the catalog
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
121 lno = 0
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
122 for l in lines:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
123 lno += 1
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
124 # If we get a comment line after a msgstr, this is a new entry
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
125 if l[0] == '#' and section == STR:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
126 add(msgid, msgstr, fuzzy)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
127 section = None
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
128 fuzzy = 0
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
129 # Record a fuzzy mark
37505
fc8c90a44e68 Bug #1451341: find fuzzy marks correctly.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
130 if l[:2] == '#,' and 'fuzzy' in l:
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
131 fuzzy = 1
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
132 # Skip comments
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
133 if l[0] == '#':
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
134 continue
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
135 # Now we are in a msgid section, output previous section
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
136 if l.startswith('msgid'):
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
137 if section == STR:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
138 add(msgid, msgstr, fuzzy)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
139 section = ID
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
140 l = l[5:]
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
141 msgid = msgstr = ''
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
142 # Now we are in a msgstr section
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
143 elif l.startswith('msgstr'):
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
144 section = STR
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
145 l = l[6:]
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
146 # Skip empty lines
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
147 l = l.strip()
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
148 if not l:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
149 continue
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
150 # XXX: Does this always follow Python escape semantics?
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
151 l = eval(l)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
152 if section == ID:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
153 msgid += l
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
154 elif section == STR:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
155 msgstr += l
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
156 else:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
157 print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
158 'before:'
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
159 print >> sys.stderr, l
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
160 sys.exit(1)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
161 # Add last entry
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
162 if section == STR:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
163 add(msgid, msgstr, fuzzy)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
164
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
165 # Compute output
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
166 output = generate()
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
167
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
168 try:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
169 open(outfile,"wb").write(output)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
170 except IOError,msg:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
171 print >> sys.stderr, msg
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 29042
diff changeset
172
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
173
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
174
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
175 def main():
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
176 try:
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
177 opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
178 ['help', 'version', 'output-file='])
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
179 except getopt.error, msg:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
180 usage(1, msg)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
181
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
182 outfile = None
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
183 # parse options
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
184 for opt, arg in opts:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
185 if opt in ('-h', '--help'):
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
186 usage(0)
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
187 elif opt in ('-V', '--version'):
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
188 print >> sys.stderr, "msgfmt.py", __version__
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
189 sys.exit(0)
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
190 elif opt in ('-o', '--output-file'):
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
191 outfile = arg
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
192 # do it
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
193 if not args:
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
194 print >> sys.stderr, 'No input file given'
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
195 print >> sys.stderr, "Try `msgfmt --help' for more information."
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
196 return
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
197
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
198 for filename in args:
16964
6f42c1d55e6a Added -o/--output-file option as per GNU msgfmt to specify the output
Barry Warsaw <barry@python.org>
parents: 14345
diff changeset
199 make(filename, outfile)
14345
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
200
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
201
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
202 if __name__ == '__main__':
432d308c45e8 Tool to generate binary GNU .mo file from .po template files. Written
Barry Warsaw <barry@python.org>
parents:
diff changeset
203 main()