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
|
#!/usr/bin/python3
# coding=utf-8
import sys, sqlite3, os, os.path
from .utils import filenamefy
base_dir = '/tmp' # where to create the directory structure
if not os.path.isdir(base_dir):
os.mkdir(base_dir)
prolog_dir = base_dir + '/prolog'
prolog_facts_dir = prolog_dir + '/facts'
prolog_groups_dir = prolog_dir + '/problems'
if not os.path.isdir(prolog_dir):
os.mkdir(prolog_dir)
if not os.path.isdir(prolog_facts_dir):
os.mkdir(prolog_facts_dir)
if not os.path.isdir(prolog_groups_dir):
os.mkdir(prolog_groups_dir)
def log(line):
sys.stdout.write('{0}\n'.format(line))
fact_map = {}
group_map = {}
sqc = sqlite3.connect('db.sqlite3') # sqlite3 connection handle
try:
cur = sqc.cursor()
try:
for row in cur.execute('select id, name, facts from tutor_library'):
facts_module = filenamefy(row[1])
facts_filename = prolog_facts_dir + '/' + facts_module + '.py'
fact_map[row[0]] = facts_module
f = open(facts_filename, 'w', encoding='UTF-8', errors='replace', newline='')
try:
f.write("""\
# coding=utf-8
id = {0}
name = '{1}'
facts = '''\\
{2}'''
""".format(row[0], row[1], '\n'.join(row[2].split('\r\n'))))
finally:
f.close()
log('facts module: {0}'.format(facts_filename))
for row in cur.execute('select id, name from tutor_group'):
group_dir = filenamefy(row[1])
group_dir_abs = prolog_groups_dir + '/' + group_dir
if not os.path.isdir(group_dir_abs):
os.mkdir(group_dir_abs)
group_map[row[0]] = group_dir
log('group dir: {0}'.format(group_dir_abs))
for row in cur.execute('select id, group_id, number, name, slug, details, solution, visible, library_id from tutor_problem'):
group_dir = group_map[row[1]]
name = row[3]
problem_dir = filenamefy(name)
problem_dir_abs = prolog_groups_dir + '/' + group_dir + '/' + problem_dir # contains all python modules for the problem
if not os.path.isdir(problem_dir_abs):
os.mkdir(problem_dir_abs)
facts_module = None if row[8] is None else fact_map.get(row[8])
visible = row[7]
f = open(problem_dir_abs + '/common.py', 'w', encoding='UTF-8', errors='replace', newline='')
try:
f.write("""\
# coding=utf-8
id = {0}
group = '{1}'
number = {2}
visible = {3}
facts = {4}
solution = '''\\
{5}'''
""".format(row[0], group_dir, row[2], 'False' if visible == 0 else 'True', 'None' if facts_module is None else "'" + facts_module + "'", '\n'.join(row[6].split('\r\n'))))
finally:
f.close()
f = open(problem_dir_abs + '/en.py', 'w', encoding='UTF-8', errors='replace', newline='')
try:
f.write("""\
# coding=utf-8
id = {0}
name = '{1}'
slug = '{2}'
description = '''\\
{3}'''
hint = {{}}
""".format(row[0], name, row[4], '\n'.join(row[5].split('\r\n'))))
finally:
f.close()
log('problem: {0}'.format(problem_dir_abs))
finally:
cur.close()
finally:
sqc.close()
|