bcf26f5a2618e5648def95de2806a2f77eb480e1
[iso_3166_1.git] / _sources / make.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #######################################################################################
4 # Plinn - http://plinn.org #
5 # Copyright © 2009 Benoît Pin <pin@cri.ensmp.fr> #
6 # #
7 # This program is free software; you can redistribute it and/or #
8 # modify it under the terms of the GNU General Public License #
9 # as published by the Free Software Foundation; either version 2 #
10 # of the License, or (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program; if not, write to the Free Software #
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
20 #######################################################################################
21 """ generates python, po and mo files from xml sources.
22
23
24
25 """
26
27
28 import os
29 from xml.dom.minidom import parse
30 from os.path import exists, sep
31 from os import mkdir
32 from subprocess import Popen
33
34 POHEADER ="""msgid ""
35 msgstr ""
36 "Project-Id-Version: ISO-3166_1\\n"
37 "MIME-Version: 1.0\\n"
38 "Content-Type: text/plain; charset=%(charset)s\\n"
39 "Content-Transfer-Encoding: 8bit\\n"
40 "Language-Code: %(lang)s\\n"
41 "Preferred-Encodings: %(charset)s latin1\\n"
42 "Domain: iso_3166_1\\n"
43
44 """
45
46 def main():
47 xmlFileNames = [name for name in os.listdir('.') if not name.startswith('.') and name.endswith('.xml')]
48
49 for name in xmlFileNames:
50 lang = os.path.splitext(name)[0].split('_')[-1]
51 entries = getEntries(name)
52 makePy(lang, entries)
53 makePo(lang, entries)
54
55
56
57
58 def getEntries(name):
59 d = parse(name)
60 countries = []
61 for entry in d.documentElement.getElementsByTagName('ISO_3166-1_Entry') :
62 code = entry.getElementsByTagName('ISO_3166-1_Alpha-2_code')[0].firstChild.nodeValue
63 value = entry.getElementsByTagName('ISO_3166-1_Country_name')[0].firstChild.nodeValue
64 countries.append((code, value))
65
66 return countries
67
68 def makePy(lang, entries, encoding='utf-8'):
69 out = open('../%s.py' % lang, 'w')
70 out.write('# -*- coding: %s -*-\n\n' % encoding)
71 out.write('__allow_access_to_unprotected_subobjects__ = 1\n\n')
72 out.write('countries = (\n')
73
74 for e in entries :
75 encodedEntry = tuple(map(lambda s : s.encode(encoding), e))
76 out.write(''' ('%s', "%s"),\n''' % encodedEntry)
77
78 out.write(' )')
79 out.close()
80
81 def makePo(lang, entries, encoding='utf-8'):
82 path = ('..', 'locales', lang, 'LC_MESSAGES')
83 poFilepath = ''
84 for p in path :
85 poFilepath = poFilepath + p + sep
86 if not exists(poFilepath) :
87 mkdir(poFilepath)
88
89 poFilepath = poFilepath + 'iso_3166_1.po'
90 out = open(poFilepath, 'w')
91
92 header = POHEADER % {'charset':encoding, 'lang':lang}
93 out.write(header)
94
95 for e in entries :
96 id, msg = tuple(map(lambda s : s.encode(encoding), e))
97 out.write('msgid "%s"\n' % id)
98 out.write('msgstr "%s"\n\n' % msg)
99 out.close()
100
101 moFilepath = poFilepath[:-3] + '.mo'
102 MSGFMT = "msgfmt -o %s %s" % (moFilepath, poFilepath)
103 p = Popen(MSGFMT, shell=True)
104 p.wait()
105
106
107
108 if __name__ == '__main__' :
109 main()