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
17 template_header_path
= sys
.argv
[1]
19 localedir
= sys
.argv
[2]
21 # List of supported languages
22 languages
= sys
.argv
[3:]
24 # Escape special characters in C strings
25 def escapeCString (s
):
26 escapeSeqs
= {'\a' : '\\a', '\b' : '\\b', '\f' : '\\f', '\n' : '\\n',
27 '\r' : '\\r', '\t' : '\\t', '\v' : '\\v', '\\' : '\\\\'}
28 # " -> '' is a hack. Quotes (") aren't possible in XML attributes.
29 # Better use Unicode characters for typographic quotes in option
30 # descriptions and translations.
34 # Special case: escape double quote with \u201c or \u201d, depending
35 # on whether it's an open or close quote. This is needed because plain
36 # double quotes are not possible in XML attributes.
38 if i
== len(s
)-1 or s
[i
+1].isspace():
45 elif s
[i
] in escapeSeqs
:
46 r
= r
+ escapeSeqs
[s
[i
]]
52 # Expand escape sequences in C strings (needed for gettext lookup)
53 def expandCString (s
):
54 escapeSeqs
= {'a' : '\a', 'b' : '\b', 'f' : '\f', 'n' : '\n',
55 'r' : '\r', 't' : '\t', 'v' : '\v',
56 '"' : '"', '\\' : '\\'}
71 if (s
[i
] >= '0' and s
[i
] <= '9') or \
72 (s
[i
] >= 'a' and s
[i
] <= 'f') or \
73 (s
[i
] >= 'A' and s
[i
] <= 'F'):
74 num
= num
* 16 + int(s
[i
],16)
83 if s
[i
] >= '0' and s
[i
] <= '7':
84 num
= num
* 8 + int(s
[i
],8)
93 if s
[i
] in escapeSeqs
:
94 r
= r
+ escapeSeqs
[s
[i
]]
96 elif s
[i
] >= '0' and s
[i
] <= '7':
103 elif s
[i
] == 'x' or s
[i
] == 'X':
113 # Expand matches. The first match is always a DESC or DESC_BEGIN match.
114 # Subsequent matches are ENUM matches.
116 # DESC, DESC_BEGIN format: \1 \2=<lang> \3 \4=gettext(" \5=<text> \6=") \7
117 # ENUM format: \1 \2=gettext(" \3=<text> \4=") \5
118 def expandMatches (matches
, translations
, end
=None):
119 assert len(matches
) > 0
120 nTranslations
= len(translations
)
122 # Expand the description+enums for all translations
123 for lang
,trans
in translations
:
125 # Make sure that all but the last line of a simple description
126 # are extended with a backslash.
128 if len(matches
) == 1 and i
< len(translations
) and \
129 not matches
[0].expand (r
'\7').endswith('\\'):
131 # Expand the description line. Need to use ugettext in order to allow
132 # non-ascii unicode chars in the original English descriptions.
133 text
= escapeCString (trans
.ugettext (unicode (expandCString (
134 matches
[0].expand (r
'\5')), "utf-8"))).encode("utf-8")
135 print(matches
[0].expand (r
'\1' + lang
+ r
'\3"' + text
+ r
'"\7') + suffix
)
136 # Expand any subsequent enum lines
137 for match
in matches
[1:]:
138 text
= escapeCString (trans
.ugettext (unicode (expandCString (
139 match
.expand (r
'\3')), "utf-8"))).encode("utf-8")
140 print(match
.expand (r
'\1"' + text
+ r
'"\5'))
142 # Expand description end
146 # Compile a list of translation classes to all supported languages.
147 # The first translation is always a NullTranslations.
148 translations
= [("en", gettext
.NullTranslations())]
149 for lang
in languages
:
151 trans
= gettext
.translation ("options", localedir
, [lang
])
153 sys
.stderr
.write ("Warning: language '%s' not found.\n" % lang
)
155 translations
.append ((lang
, trans
))
157 # Regular expressions:
158 reLibintl_h
= re
.compile (r
'#\s*include\s*<libintl.h>')
159 reDESC
= re
.compile (r
'(\s*DRI_CONF_DESC\s*\(\s*)([a-z]+)(\s*,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
160 reDESC_BEGIN
= re
.compile (r
'(\s*DRI_CONF_DESC_BEGIN\s*\(\s*)([a-z]+)(\s*,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
161 reENUM
= re
.compile (r
'(\s*DRI_CONF_ENUM\s*\([^,]+,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
162 reDESC_END
= re
.compile (r
'\s*DRI_CONF_DESC_END')
165 print("/***********************************************************************\n" \
166 " *** THIS FILE IS GENERATED AUTOMATICALLY. DON'T EDIT! ***\n" \
167 " ***********************************************************************/")
169 # Process the options template and generate options.h with all
171 template
= file (template_header_path
, "r")
173 for line
in template
:
174 if len(descMatches
) > 0:
175 matchENUM
= reENUM
.match (line
)
176 matchDESC_END
= reDESC_END
.match (line
)
178 descMatches
.append (matchENUM
)
180 expandMatches (descMatches
, translations
, line
)
184 "Warning: unexpected line inside description dropped:\n%s\n" \
187 if reLibintl_h
.search (line
):
188 # Ignore (comment out) #include <libintl.h>
189 print("/* %s * commented out by gen_xmlpool.py */" % line
)
191 matchDESC
= reDESC
.match (line
)
192 matchDESC_BEGIN
= reDESC_BEGIN
.match (line
)
194 assert len(descMatches
) == 0
195 expandMatches ([matchDESC
], translations
)
196 elif matchDESC_BEGIN
:
197 assert len(descMatches
) == 0
198 descMatches
= [matchDESC_BEGIN
]
202 if len(descMatches
) > 0:
203 sys
.stderr
.write ("Warning: unterminated description at end of file.\n")
204 expandMatches (descMatches
, translations
)