+++ /dev/null
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-#######################################################################################
-# Plinn - http://plinn.org #
-# Copyright © 2009 Benoît Pin <pin@cri.ensmp.fr> #
-# #
-# This program is free software; you can redistribute it and/or #
-# modify it under the terms of the GNU General Public License #
-# as published by the Free Software Foundation; either version 2 #
-# of the License, or (at your option) any later version. #
-# #
-# This program is distributed in the hope that it will be useful, #
-# but WITHOUT ANY WARRANTY; without even the implied warranty of #
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
-# GNU General Public License for more details. #
-# #
-# You should have received a copy of the GNU General Public License #
-# along with this program; if not, write to the Free Software #
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
-#######################################################################################
-""" generates python, po and mo files from xml sources.
-
-
-
-"""
-
-
-import os
-from xml.dom.minidom import parse
-from os.path import exists, sep
-from os import mkdir
-from subprocess import Popen
-
-POHEADER ="""msgid ""
-msgstr ""
-"Project-Id-Version: ISO-3166_1\\n"
-"MIME-Version: 1.0\\n"
-"Content-Type: text/plain; charset=%(charset)s\\n"
-"Content-Transfer-Encoding: 8bit\\n"
-"Language-Code: %(lang)s\\n"
-"Preferred-Encodings: %(charset)s latin1\\n"
-"Domain: iso_3166_1\\n"
-
-"""
-
-def main():
- xmlFileNames = [name for name in os.listdir('.') if not name.startswith('.') and name.endswith('.xml')]
-
- for name in xmlFileNames:
- lang = os.path.splitext(name)[0].split('_')[-1]
- entries = getEntries(name)
- makePy(lang, entries)
- makePo(lang, entries)
-
-
-
-
-def getEntries(name):
- d = parse(name)
- countries = []
- for entry in d.documentElement.getElementsByTagName('ISO_3166-1_Entry') :
- code = entry.getElementsByTagName('ISO_3166-1_Alpha-2_code')[0].firstChild.nodeValue
- value = entry.getElementsByTagName('ISO_3166-1_Country_name')[0].firstChild.nodeValue
- countries.append((code, value))
-
- return countries
-
-def makePy(lang, entries, encoding='utf-8'):
- out = open('../%s.py' % lang, 'w')
- out.write('# -*- coding: %s -*-\n\n' % encoding)
- out.write('__allow_access_to_unprotected_subobjects__ = 1\n\n')
- out.write('countries = (\n')
-
- for e in entries :
- encodedEntry = tuple(map(lambda s : s.encode(encoding), e))
- out.write(''' ('%s', "%s"),\n''' % encodedEntry)
-
- out.write(' )')
- out.close()
-
-def makePo(lang, entries, encoding='utf-8'):
- path = ('..', 'locales', lang, 'LC_MESSAGES')
- poFilepath = ''
- for p in path :
- poFilepath = poFilepath + p + sep
- if not exists(poFilepath) :
- mkdir(poFilepath)
-
- poFilepath = poFilepath + 'iso_3166_1.po'
- out = open(poFilepath, 'w')
-
- header = POHEADER % {'charset':encoding, 'lang':lang}
- out.write(header)
-
- for e in entries :
- id, msg = tuple(map(lambda s : s.encode(encoding), e))
- out.write('msgid "%s"\n' % id)
- out.write('msgstr "%s"\n\n' % msg)
- out.close()
-
- moFilepath = poFilepath[:-3] + '.mo'
- MSGFMT = "msgfmt -o %s %s" % (moFilepath, poFilepath)
- p = Popen(MSGFMT, shell=True)
- p.wait()
-
-
-
-if __name__ == '__main__' :
- main()