-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript_create_index.py
More file actions
195 lines (171 loc) · 6.5 KB
/
script_create_index.py
File metadata and controls
195 lines (171 loc) · 6.5 KB
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
### CREATE MODULE
# 1. Create Git repository
# 2. Create Preliminary index.yml
# 3. Start build
import yaml
from yaml import SafeLoader, SafeDumper
import os
import json
import sys
import glob
import frontmatter
from frontmatter.default_handlers import BaseHandler
from io import BytesIO
import re
class YAMLHandlerNoReorder(BaseHandler):
"""
Load and export YAML metadata. By default, this handler uses YAML's
"safe" mode, though it's possible to override that.
"""
FM_BOUNDARY = re.compile(r"^-{3,}\s*$", re.MULTILINE)
START_DELIMITER = END_DELIMITER = "---"
def load(self, fm, **kwargs):
"""
Parse YAML front matter. This uses yaml.SafeLoader by default.
"""
kwargs.setdefault("Loader", SafeLoader)
return yaml.load(fm, **kwargs)
def export(self, metadata, **kwargs):
"""
Export metadata as YAML. This uses yaml.SafeDumper by default.
"""
kwargs.setdefault("Dumper", SafeDumper)
kwargs.setdefault("default_flow_style", False)
kwargs.setdefault("allow_unicode", True)
metadata = yaml.dump(metadata, default_flow_style=False, sort_keys=False).strip()
return metadata
def get_path_names(pattern, sort = True):
res = [d.replace('./', '') for d in glob.glob(pattern)]
res.sort()
return res
with open('index.yml', 'r') as f:
module = yaml.load(f)
module['moduleId'] = sys.argv[1]
module['contentId'] = sys.argv[1]
if 'image' in module:
module['image'] = '/'.join(['/assets/courses', module['moduleId'], module['image']])
topics = []
contents = []
for d in get_path_names('./[0-9][0-9]*'):
file_idx_topic = os.path.join(d, 'index.yml')
with open(file_idx_topic, 'r') as f:
topic = yaml.load(f, Loader=yaml.BaseLoader)
topic['contentId'] = module['moduleId'] + '#' + d
topic['type'] = 'contentId'
if 'image' in topic:
topic['image'] = '/'.join(['/assets/courses', module['moduleId'], d , topic['image']])
topics.append(topic)
content = {}
content['contentId'] = module['moduleId'] + '#' + d
content['moduleId'] = module['moduleId']
content['title'] = topic['title']
content['contentType'] = 'index'
content['contents'] = []
dir_chapter = './%s/*.Rmd' % (d)
for rmd in get_path_names(dir_chapter):
print('rmd: ' + rmd)
chapter = {}
chapter['type'] = 'contentId'
chapter['content'] = '#'.join([module['moduleId'],
d,
os.path.basename(rmd).replace('.Rmd', '')])
chapter['contentId'] = '#'.join([module['moduleId'],
d,
os.path.basename(rmd).replace('.Rmd', '')])
content['contents'].append(chapter)
# Adjust content id in file
print('contentId: ' + content['contentId'])
section = None
with open(rmd, 'r') as f:
section = frontmatter.loads(f.read())
print('Setting tutorial id to "' + chapter['content'] + '"')
section.metadata['tutorial']['id'] = chapter['content']
with open(rmd, 'w+') as f:
f.write(frontmatter.dumps(section, handler=YAMLHandlerNoReorder()))
f.write("\n")
contents.append(content)
with open('contents.yml', 'w+') as f:
yaml.dump(contents, f, default_flow_style=False, sort_keys=False)
module['contents'] = topics
# Include stats
n_exercises = 0
n_quizzes = 0
for cfile in get_path_names('./[0-9][0-9]*/*.json'):
with open(cfile, 'r') as f:
chapter = json.load(f)
for c in chapter:
if 'contentType' in c and 'exerciseType' in c:
if c['contentType'] == 'exercise':
if c['exerciseType'] == 'code':
n_exercises += 1
if c['exerciseType'].startswith('quiz'):
n_quizzes += 1
module['moduleStats'] = {
'topics': len(topics),
'exercises': n_exercises,
'quizzes': n_quizzes
}
with open('index.yml', 'w+') as f:
yaml.dump(module, f, default_flow_style=False, sort_keys=False)
course = {
'moduleId': module['moduleId'],
'contentId': module['contentId'] + '#' + 'course',
'contentType': 'course',
'image': module['image'],
'title': module['title']
}
achievements = []
for bfile in get_path_names('./[0-9][0-9]*/index.yml'):
with open(bfile, 'r') as f:
badge = yaml.load(f, Loader=yaml.BaseLoader)
badge['moduleId'] = module['moduleId']
badge['contentId'] = '#'.join([module['contentId'], os.path.dirname(bfile), 'badge'])
badge['contentType'] = 'badge'
badge['image'] = '/'.join(['/assets/courses', module['contentId'], os.path.dirname(bfile), badge['image']])
badge['title'] = badge['badgeTitle']
achievements.append(badge)
for cfile in get_path_names(os.path.dirname(bfile) + '/*.json'):
with open(cfile, 'r') as f:
chapter = json.load(f)
if (chapter[0]['contentType'] == 'recipe'):
achievements.append({
'moduleId': module['moduleId'],
'contentId': '_'.join([badge['contentId'], 'dependency', chapter[0]['contentId']]),
'dependencyFrom': badge['contentId'],
'dependencyTo': chapter[0]['contentId'],
'contentType': 'dependency'
})
for ex in chapter[0]['dependencies']:
achievements.append({
'moduleId': module['moduleId'],
'contentId': '_'.join([chapter[0]['contentId'], 'dependency', ex]),
'dependencyFrom': chapter[0]['contentId'],
'dependencyTo': ex,
'contentType': 'dependency'
})
achievements.append({
'moduleId': module['moduleId'],
'contentId': '_'.join([course['contentId'], 'dependency', badge['contentId']]),
'dependencyFrom': course['contentId'],
'dependencyTo': badge['contentId'],
'contentType': 'dependency'
})
achievements.append(course)
certificate = {
'moduleId': module['moduleId'],
'contentId': module['contentId'] + '#' + 'certificate',
'contentType': 'certificate',
'image': '/img/certificate.png',
'title': 'Certificate ' + module['title']
}
achievements.append(certificate)
if ('assessment' in module) and (module['assessment'] == False):
achievements.append({
'moduleId': module['moduleId'],
'contentId': '_'.join([certificate['contentId'], 'dependency', course['contentId']]),
'dependencyFrom': certificate['contentId'],
'dependencyTo': course['contentId'],
'contentType': 'dependency'
})
with open('badge.yml', 'w+') as f:
yaml.dump(achievements, f, default_flow_style=False, sort_keys=False)