summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--export_problems.py108
1 files changed, 108 insertions, 0 deletions
diff --git a/export_problems.py b/export_problems.py
new file mode 100644
index 0000000..7191e74
--- /dev/null
+++ b/export_problems.py
@@ -0,0 +1,108 @@
+#!/usr/bin/python3
+# coding=utf-8
+
+import sys, sqlite3, os, os.path, re
+
+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)
+
+re_filenamify = re.compile(r''' |\.|;|,|:|"|'|\*|\(|\)|\/|\\|&|%|\$|\#|!|\n|\r|\t|\|''', re.MULTILINE)
+def filenamefy(name):
+ return '_'.join(re_filenamify.split(name)).lower().strip('_')
+
+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() \ No newline at end of file