2 # -*- coding: utf-8 -*-
3 #######################################################################################
4 # Plinn - http://plinn.org #
5 # Copyright © 2009 Benoît Pin <pin@cri.ensmp.fr> #
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. #
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. #
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.
29 from xml
.dom
.minidom
import parse
30 from os
.path
import exists
, sep
32 from subprocess
import Popen
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"
47 xmlFileNames
= [name
for name
in os
.listdir('.') if not name
.startswith('.') and name
.endswith('.xml')]
49 for name
in xmlFileNames
:
50 lang
= os
.path
.splitext(name
)[0].split('_')[-1]
51 entries
= getEntries(name
)
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
))
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')
75 encodedEntry
= tuple(map(lambda s
: s
.encode(encoding
), e
))
76 out
.write(''' ('%s', "%s"),\n''' % encodedEntry
)
81 def makePo(lang
, entries
, encoding
='utf-8'):
82 path
= ('..', 'locales', lang
, 'LC_MESSAGES')
85 poFilepath
= poFilepath
+ p
+ sep
86 if not exists(poFilepath
) :
89 poFilepath
= poFilepath
+ 'iso_3166_1.po'
90 out
= open(poFilepath
, 'w')
92 header
= POHEADER
% {'charset':encoding
, 'lang':lang
}
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
)
101 moFilepath
= poFilepath
[:-3] + '.mo'
102 MSGFMT
= "msgfmt -o %s %s" % (moFilepath
, poFilepath
)
103 p
= Popen(MSGFMT
, shell
=True)
108 if __name__
== '__main__' :