annotate scripts/msgfmt.py @ 779:087d3ff0aa08 pyntnclick

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