comparison build_website.py @ 0:0a1d0ab5f518

Initial commit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 07 Sep 2017 15:04:29 +0100
parents
children 0994ae3e4518
comparison
equal deleted inserted replaced
-1:000000000000 0:0a1d0ab5f518
1 #!/usr/bin/env python3
2
3 '''Supervision script for PyTouhou’s website.'''
4
5 import sys
6 from django.conf import settings
7
8 AVAILABLE_LANGUAGES = ['de', 'en', 'es', 'fr', 'fi', 'it', 'pt']
9
10
11 def make_messages():
12 '''Extract translatable strings from the template.'''
13
14 from django.core.management.commands import makemessages
15
16 command = makemessages.Command()
17 command.stdout = sys.stdout
18 #command.handle(verbosity=True, ignore_patterns=[], domain='django', exclude=[], extensions=['xhtml'], locale=AVAILABLE_LANGUAGES, keep_pot=True)
19 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)
20
21
22 def compile_messages():
23 '''For each locale we support, compile the po file into a mo.'''
24
25 from django.core.management.commands import compilemessages
26
27 command = compilemessages.Command()
28 command.verbosity = 1
29 command.stdout = sys.stdout
30 command.compile_messages(('locale/%s/LC_MESSAGES' % lang, 'django.po')
31 for lang in AVAILABLE_LANGUAGES)
32
33
34 def main():
35 '''Call everything in the correct order.'''
36
37 from subprocess import call
38
39 settings.configure()
40 make_messages()
41 compile_messages()
42
43 for language in AVAILABLE_LANGUAGES:
44 call([sys.argv[0], language])
45
46
47 def generate_page(language):
48 '''Write a single page, and gzip it for lower network usage.'''
49
50 assert language in AVAILABLE_LANGUAGES
51
52 import django
53 from django.template import Template, Context
54 import gzip
55
56 settings.configure(LANGUAGE_CODE=language, LOCALE_PATHS=['locale'])
57
58 django.setup()
59
60 with open('index.tpl.xhtml') as template_file:
61 template = Template(template_file.read())
62
63 rendered = template.render(Context({'AVAILABLE_LANGUAGES':
64 AVAILABLE_LANGUAGES}))
65 filename = '../index.%s.xhtml' % language
66 with open(filename, 'w') as out:
67 out.write(rendered)
68 with gzip.open(filename + '.gz', 'w') as out:
69 out.write(rendered.encode('utf-8'))
70
71
72 if __name__ == '__main__':
73 if len(sys.argv) == 1:
74 main()
75 else:
76 generate_page(sys.argv[1])