dosage/scripts/mklanguages.py
2018-05-23 00:54:40 +02:00

50 lines
1.5 KiB
Python
Executable file

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2018 Tobias Gruetzmacher
'''update languages.py from pycountry'''
from __future__ import absolute_import, division, print_function
import os
import sys
import codecs
basepath = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, basepath)
from dosagelib.scraper import get_scrapers # noqa
def main():
"""Update language information in dosagelib/languages.py."""
fn = os.path.join(basepath, 'dosagelib', 'languages.py')
encoding = 'utf-8'
with codecs.open(fn, 'w', encoding) as f:
f.write('# -*- coding: %s -*-%s' % (encoding, os.linesep))
f.write('# ISO 693-1 language codes from pycountry%s' % os.linesep)
f.write('# This file is automatically generated, DO NOT EDIT!%s' % os.linesep)
f.write('from __future__ import absolute_import, division, print_function%s' % os.linesep)
lang = get_used_languages()
write_languages(f, lang)
def get_used_languages():
languages = {}
for scraperobj in get_scrapers():
lang = scraperobj.lang
if lang not in languages:
languages[lang] = scraperobj.language()
return languages
def write_languages(f, l):
"""Write language information."""
f.write("Languages = {%s" % os.linesep)
for lang in sorted(l):
f.write(" %r: %r,%s" % (lang, l[lang], os.linesep))
f.write("}%s" % os.linesep)
if __name__ == '__main__':
main()