4 # gen_xmlpool.py /path/to/t_option.h localedir lang lang lang ...
6 # For each given language, this script expects to find a .mo file at
7 # `{localedir}/{language}/LC_MESSAGES/options.mo`.
10 from __future__
import print_function
, unicode_literals
18 if sys
.version_info
< (3, 0):
19 gettext_method
= 'ugettext'
21 gettext_method
= 'gettext'
23 # Escape special characters in C strings
25 escapeSeqs
= {'\a' : '\\a', '\b' : '\\b', '\f' : '\\f', '\n' : '\\n',
26 '\r' : '\\r', '\t' : '\\t', '\v' : '\\v', '\\' : '\\\\'}
27 # " -> '' is a hack. Quotes (") aren't possible in XML attributes.
28 # Better use Unicode characters for typographic quotes in option
29 # descriptions and translations.
33 # Special case: escape double quote with \u201c or \u201d, depending
34 # on whether it's an open or close quote. This is needed because plain
35 # double quotes are not possible in XML attributes.
37 if i
== len(s
) - 1 or s
[i
+ 1].isspace():
44 elif s
[i
] in escapeSeqs
:
45 r
= r
+ escapeSeqs
[s
[i
]]
51 # Expand escape sequences in C strings (needed for gettext lookup)
53 escapeSeqs
= {'a' : '\a', 'b' : '\b', 'f' : '\f', 'n' : '\n',
54 'r' : '\r', 't' : '\t', 'v' : '\v',
55 '"' : '"', '\\' : '\\'}
70 if (s
[i
] >= '0' and s
[i
] <= '9') or \
71 (s
[i
] >= 'a' and s
[i
] <= 'f') or \
72 (s
[i
] >= 'A' and s
[i
] <= 'F'):
73 num
= num
* 16 + int(s
[i
],16)
82 if s
[i
] >= '0' and s
[i
] <= '7':
83 num
= num
* 8 + int(s
[i
],8)
92 if s
[i
] in escapeSeqs
:
93 r
= r
+ escapeSeqs
[s
[i
]]
95 elif s
[i
] >= '0' and s
[i
] <= '7':
102 elif s
[i
] == 'x' or s
[i
] == 'X':
112 # Expand matches. The first match is always a DESC or DESC_BEGIN match.
113 # Subsequent matches are ENUM matches.
115 # DESC, DESC_BEGIN format: \1 \2=<lang> \3 \4=gettext(" \5=<text> \6=") \7
116 # ENUM format: \1 \2=gettext(" \3=<text> \4=") \5
117 def expandMatches(matches
, translations
, outfile
, end
=None):
118 assert len(matches
) > 0
119 nTranslations
= len(translations
)
121 # Expand the description+enums for all translations
122 for lang
,trans
in translations
:
124 # Make sure that all but the last line of a simple description
125 # are extended with a backslash.
127 if len(matches
) == 1 and i
< len(translations
) and \
128 not matches
[0].expand(r
'\7').endswith('\\'):
130 text
= escapeCString(getattr(trans
, gettext_method
)(expandCString(
131 matches
[0].expand (r
'\5'))))
132 text
= (matches
[0].expand(r
'\1' + lang
+ r
'\3"' + text
+ r
'"\7') + suffix
)
134 outfile
.write(text
+ '\n')
136 # Expand any subsequent enum lines
137 for match
in matches
[1:]:
138 text
= escapeCString(getattr(trans
, gettext_method
)(expandCString(
139 match
.expand(r
'\3'))))
140 text
= match
.expand(r
'\1"' + text
+ r
'"\5')
142 outfile
.write(text
+ '\n')
144 # Expand description end
148 # Regular expressions:
149 reLibintl_h
= re
.compile(r
'#\s*include\s*<libintl.h>')
150 reDESC
= re
.compile(r
'(\s*DRI_CONF_DESC\s*\(\s*)([a-z]+)(\s*,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
151 reDESC_BEGIN
= re
.compile(r
'(\s*DRI_CONF_DESC_BEGIN\s*\(\s*)([a-z]+)(\s*,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
152 reENUM
= re
.compile(r
'(\s*DRI_CONF_ENUM\s*\([^,]+,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
153 reDESC_END
= re
.compile(r
'\s*DRI_CONF_DESC_END')
157 parser
= argparse
.ArgumentParser()
158 parser
.add_argument('--template', required
=True)
159 parser
.add_argument('--output', required
=True)
160 parser
.add_argument('--localedir', required
=True)
161 parser
.add_argument('--languages', nargs
='*', default
=[])
162 args
= parser
.parse_args()
164 # Compile a list of translation classes to all supported languages.
165 # The first translation is always a NullTranslations.
166 translations
= [("en", gettext
.NullTranslations())]
167 for lang
in args
.languages
:
169 filename
= os
.path
.join(args
.localedir
, '{}.gmo'.format(lang
))
170 with io
.open(filename
, 'rb') as f
:
171 trans
= gettext
.GNUTranslations(f
)
172 except (IOError, OSError):
173 print("Warning: language '%s' not found." % lang
, file=sys
.stderr
)
175 translations
.append((lang
, trans
))
177 with io
.open(args
.output
, mode
='wt', encoding
='utf-8') as output
:
178 output
.write("/* This is file is generated automatically. Don't edit! */\n")
180 # Process the options template and generate options.h with all
182 with io
.open(args
.template
, mode
="rt", encoding
='utf-8') as template
:
184 for line
in template
:
186 matchENUM
= reENUM
.match(line
)
187 matchDESC_END
= reDESC_END
.match(line
)
189 descMatches
.append(matchENUM
)
191 expandMatches(descMatches
, translations
, output
, line
)
194 print("Warning: unexpected line inside description dropped:\n",
195 line
, file=sys
.stderr
)
197 if reLibintl_h
.search(line
):
198 # Ignore (comment out) #include <libintl.h>
199 output
.write("/* %s * commented out by gen_xmlpool.py */\n" % line
)
201 matchDESC
= reDESC
.match(line
)
202 matchDESC_BEGIN
= reDESC_BEGIN
.match(line
)
204 assert not descMatches
205 expandMatches([matchDESC
], translations
, output
)
206 elif matchDESC_BEGIN
:
207 assert not descMatches
208 descMatches
= [matchDESC_BEGIN
]
214 print("Warning: unterminated description at end of file.", file=sys
.stderr
)
215 expandMatches(descMatches
, translations
, output
)
218 if __name__
== '__main__':