forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hedy_content.py
166 lines (140 loc) · 5.41 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
import copy
import attr
from website.yaml_file import YamlFile
# Define and load all available language data
ALL_LANGUAGES = {
'id': 'Bahasa Indonesia',
'de': 'Deutsch',
'en': 'English',
'es': 'Español',
'fr': 'Français',
'pl': 'Polski',
'pt_PT': 'Português (pt)',
'pt_BR': 'Português (br)',
'fy': 'Frysk',
'it': 'Italiano',
'hu': 'Magyar',
'el': 'Ελληνικά',
'zh_Hans': "简体中文",
'nl': 'Nederlands',
'nb_NO': 'Norsk',
'sw': 'Swahili',
'tr': 'Türk',
'cs': 'Čeština',
'bg': 'Български',
'ar': 'عربى',
'hi': 'हिंदी',
'bn': 'বাংলা',
}
# Define fall back languages for adventures
FALL_BACK_ADVENTURE = {
'fy': 'nl',
'pt_BR': 'pt_PT'
}
ALL_KEYWORD_LANGUAGES = {
'en': 'EN',
'es': 'ES',
'fr': 'FR',
'nl': 'NL',
'nb_NO': 'NB',
'tr': 'TR',
'ar': 'AR',
'hi': 'HI'
}
class LevelDefaults:
def __init__(self, language):
self.language = language
self.keyword_lang = "en"
self.keywords = YamlFile.for_file(f'coursedata/keywords/{self.keyword_lang}.yaml').to_dict()
self.levels = YamlFile.for_file(f'coursedata/level-defaults/{self.language}.yaml')
def set_keyword_language(self, language):
if language != self.keyword_lang:
self.keyword_lang = language
self.keywords = YamlFile.for_file(f'coursedata/keywords/{self.keyword_lang}.yaml')
def max_level(self):
all_levels = sorted(self.levels.keys()) # We should sort this to make sure the max_level returned is correct
max_consecutive_level = 1
previous_level = 0
for level in all_levels:
if level == previous_level + 1:
previous_level = level
max_consecutive_level = level
else:
return previous_level
return max_consecutive_level
def get_defaults_for_level(self, level):
#grabs level defaults from yaml and converts to DefaultValues type
default_values = copy.deepcopy(self.levels[level])
# Sometimes we have multiple text and example_code -> iterate these and add as well!
extra_examples = []
for i in range(2, 10):
extra_example = {}
if default_values.get('intro_text_' + str(i)):
extra_example['intro_text'] = default_values.get('intro_text_' + str(i)).format(**self.keywords)
default_values.pop('intro_text_' + str(i))
if default_values.get('example_code_' + str(i)):
extra_example['example_code'] = default_values.get('example_code_' + str(i)).format(**self.keywords)
default_values.pop('example_code_' + str(i))
extra_examples.append(extra_example)
else:
break
default_values['extra_examples'] = extra_examples
# Todo TB -> We have to improve this coding (a lot!)
# We use the following section to replace the placeholders with the actual keywords, but this is complex
# One solution might be: Separate the commands from the level_defaults -> load them separately
# This way can use a more simplistic structure less keen to mistakes and easier to understand
# We have to verify if it's a string as the extra examples are stored within a list
for k,v in default_values.items():
if isinstance(v, str):
default_values[k] = v.format(**self.keywords)
# The commands value is a list of dicts -> we have to parse this separately
if k == "commands":
parsed_commands = []
for command in v:
temp = {}
for command_key, command_value in command.items():
temp[command_key] = command_value.format(**self.keywords)
parsed_commands.append(temp)
default_values[k] = parsed_commands
default_type = {
"level": str(level),
}
default_type.update(**default_values)
return DefaultValues(**default_type)
def get_defaults(self, level):
"""Return the level defaults for a given level number."""
return copy.deepcopy(self.levels.get(int(level), {}))
class NoSuchDefaults:
def get_defaults(self, level):
return {}
class Adventures:
def __init__(self, language):
self.language = language
self.keyword_lang = "en"
self.keywords = YamlFile.for_file(f'coursedata/keywords/{self.keyword_lang}.yaml').to_dict()
self.adventures_file = YamlFile.for_file(f'coursedata/adventures/{self.language}.yaml')
def set_keyword_language(self, language):
if language != self.keyword_lang:
self.keyword_lang = language
self.keywords = YamlFile.for_file(f'coursedata/keywords/{self.keyword_lang}.yaml')
# When customizing classes we only want to retrieve the name, (id) and level of each adventure
def get_adventure_keyname_name_levels(self):
adventures = self.adventures_file['adventures']
adventures_dict = {}
for adventure in adventures.items():
adventures_dict[adventure[0]] = {adventure[1]['name']: list(adventure[1]['levels'].keys())}
return adventures_dict
def has_adventures(self):
return self.adventures_file.exists() and self.adventures_file.get('adventures')
class NoSuchAdventure:
def get_defaults(self, level):
return {}
@attr.s(slots=True)
class DefaultValues:
"""Default texts for a level"""
level = attr.ib()
intro_text = attr.ib(default=None)
example_code = attr.ib(default=None)
extra_examples = attr.ib(default=None)
start_code = attr.ib(default=None)
commands = attr.ib(default=None)