2 # -*- coding: utf-8 -*-
3 ####################################################
4 # Copyright © 2009 Luxia SAS. All rights reserved. #
7 # - Benoît Pin <pinbe@luxia.fr> #
8 ####################################################
9 """ generates python, po and mo files from xml sources.
17 from xml
.dom
.minidom
import parse
18 from os
.path
import exists
, sep
20 from subprocess
import Popen
24 "Project-Id-Version: ISO-3166_1\\n"
25 "MIME-Version: 1.0\\n"
26 "Content-Type: text/plain; charset=%(charset)s\\n"
27 "Content-Transfer-Encoding: 8bit\\n"
28 "Language-Code: %(lang)s\\n"
29 "Preferred-Encodings: %(charset)s latin1\\n"
30 "Domain: iso_3166_1\\n"
35 xmlFileNames
= [name
for name
in os
.listdir('.') if not name
.startswith('.') and name
.endswith('.xml')]
37 for name
in xmlFileNames
:
38 lang
= os
.path
.splitext(name
)[0].split('_')[-1]
39 entries
= getEntries(name
)
49 for entry
in d
.documentElement
.getElementsByTagName('ISO_3166-1_Entry') :
50 code
= entry
.getElementsByTagName('ISO_3166-1_Alpha-2_code')[0].firstChild
.nodeValue
51 value
= entry
.getElementsByTagName('ISO_3166-1_Country_name')[0].firstChild
.nodeValue
52 countries
.append((code
, value
))
56 def makePy(lang
, entries
, encoding
='utf-8'):
57 out
= open('../%s.py' % lang
, 'w')
58 out
.write('# -*- coding: %s -*-\n\n' % encoding
)
59 out
.write('__allow_access_to_unprotected_subobjects__ = 1\n\n')
60 out
.write('countries = (\n')
63 encodedEntry
= tuple(map(lambda s
: s
.encode(encoding
), e
))
64 out
.write(''' ('%s', "%s"),\n''' % encodedEntry
)
69 def makePo(lang
, entries
, encoding
='utf-8'):
70 path
= ('..', 'locales', lang
, 'LC_MESSAGES')
73 poFilepath
= poFilepath
+ p
+ sep
74 if not exists(poFilepath
) :
77 poFilepath
= poFilepath
+ 'iso_3166_1.po'
78 out
= open(poFilepath
, 'w')
80 header
= POHEADER
% {'charset':encoding
, 'lang':lang
}
84 id, msg
= tuple(map(lambda s
: s
.encode(encoding
), e
))
85 out
.write('msgid "%s"\n' % id)
86 out
.write('msgstr "%s"\n\n' % msg
)
89 moFilepath
= poFilepath
[:-3] + '.mo'
90 MSGFMT
= "msgfmt -o %s %s" % (moFilepath
, poFilepath
)
91 p
= Popen(MSGFMT
, shell
=True)
96 if __name__
== '__main__' :