-
Notifications
You must be signed in to change notification settings - Fork 289
/
hedy_content.py
728 lines (648 loc) · 19 KB
/
hedy_content.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
import logging
import os
from os import path
import static_babel_content
from utils import customize_babel_locale
from website.yaml_file import YamlFile
from safe_format import safe_format
logger = logging.getLogger(__name__)
COUNTRIES = static_babel_content.COUNTRIES
# Define dictionary for available languages. Fill dynamically later.
ALL_LANGUAGES = {}
ALL_KEYWORD_LANGUAGES = {}
# Babel has a different naming convention than Weblate and doesn't support some languages -> fix this manually
# Map our langauge code to the language code recognized by Babel, or 'en' if Babel doesn't support this locale
# at all.
CUSTOM_BABEL_LANGUAGES = {'pa_PK': 'pa_Arab_PK',
'kmr': 'ku_TR',
'tl': 'en',
'iba': 'en',
'peo': 'fa',
}
# For the non-existing language manually overwrite the display language to make sure it is displayed correctly
CUSTOM_LANGUAGE_TRANSLATIONS = {'kmr': 'Kurdî (Tirkiye)',
'tl': 'ᜆᜄᜎᜓᜄ᜔',
'peo': 'Old Persian',
'iba': 'Iban',
'kab': 'Taqbaylit',
}
customize_babel_locale(CUSTOM_BABEL_LANGUAGES)
# This changes the color of the adventure tab to pink
KEYWORDS_ADVENTURES = {'print_command', 'ask_command', 'is_command', 'sleep_command', 'random_command',
'add_remove_command', 'quotation_marks', 'if_command', 'in_command', 'maths', 'repeat_command',
'repeat_command_2', 'for_command', 'and_or_command', 'while_command', 'elif_command',
'clear_command', 'pressit', 'debugging', 'functions'}
ADVENTURE_ORDER_PER_LEVEL = {
1: [
'default',
'print_command',
'ask_command',
'parrot',
'rock',
'haunted',
'story',
'music',
'turtle',
'turtle_draw_it',
'restaurant',
'fortune',
'debugging',
'parsons',
'quiz',
],
2: [
'default',
'is_command',
'rock',
'ask_command',
'rock_2',
'haunted',
'sleep_command',
'parrot',
'story',
'music',
'restaurant',
'turtle',
'turtle_draw_it',
'debugging',
'parsons',
'quiz',
],
3: [
'default',
'random_command',
'dice',
'rock',
'music',
'fortune',
'restaurant',
'add_remove_command',
'parrot',
'dishes',
'story',
'haunted',
'turtle',
'turtle_draw_it',
'debugging',
'parsons',
'quiz',
],
4: [
'default',
'quotation_marks',
'rock',
'dice',
'dishes',
'parrot',
'turtle',
'turtle_draw_it',
'clear_command',
'music',
'story',
'haunted',
'fortune',
'restaurant',
'debugging',
'parsons',
'quiz',
],
5: [
'default',
'if_command',
'music',
'language',
'dice',
'dishes',
'story',
'rock',
'parrot',
'haunted',
'in_command',
'restaurant',
'fortune',
'pressit',
'turtle',
'turtle_draw_it',
'debugging',
'parsons',
'quiz',
],
6: [
'default',
'maths',
'music',
'is_command',
'songs',
'dice',
'dishes',
'turtle',
'turtle_draw_it',
'calculator',
'fortune',
'restaurant',
'debugging',
'parsons',
'quiz',
],
7: [
'default',
'repeat_command',
'story',
'songs',
'music',
'dishes',
'dice',
'repeat_command_2',
'fortune',
'restaurant',
'pressit',
'turtle_draw_it',
'debugging',
'parsons',
'quiz',
],
8: [
'default',
'repeat_command',
'fortune',
'repeat_command_2',
'songs',
'music',
'if_command',
'story',
'haunted',
'restaurant',
'turtle',
'turtle_draw_it',
'debugging',
'parsons',
'quiz',
],
9: [
'default',
'repeat_command',
'if_command',
'rock',
'story',
'calculator',
'music',
'restaurant',
'haunted',
'pressit',
'turtle',
'turtle_draw_it',
'debugging',
'parsons',
'quiz',
],
10: [
'default',
'for_command',
'dishes',
'dice',
'fortune',
'turtle',
'turtle_draw_it',
'harry_potter',
'songs',
'story',
'rock',
'calculator',
'restaurant',
'debugging',
'parsons',
'quiz',
],
11: [
'default',
'for_command',
'years',
'calculator',
'songs',
'restaurant',
'haunted',
'turtle_draw_it',
'debugging',
'parsons',
'quiz',
],
12: [
'default',
'maths',
'quotation_marks',
'functions',
'story',
'fortune',
'music',
'songs',
'songs_2',
'restaurant',
'calculator',
'turtle',
'piggybank',
'secret',
'turtle_draw_it',
'debugging',
'parsons',
'quiz',
],
13: [
'default',
'and_or_command',
'secret',
'functions',
'music',
'story',
'rock',
'turtle_draw_it',
'restaurant',
'calculator',
'debugging',
'quiz',
],
14: [
'default',
'is_command',
'guess_my_number',
'music',
'haunted',
'functions',
'turtle_draw_it',
'hotel',
'calculator',
'calculator_2',
'piggybank',
'quizmaster',
'debugging',
'quiz',
],
15: [
'default',
'while_command',
'music',
'turtle_draw_it',
'restaurant',
'story',
'dice',
'rock',
'calculator',
'debugging',
'quiz',
],
16: [
'default',
'random_command',
'haunted',
'songs',
'songs_2',
'music',
'language',
'tic',
'tic_2',
'tic_3',
'simon',
'simon_2',
'simon_3',
'debugging',
'quiz',
],
17: [
'default',
'for_command',
'elif_command',
'music',
'tic',
'hangman',
'hangman_2',
'hangman_3',
'blackjack',
'blackjack_2',
'blackjack_3',
'blackjack_4',
'debugging',
'quiz',
],
18: [
'default',
'print_command',
'ask_command',
'functions',
'for_command',
'story',
'songs',
'music',
'debugging'
]
}
HOUR_OF_CODE_ADVENTURES = {
1: [
'print_command',
'parrot',
'turtle',
'debugging'
],
2: [
'default',
'parrot',
'turtle',
'debugging'
],
3: [
'parrot',
'dishes',
'turtle',
'debugging'
],
4: [
'dishes',
'parrot',
'story',
'debugging'
],
5: [
'language',
'parrot',
'turtle',
'debugging'
],
6: [
'songs',
'dishes',
'turtle',
'debugging'
],
7: [
'songs',
'dishes',
'restaurant',
'debugging'
],
8: [
'songs',
'restaurant',
'turtle',
'debugging'
],
9: [
'rock',
'restaurant',
'turtle',
'debugging'
],
10: [
'dishes',
'dice',
'turtle',
'songs',
'debugging'
],
11: [
'years',
'songs',
'restaurant',
'debugging'
],
12: [
'maths',
'functions',
'story',
'turtle',
'debugging'
],
13: [
'story',
'rock',
'restaurant',
'calculator',
'debugging'
],
14: [
'guess_my_number',
'haunted',
'hotel',
'calculator',
'quizmaster',
'debugging'
],
15: [
'restaurant',
'story',
'dice',
'rock',
'debugging'
],
16: [
'haunted',
'songs',
'language',
'debugging'
],
17: [
'blackjack',
'debugging'
],
18: [
'story',
'songs',
'debugging'
]
}
# We must find our data relative to this .py file. This will give the
# correct answer both for when Hedy is run as a webserver on Heroku, as well
# as when it has been bundled using pyinstaller.
data_root = path.dirname(__file__)
content_dir = path.join(data_root, 'content')
translations_dir = path.join(data_root, 'translations')
RESEARCH = {}
for paper in sorted(os.listdir(f'{content_dir}/research'),
key=lambda x: int(x.split("_")[-1][:-4]),
reverse=True):
# An_approach_to_describing_the_semantics_of_Hedy_2022.pdf -> 2022, An
# approach to describing the semantics of Hedy
name = paper.replace("_", " ").split(".")[0]
name = name[-4:] + ". " + name[:-5]
RESEARCH[name] = paper
# load all available languages in dict
# list_translations of babel does about the same, but without territories.
languages = {}
if not os.path.isdir(translations_dir):
# should not be possible, but if it's moved someday, EN would still be working.
ALL_LANGUAGES['en'] = 'English'
ALL_KEYWORD_LANGUAGES['en'] = 'EN'
for folder in os.listdir(translations_dir):
locale_dir = os.path.join(translations_dir, folder, 'LC_MESSAGES')
if not os.path.isdir(locale_dir):
continue
if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
languages[folder] = CUSTOM_LANGUAGE_TRANSLATIONS.get(folder,
static_babel_content.LANGUAGE_NAMES.get(folder, folder))
for lang in sorted(languages):
ALL_LANGUAGES[lang] = languages[lang]
if os.path.exists(path.join(data_root, './grammars/keywords-' + lang + '.lark')):
ALL_KEYWORD_LANGUAGES[lang] = lang[0:2].upper() # first two characters
# Load and cache all keyword yamls
KEYWORDS = {}
for lang in ALL_KEYWORD_LANGUAGES.keys():
KEYWORDS[lang] = YamlFile.for_file(f'{content_dir}/keywords/{lang}.yaml').to_dict()
for k, v in KEYWORDS[lang].items():
if isinstance(v, str) and "|" in v:
# when we have several options, pick the first one as default
# Some keys are ints, turn them into strings
KEYWORDS[lang][k] = v.split('|')[0]
class StructuredDataFile:
"""Base class for all data files in the content directory."""
def __init__(self, filename):
self.filename = filename
self._file = None
@property
def file(self):
"""Lazily load the requested file."""
if not self._file:
self._file = YamlFile.for_file(self.filename)
return self._file
class Commands(StructuredDataFile):
def __init__(self, language):
self.language = language
super().__init__(f'{content_dir}/cheatsheets/{self.language}.yaml')
def get_commands_for_level(self, level, keyword_lang):
return deep_translate_keywords(self.file.get(int(level), {}), keyword_lang)
def deep_translate_keywords(yaml, keyword_language):
try:
"""Recurse through a data structure and replace keyword placeholders in any strings we encounter."""
if isinstance(yaml, str):
# this is used to localize adventures linked in slides (PR 3860)
yaml = yaml.replace('/raw', f'/raw?keyword_language={keyword_language}')
return safe_format(yaml, **KEYWORDS.get(keyword_language))
if isinstance(yaml, list):
return [deep_translate_keywords(e, keyword_language) for e in yaml]
if isinstance(yaml, dict):
return {k: deep_translate_keywords(v, keyword_language) for k, v in yaml.items()}
return yaml
except ValueError as E:
raise ValueError(f'Issue in language {keyword_language}. Offending yaml: {yaml}. Error: {E}')
except TypeError as E:
raise TypeError(f'Issue in language {keyword_language}. Offending yaml: {yaml}. Error: {E}')
def get_localized_name(name, keyword_lang):
return safe_format(name, **KEYWORDS.get(keyword_lang))
# Todo TB -> We don't need these anymore as we guarantee with Weblate that
# each language file is there
class NoSuchCommand:
def get_commands_for_level(self, level, keyword_lang):
return {}
class Adventures(StructuredDataFile):
def __init__(self, language):
self.language = language
super().__init__(f'{content_dir}/adventures/{self.language}.yaml')
def get_adventure_keyname_name_levels(self):
return {aid: {adv['name']: list(adv['levels'].keys())} for aid, adv in self.file.get('adventures', {}).items()}
def get_sorted_level_programs(self, programs, adventure_names):
programs_by_level = []
for item in programs:
programs_by_level.append(
{'level': item['level'],
'adventure_name': item.get('adventure_name', item['name']),
}
)
sort = {}
for program in programs_by_level:
if program['level'] in sort:
sort[program['level']].append(adventure_names.get(program['adventure_name'], program['adventure_name']))
else:
sort[program['level']] = [adventure_names.get(program['adventure_name'], program['adventure_name'])]
for level, adventures in sort.copy().items():
sort[level] = sorted(adventures, key=lambda s: s.lower() if s else "")
return dict(sorted(sort.items(), key=lambda item: item[0]))
def get_sorted_adventure_programs(self, programs, adventure_names):
programs_by_adventure = []
for item in programs:
if item.get('adventure_name'):
programs_by_adventure.append(
{'adventure_name': adventure_names.get(item.get('adventure_name')) or item.get('adventure_name'),
'level': item['level'],
}
)
sort = {}
for program in programs_by_adventure:
if program['adventure_name'] in sort:
sort[program['adventure_name']].append(program['level'])
else:
sort[program['adventure_name']] = [program['level']]
for adventure, levels in sort.copy().items():
sort[adventure] = sorted(levels, key=lambda item: item)
return {key: sort[key]
for key in sorted(sort.keys(), key=lambda s: s.lower() if s else "")}
def get_adventure_names(self, keyword_lang):
return {aid: adv['name'] for aid, adv in deep_translate_keywords(
self.file.get('adventures'), keyword_lang).items()}
def get_adventures(self, keyword_lang="en"):
return deep_translate_keywords(self.file.get('adventures'), keyword_lang)
def get_adventures_subset(self, subset=["print_command", "parrot"], keyword_lang="en"):
adventures = {aid: adv for aid, adv in self.file.get('adventures', {}).items() if aid in subset}
return deep_translate_keywords(adventures, keyword_lang)
def has_adventures(self):
return True if self.file.get('adventures') else False
class NoSuchAdventure:
def get_adventure(self):
return {}
class ParsonsProblem(StructuredDataFile):
def __init__(self, language):
self.language = language
super().__init__(f'{content_dir}/parsons/{self.language}.yaml')
def get_highest_exercise_level(self, level):
return max(int(lnum) for lnum in self.file.get('levels', {}).get(level, {}).keys())
def get_parsons_data_for_level(self, level, keyword_lang="en"):
return deep_translate_keywords(self.file.get('levels', {}).get(level, None), keyword_lang)
def get_parsons_data_for_level_exercise(self, level, excercise, keyword_lang="en"):
return deep_translate_keywords(self.file.get('levels', {}).get(level, {}).get(excercise), keyword_lang)
class Quizzes(StructuredDataFile):
def __init__(self, language):
self.language = language
super().__init__(f'{content_dir}/quizzes/{self.language}.yaml')
def get_highest_question_level(self, level):
return max(int(k) for k in self.file.get('levels', {}).get(level, {}))
def get_quiz_data_for_level(self, level, keyword_lang="en"):
return deep_translate_keywords(self.file.get('levels', {}).get(level), keyword_lang)
def get_quiz_data_for_level_question(self, level, question, keyword_lang="en"):
return deep_translate_keywords(self.file.get('levels', {}).get(level, {}).get(question), keyword_lang)
class NoSuchQuiz:
def get_quiz_data_for_level(self, level, keyword_lang):
return {}
class Tutorials(StructuredDataFile):
# Want to parse the keywords only once, they can be cached -> perform this
# action on server start
def __init__(self, language):
self.language = language
super().__init__(f'{content_dir}/tutorials/{self.language}.yaml')
def get_tutorial_for_level(self, level, keyword_lang="en"):
if level not in ["intro", "teacher"]:
level = int(level)
return deep_translate_keywords(self.file.get(level, None), keyword_lang)
def get_tutorial_for_level_step(self, level, step, keyword_lang="en"):
if level not in ["intro", "teacher"]:
level = int(level)
return deep_translate_keywords(self.file.get(level, {}).get('steps', {}).get(step), keyword_lang)
class NoSuchTutorial:
def get_tutorial_for_level(self, level, keyword_lang):
return {}
class Slides(StructuredDataFile):
def __init__(self, language):
self.language = language
super().__init__(f'{content_dir}/slides/{self.language}.yaml')
def get_slides_for_level(self, level, keyword_lang="en"):
return deep_translate_keywords(self.file.get('levels', {}).get(level), keyword_lang)
class NoSuchSlides:
def get_slides_for_level(self, level, keyword_lang):
return {}
class NoSuchTags:
def get_tags(self):
return {}
class Tags(StructuredDataFile):
def __init__(self, language):
self.language = language
super().__init__(f'{content_dir}/tags/{self.language}.yaml')
def get_tags_names(self):
return {tid: tags['items'] for tid, tags in self.file.get('tags', {}).items()}
def get_tags(self, keyword_lang="en"):
return deep_translate_keywords(self.file.get('tags'), keyword_lang)
def has_tags(self):
return True if self.file.get('tags') else False