forked from openshift-evangelists/workshop-base-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
courses.py
99 lines (67 loc) · 2.71 KB
/
courses.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
import os
import json
# The courses need to be placed under /opt/app-root/courses directory.
# Expect to find a pathway.json file which lists the courses which are
# available. Each course needs to have a matching directory, and in that
# there needs to be an index.json file which describes the steps which
# make up the course. The format of these files is the same as is used
# in Katacoda to make porting easier.
COURSES_DIRECTORY = '/opt/app-root/courses'
def load_course(name):
course_file = os.path.join(COURSES_DIRECTORY, name, 'index.json')
if not os.path.exists(course_file):
return
with open(course_file) as fp:
data = json.load(fp)
if not 'details' in data:
return
if not 'steps' in data['details']:
return
course_details = {}
course_details['name'] = name
course_details['title'] = data.get('title')
course_details['modules'] = []
course_details['index'] = {}
def create_module(entry, title=None):
current = {}
current['title'] = title or entry['title']
current['file'] = entry['text']
current['path'] = os.path.splitext(current['file'])[0] + '.html'
current['previous'] = None
current['next'] = None
if course_details['modules']:
path = course_details['modules'][-1]
previous = course_details['index'][path]
previous['next'] = current['path']
course_details['modules'].append(current['path'])
course_details['index'][current['path']] = current
return current
if 'intro' in data['details']:
if 'text' in data['details']['intro']:
create_module(data['details']['intro'], 'Introduction')
for entry in data['details']['steps']:
create_module(entry)
if 'finish' in data['details']:
if 'text' in data['details']['finish']:
create_module(data['details']['finish'], 'Summary')
return course_details
def load_workshop():
pathway_file = os.path.join(COURSES_DIRECTORY, 'pathway.json')
if not os.path.exists(pathway_file):
return
with open(pathway_file) as fp:
data = json.load(fp)
workshop_details = {}
workshop_details['title'] = data.get('title')
print('workshop title:', workshop_details['title'])
workshop_details['courses'] = []
workshop_details['index'] = {}
for course in data.get('courses', []):
name = course.get('course_id')
print('loading course:', name)
if name:
course_details = load_course(name)
if course_details:
workshop_details['courses'].append(name)
workshop_details['index'][name] = course_details
return workshop_details