view build_website.py @ 2:0994ae3e4518

Fix compilation on Django 1.10+.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 07 Sep 2017 16:45:01 +0100
parents 0a1d0ab5f518
children
line wrap: on
line source

#!/usr/bin/env python3

'''Supervision script for PyTouhou’s website.'''

import sys
from django.conf import settings

AVAILABLE_LANGUAGES = ['de', 'en', 'es', 'fr', 'fi', 'it', 'pt']


def make_messages():
    '''Extract translatable strings from the template.'''

    from django.core.management.commands import makemessages

    command = makemessages.Command()
    command.stdout = sys.stdout
    #command.handle(verbosity=True, ignore_patterns=[], domain='django', exclude=[], extensions=['xhtml'], locale=AVAILABLE_LANGUAGES, keep_pot=True)
    command.handle(locale=AVAILABLE_LANGUAGES, exclude=[], domain='django', verbosity=True, all=True, extensions=['xhtml'], symlinks=False, ignore_patterns=[], use_default_ignore_patterns=True, no_wrap=True, no_location=True, no_obsolete=True, keep_pot=True)


def compile_messages():
    '''For each locale we support, compile the po file into a mo.'''

    from django.core.management.commands import compilemessages

    command = compilemessages.Command()
    command.verbosity = 1
    command.stdout = sys.stdout
    command.compile_messages(('locale/%s/LC_MESSAGES' % lang, 'django.po')
                             for lang in AVAILABLE_LANGUAGES)


def main():
    '''Call everything in the correct order.'''

    from subprocess import call

    settings.configure()
    make_messages()
    compile_messages()

    for language in AVAILABLE_LANGUAGES:
        call([sys.argv[0], language])


def generate_page(language):
    '''Write a single page, and gzip it for lower network usage.'''

    assert language in AVAILABLE_LANGUAGES

    import django
    from django.template import Template, Context
    import gzip

    settings.configure(LANGUAGE_CODE=language, LOCALE_PATHS=['locale'], TEMPLATES=[{'BACKEND': 'django.template.backends.django.DjangoTemplates'}])

    django.setup()

    with open('index.tpl.xhtml') as template_file:
        template = Template(template_file.read())

    rendered = template.render(Context({'AVAILABLE_LANGUAGES':
                                        AVAILABLE_LANGUAGES}))
    filename = '../index.%s.xhtml' % language
    with open(filename, 'w') as out:
        out.write(rendered)
    with gzip.open(filename + '.gz', 'w') as out:
        out.write(rendered.encode('utf-8'))


if __name__ == '__main__':
    if len(sys.argv) == 1:
        main()
    else:
        generate_page(sys.argv[1])