Applied invisible=... tag on page,group,notebook and button
[openerp-client.git] / msgfmt.py
blob605a87633064235afa0435cc2f97d35bd26e9385
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
4 # This file is part of Gnomolicious and is distributed under the Python
5 # Software License (http://www.python.org/2.3/license.html).
7 # Permission to use, copy, modify, and distribute this software and its
8 # documentation for any purpose and without fee is hereby granted,
9 # provided that the above copyright notice appear in all copies and that
10 # both that copyright notice and this permission notice appear in
11 # supporting documentation, and that the name of Stichting Mathematisch
12 # Centrum or CWI not be used in advertising or publicity pertaining to
13 # distribution of the software without specific, written prior
14 # permission.
16 # Written by Martin v. Löwis <loewis@informatik.hu-berlin.de>
18 """Generate binary message catalog from textual translation description.
20 This program converts a textual Uniforum-style message catalog (.po file) into
21 a binary GNU catalog (.mo file). This is essentially the same function as the
22 GNU msgfmt program, however, it is a simpler implementation.
24 Usage: msgfmt.py [OPTIONS] filename.po
26 Options:
27 -o file
28 --output-file=file
29 Specify the output file to write to. If omitted, output will go to a
30 file named filename.mo (based off the input file name).
33 --help
34 Print this message and exit.
37 --version
38 Display version information and exit.
39 """
41 import sys
42 import os
43 import getopt
44 import struct
45 import array
47 __version__ = "1.1"
49 def usage(code, msg=''):
50 print >> sys.stderr, __doc__
51 if msg:
52 print >> sys.stderr, msg
53 sys.exit(code)
55 def add(id, str, fuzzy):
56 "Add a non-fuzzy translation to the dictionary."
57 global MESSAGES
58 if not fuzzy and str:
59 MESSAGES[id] = str
61 def generate():
62 "Return the generated output."
63 global MESSAGES
64 keys = MESSAGES.keys()
65 # the keys are sorted in the .mo file
66 keys.sort()
67 offsets = []
68 ids = strs = ''
69 for id in keys:
70 # For each string, we need size and file offset. Each string is NUL
71 # terminated; the NUL does not count into the size.
72 offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id])))
73 ids += id + '\0'
74 strs += MESSAGES[id] + '\0'
75 output = ''
76 # The header is 7 32-bit unsigned integers. We don't use hash tables, so
77 # the keys start right after the index tables.
78 # translated string.
79 keystart = 7*4+16*len(keys)
80 # and the values start after the keys
81 valuestart = keystart + len(ids)
82 koffsets = []
83 voffsets = []
84 # The string table first has the list of keys, then the list of values.
85 # Each entry has first the size of the string, then the file offset.
86 for o1, l1, o2, l2 in offsets:
87 koffsets += [l1, o1+keystart]
88 voffsets += [l2, o2+valuestart]
89 offsets = koffsets + voffsets
90 output = struct.pack("Iiiiiii",
91 0x950412deL, # Magic
92 0, # Version
93 len(keys), # # of entries
94 7*4, # start of key index
95 7*4+len(keys)*8, # start of value index
96 0, 0) # size and offset of hash table
97 output += array.array("i", offsets).tostring()
98 output += ids
99 output += strs
100 return output
104 def make(filename, outfile):
105 global MESSAGES
106 MESSAGES = {}
107 ID = 1
108 STR = 2
110 # Compute .mo name from .po name and arguments
111 if filename.endswith('.po'):
112 infile = filename
113 else:
114 infile = filename + '.po'
115 if outfile is None:
116 outfile = os.path.splitext(infile)[0] + '.mo'
118 try:
119 lines = open(infile).readlines()
120 except IOError, msg:
121 print >> sys.stderr, msg
122 sys.exit(1)
124 section = None
125 fuzzy = 0
127 # Parse the catalog
128 lno = 0
129 for l in lines:
130 lno += 1
131 # If we get a comment line after a msgstr, this is a new entry
132 if l[0] == '#' and section == STR:
133 add(msgid, msgstr, fuzzy)
134 section = None
135 fuzzy = 0
136 # Record a fuzzy mark
137 if l[:2] == '#,' and l.count('fuzzy'):
138 fuzzy = 1
139 # Skip comments
140 if l[0] == '#':
141 continue
142 # Now we are in a msgid section, output previous section
143 if l.startswith('msgid'):
144 if section == STR:
145 add(msgid, msgstr, fuzzy)
146 section = ID
147 l = l[5:]
148 msgid = msgstr = ''
149 # Now we are in a msgstr section
150 elif l.startswith('msgstr'):
151 section = STR
152 l = l[6:]
153 # Skip empty lines
154 l = l.strip()
155 if not l:
156 continue
157 # XXX: Does this always follow Python escape semantics?
158 l = eval(l)
159 if section == ID:
160 msgid += l
161 elif section == STR:
162 msgstr += l
163 else:
164 print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
165 'before:'
166 print >> sys.stderr, l
167 sys.exit(1)
168 # Add last entry
169 if section == STR:
170 add(msgid, msgstr, fuzzy)
172 # Compute output
173 output = generate()
175 try:
176 open(outfile,"wb").write(output)
177 except IOError,msg:
178 print >> sys.stderr, msg
182 def main():
183 try:
184 opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
185 ['help', 'version', 'output-file='])
186 except getopt.error, msg:
187 usage(1, msg)
189 outfile = None
190 # parse options
191 for opt, arg in opts:
192 if opt in ('-h', '--help'):
193 usage(0)
194 elif opt in ('-V', '--version'):
195 print >> sys.stderr, "msgfmt.py", __version__
196 sys.exit(0)
197 elif opt in ('-o', '--output-file'):
198 outfile = arg
199 # do it
200 if not args:
201 print >> sys.stderr, 'No input file given'
202 print >> sys.stderr, "Try `msgfmt --help' for more information."
203 return
205 for filename in args:
206 make(filename, outfile)
209 if __name__ == '__main__':
210 main()