view build_website.py @ 55:6a7a99f5a0ca

Add pubsub#access_model retrieval for the vCard4 node.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 23 May 2020 21:59:17 +0200
parents 78f3f79c911b
children 010b905a74d4
line wrap: on
line source

#!/usr/bin/env python3
'''Locale build script for XAM'''

import sys
from os import makedirs, path
from django.conf import settings
from shutil import copytree, copy

AVAILABLE_LANGUAGES = ['en', 'fr']


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(
        locale=AVAILABLE_LANGUAGES, exclude=[], domain='django', verbosity=True,
        all=True, extensions=['xhtml', 'js'], symlinks=False, ignore_patterns=[],
        use_default_ignore_patterns=True, no_wrap=True, no_location=True,
        no_obsolete=True, keep_pot=True, add_location=False
    )


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_pages(language):
    '''Write a single page, and gzip it for lower network usage.'''

    assert language in AVAILABLE_LANGUAGES
    target_dir = path.join('.', 'build', language)
    makedirs(target_dir, exist_ok=True)

    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()

    static = ['share', 'prosody.css', 'prosody.svg', 'spinner.svg', 'ok.svg', 'error.svg']
    files = [
        'index.xhtml', 'client.js', 'roster.js', 'avatar.js', 'client.js', 'index.xhtml',
        'mam.js', 'nickname.js', 'pep.js', 'roster.js', 'strophe.js', 'strophe.mam.js',
        'strophe.rsm.js', 'util.js', 'vcard.js'
    ]
    for file_to_localize in files:
        with open(file_to_localize) as template_file:
            template = Template(template_file.read())
        rendered = template.render(Context({'AVAILABLE_LANGUAGES':
                                            AVAILABLE_LANGUAGES}))
        filename = path.join(target_dir, file_to_localize)
        with open(filename, 'w') as out:
            out.write(rendered)
    for static_resource in static:
        if path.isdir(static_resource):
            copytree(static_resource, path.join(target_dir, static_resource), dirs_exist_ok=True)
        else:
            copy(static_resource, target_dir)

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