-
Notifications
You must be signed in to change notification settings - Fork 3
/
whogoesfirst.py
332 lines (263 loc) · 10.6 KB
/
whogoesfirst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""Run Who Goes First web app."""
import argparse
import collections
import gettext
import os.path
import pathlib
import flask
from flask import Flask
import flask_babel
app = Flask(__name__)
babel = flask_babel.Babel(app)
LANGUAGES = {
'en': {
'name': u'English',
'translations': {
'random-card': 'random-card'
}
},
'fr': {
'name': u'français',
'translations': {
'random-card': 'carte-au-hasard'
}
},
'uk': {
'name': u'українська мова',
'translations': {
'random-card': 'random-card'
}
},
}
LOCALEDIR = pathlib.Path(__file__).resolve().parent / "translations"
DEFAULT_LANGUAGE = 'en'
def get_locale():
lang = flask.request.path[1:].split('/', 1)[0]
if lang in LANGUAGES:
return lang
else:
return DEFAULT_LANGUAGE
babel.init_app(app, locale_selector=get_locale)
@app.before_request
def before_request():
flask_babel.refresh() # Ensure we're using the right lang in frozen-flask.
@app.route('/')
def redirect_home_page():
return flask.render_template('redirect_top_card.html')
@app.route('/api/v1/cards.json', endpoint='api_v1_cards')
def handle_api_v1_cards():
return flask.jsonify(get_all_cards())
@app.route('/en/', endpoint='index_en')
@app.route('/fr/', endpoint='index_fr')
@app.route('/uk/', endpoint='index_uk')
def index_card():
return flask.render_template('index.html')
@app.route('/en/contribute/', endpoint='contribute_en')
@app.route('/fr/contribute/', endpoint='contribute_fr')
@app.route('/uk/contribute/', endpoint='contribute_uk')
def handle_contribute():
return flask.render_template('contribute.html')
@app.route('/en/privacy/', endpoint='privacy_en')
@app.route('/fr/privacy/', endpoint='privacy_fr')
@app.route('/uk/privacy/', endpoint='privacy_uk')
def handle_privacy():
return flask.render_template('privacy.html')
@app.route('/en/help/', endpoint='help_en')
@app.route('/fr/help/', endpoint='help_fr')
@app.route('/uk/help/', endpoint='help_uk')
def handle_help():
return flask.render_template('help.html')
@app.route(u'/fr/about/', endpoint='redirect_about_index_fr')
def redirect_about_index_card_fr():
return flask.render_template(
'redirect.html', redirect_target='/fr/a-propos/')
@app.route('/en/about/', endpoint='about_index_en')
@app.route(u'/fr/a-propos/', endpoint='about_index_fr')
@app.route('/uk/about/', endpoint='about_index_uk')
def about_index_card():
return flask.render_template('about_index.html')
@app.route(u'/fr/random-card/')
def redirect_random_card_fr():
target = '/fr/{}/'.format(LANGUAGES['fr']['translations']['random-card'])
return flask.render_template('redirect.html', redirect_target=target)
@app.route(
'/fr/{}/'.format(LANGUAGES['fr']['translations']['random-card']),
endpoint='random_card_page_fr')
@app.route(
'/uk/{}/'.format(LANGUAGES['uk']['translations']['random-card']),
endpoint='random_card_page_uk')
@app.route('/en/random-card/', endpoint='random_card_page_en')
def random_card():
return flask.render_template('random_card.html', cards=get_all_cards())
def get_card_handler(cid):
def handler():
return flask.render_template(
os.path.join('cards', cid.replace('-', '_') + '.html'))
return handler
def get_about_card_handler(cid):
def handler():
return flask.render_template(
os.path.join('cards', 'about_' + cid.replace('-', '_') + '.html'))
return handler
def get_redirect_handler(redirect_target):
def handler():
return flask.render_template(
'redirect.html', redirect_target=redirect_target)
return handler
@app.before_request
def populate_request():
flask.g.babel = babel
flask.g.language = get_locale()
def get_translations(page):
translations = {}
if page.endswith('_card'):
cid = page[:-5] # Remove _card suffix
if cid.startswith('about_'):
cid = cid[6:]
for lang in LANGUAGES:
translations[lang] = {
'url': flask.url_for(page + '_' + lang),
'name': LANGUAGES[lang]['name']
}
return translations
def get_page(endpoint):
return endpoint.rpartition('_')[0]
@app.context_processor
def inject_custom():
endpoint = flask.request.endpoint
if endpoint.startswith('api_') or endpoint.startswith('redirect_'):
return {}
return {
'translations': get_translations(get_page(flask.request.endpoint)),
'global_translations': LANGUAGES,
'google_analytics_id': 'UA-71804102-1'
}
def get_card_redirects(language, directory, url):
translation = LANGUAGES[language]['translation']
redirects = set([
u'/{}/cards/{}'.format(language, directory),
u'/{}/cards/{}'.format(language, translation.gettext(directory)),
u'/{}/{}{}'.format(
language, translation.gettext('cards/'), directory),
])
return [redirect for redirect in redirects if redirect != url]
def get_card(directory):
card = collections.defaultdict(dict)
for language in LANGUAGES:
card[language]['name'] = directory[:-1] # Remove trailing slash.
translation = LANGUAGES[language]['translation']
url = u'/{}/{}{}'.format(
language,
translation.gettext('cards/'),
translation.gettext(directory))
card[language]['url'] = url
card[language]['redirects'] = get_card_redirects(
language, directory, url)
return card
def get_all_cards():
# The translation object for the default language should be the null
# translation. That means it returns what you give it. The purpose in
# using it is to trick gettext into extracting these as messages to
# translate.
translation = LANGUAGES[DEFAULT_LANGUAGE]['translation']
# Include a trailing / in each translation so it is clear it is a URL.
all_cards = {
'award': get_card(translation.gettext('award/')),
'baking': get_card(translation.gettext('baking/')),
'batteries': get_card(translation.gettext('batteries/')),
'birthday': get_card(translation.gettext('birthday/')),
'bowling': get_card(translation.gettext('bowling/')),
'building': get_card(translation.gettext('building/')),
'buttons': get_card(translation.gettext('buttons/')),
'catnap': get_card(translation.gettext('catnap/')),
'coins': get_card(translation.gettext('coins/')),
'dessert': get_card(translation.gettext('dessert/')),
'drawing': get_card(translation.gettext('drawing/')),
'd20': get_card(translation.gettext('d20/')),
'flat-tire': get_card(translation.gettext('flat-tire/')),
'foreign-language':
get_card(translation.gettext('foreign-language/')),
'hammock': get_card(translation.gettext('hammock/')),
'junk-mail': get_card(translation.gettext('junk-mail/')),
'light-bulb': get_card(translation.gettext('light-bulb/')),
'litter-box': get_card(translation.gettext('litter-box/')),
'oldest-movie': get_card(translation.gettext('oldest-movie/')),
'onion': get_card(translation.gettext('onion/')),
'pizza': get_card(translation.gettext('pizza/')),
'post-office': get_card(translation.gettext('post-office/')),
'postcard': get_card(translation.gettext('postcard/')),
'sharpen': get_card(translation.gettext('sharpen/')),
'stung': get_card(translation.gettext('stung/')),
'survey': get_card(translation.gettext('survey/')),
'train': get_card(translation.gettext('train/')),
'trash': get_card(translation.gettext('trash/')),
'tv': get_card(translation.gettext('television/')),
'walk-dog': get_card(translation.gettext('walk-a-dog/')),
'went-to-movies':
get_card(translation.gettext('went-to-the-movies/')),
}
return all_cards
def populate_card_url_rules():
all_cards = get_all_cards()
for cid, card in all_cards.items():
for language, translated_card in card.items():
translation = LANGUAGES[language]['translation']
card_url = translated_card['url']
about_url = u'{}{}'.format(
card_url, translation.gettext('about/'))
app.add_url_rule(
card_url, cid + '_card_' + language, get_card_handler(cid))
app.add_url_rule(
about_url,
'about_' + cid + '_card_' + language,
get_about_card_handler(cid))
for i, redirect in enumerate(translated_card['redirects']):
app.add_url_rule(
redirect,
'redirect_{}_card_{}_{}'.format(cid, language, i),
get_redirect_handler(card_url))
app.add_url_rule(
redirect,
'redirect_{}_card_{}_about_0_{}'.format(cid, language, i),
get_redirect_handler(about_url))
if translation.gettext('about/') != 'about/':
app.add_url_rule(
redirect,
'redirect_{}_card_{}_about_1_{}'.format(
cid, language, i),
get_redirect_handler(about_url))
def load_translations():
for language in LANGUAGES:
if language == DEFAULT_LANGUAGE:
translation = gettext.NullTranslations()
else:
translation = gettext.translation(
'messages',
localedir=LOCALEDIR,
languages=[language])
LANGUAGES[language]['translation'] = translation
def initialize():
"""Initialize the module.
These actions are needed, regardless of whether this is being loaded as a
module (for freezing) or as the main script.
"""
load_translations()
populate_card_url_rules()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--host', help='Hostname of the Flask app.', default='127.0.0.1')
parser.add_argument(
'--port', help='Port for the Flask app.', type=int, default=8080)
parser.add_argument(
'--debug',
help='Use debug mode for the Flask app.',
type=bool,
default=False)
args = parser.parse_args()
initialize()
app.run(host=args.host, port=args.port, debug=args.debug)