Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/generate_basic_lesson.py
diff options
context:
space:
mode:
Diffstat (limited to 'generate_basic_lesson.py')
-rwxr-xr-xgenerate_basic_lesson.py407
1 files changed, 407 insertions, 0 deletions
diff --git a/generate_basic_lesson.py b/generate_basic_lesson.py
new file mode 100755
index 0000000..8876c3f
--- /dev/null
+++ b/generate_basic_lesson.py
@@ -0,0 +1,407 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+import sys, subprocess
+from path import path
+
+MENUS = path('/home/tony/Desktop/git/generate/menus')
+#
+def getTag(txt, tag):
+ start = txt.find(tag)+len(tag)+1
+ end = txt[start:].find(' ')+start
+ if end < start:
+ end = len(txt)
+ return txt[start:end]
+
+def getImageData(image):
+ #parse image:format: 1_yyyy.png height xxx width yyy left
+ #get image number
+ end = image.find('_')
+ imgno = image[:end]
+ #get image name
+ start = image.find('_')
+ end = image.find(' ')
+ name = image[start+1:end]
+ #get height
+ height = getTag(image, 'height')
+ width = getTag(image, 'width')
+ if 'left' in image:
+ ifloat = 'left'
+ elif 'right'in image:
+ ifloat = 'right'
+ else:
+ ifloat = None
+ return (imgno, name, height, width, ifloat)
+
+def makeQuiz(screen):
+ #scan for quiz comments
+ tag = '<!--Q'
+ endTag = '-->'
+ quiz = []
+ if tag+endTag in screen:
+ return True, quiz
+ while tag in screen:
+ #comment = <!--Q:'question' A:'answer'-->
+ start = screen.find(tag)
+ end = screen.find(endTag)
+ question = screen[start+4:end]
+ if question:
+ quiz.append(question)
+ screen = screen[end+len(endTag):]
+ return False, quiz
+
+
+def findImages(screen):
+ #process imagelist
+ imageList = []
+ tag = '<!--I'
+ endTag = '-->'
+ while tag in screen:
+ start = screen.find(tag)
+ end = screen.find(endTag)
+ comment = screen[start+len(tag):end]
+ if len(comment) > 0:
+ imageList.append(comment)
+ screen = screen[end+len(endTag):]
+ return imageList
+
+
+def findAudio(screen):
+ #process audiolist
+ audioList = []
+ tag = '<!--'
+ tags = "ARSXH"
+ endTag = '-->'
+ while tag in screen:
+ start = screen.find(tag)
+ end = screen.find(endTag)
+ comment = screen[start+len(tag):end]
+ if len(comment) > 0 and comment[0] in tags:
+ audioList.append(comment)
+ screen = screen[end+len(endTag):]
+ return audioList
+
+def generate_image(imageList):
+ #format: nn_img1.png height:xxx width:yyy left
+ txtout = ''
+ for line in imageList:
+ (imgno, name, height, width, ifloat) = getImageData(line)
+ real = path(name).namebase
+ txtout = txtout + " $('#I" + imgno + "')\n"
+ if ifloat:
+ txtout = txtout + " .addClass('image_"+ifloat+"')\n"
+ #txtout = txtout + " .append(karma.createImg('" + real + "'))\n"
+ txtout += " $('<img>',{\n"
+ txtout += " src:host+pth+'/"+name+"'\n })\n .appendTo('#I"+imgno+"')\n"
+ return txtout
+
+def generate_load(pth):
+ lesson = ''
+ lesson = lesson + " $('<div id="
+ lesson = lesson + '"txtMain"/>'
+ lesson = lesson + "')\n"
+ lesson = lesson + " .appendTo('#content')\n"
+ lesson = lesson + " .load(host+'cgi-bin/getFile.py',\n"
+ if screen == 0:
+ lesson = lesson + " {'filename':" + pth + "/a.txt'},\n"
+ else:
+ lesson = lesson + " {'filename':" + pth + "/a" + str(screen) + ".txt'},\n"
+ lesson = lesson + " function(){\n"
+ return lesson
+
+def generate_audio(audiolist):
+ #format A1_clip.ogg or R1_clip.ogg or X1_clip.ogg or S1_clip.txt or H1_clip.ogg
+ txtout = ''
+ for line in audiolist:
+ typ = line[0]
+ pos = line.find('_')
+ clip = line[pos+1:]
+ if typ == 'A':
+ txtout += " playAudio(pth+'"+clip+"')\n"
+ elif typ == 'X':
+ txtout += " playXoAudio(pth+'"+clip+"')\n"
+ elif typ == 'H':
+ txtout += " playHelp(pth+'"+clip+"')\n"
+ elif typ == 'S' and line[1] != 'G':
+ txtout += " sayText(pth+'"+clip+"')\n"
+ elif typ == 'R':
+ txtout += " recordAudio(pth+'"+clip+"')\n"
+ return txtout
+
+def generateLessonCSS(imageList):
+ txtout = ''
+ for image in imageList:
+ (imgno, name, height, width, ifloat) = getImageData(image)
+ insert = "#I"+imgno+"{height:"+height+"px; width:"+width+"px; }\n\n"
+ txtout += insert
+ return txtout
+
+def generateLessonKarma(imageList):
+ txtout = 'function lesson_karma(){\n return Karma({\n image:[\n'
+ for image in imageList:
+ (imgno, name, height, width, float) = getImageData(image)
+ txtout += "{name:'"+path(name).namebase+"', file:'"+name+"'},\n"
+ txtout += ' ],\n audio:[\n ]})};\n'
+ return txtout
+
+def generateQuiz(activity, quizList):
+ for i in range(len(quizList)):
+ quiz = quizList[i]
+ if len(quizList) == 1:
+ txtout = 'var quiz = {\n'
+ else:
+ txtout = 'var quiz'+str(i+1)+' = {\n'
+ tflist = []
+ mclist = []
+ salist = []
+ #we need to make a list of the questions by type: multilist, tf, fill
+ #note: we should have cloze and sa should accept variant answers (e.g a/b where a, b are regular expressions
+ for question in quiz:
+ if "S:" in question:
+ mclist.append(question)
+ elif "T:" in question or 'F:' in question:
+ tflist.append(question)
+ else:
+ salist.append(question)
+ if mclist:
+ txtout += ' multiList:[\n'
+ for q in mclist:
+ #process question and add to txtout
+ apos = q.find('A:')
+ spos = q.find('S:')
+ ans = 'ans: ' + q[apos+2:spos] + ', '
+ sel = 'ansSel: [' + q[spos+2:] + ']'
+ ques = '{ques: ' + q[:apos] + ', '
+ txtout += ques + ans + sel + '},\n'
+ txtout += '],\n'
+ if tflist:
+ txtout += ' tf:[\n'
+ for q in tflist:
+ #process questionstring and add to quiztxt
+ apos1 = q.find('T:')
+ apos2 = q.find('F:')
+ ans = 'ans:' + q[apos1+2:apos2] + ', '
+ sel = 'ansSel:' + q[apos2+2:]
+ ques = '{ques: ' + q[:apos1] + ', '
+ txtout += ques + ans + sel + '},\n'
+ txtout += '],\n'
+ if salist:
+ txtout += ' fill:[\n'
+ for q in salist:
+ #process question string and add to quiztxt
+ apos = q.find('A:')
+ ans = 'ans: ' + q[apos + 2:]
+ ques = '{ques: ' + q[2:apos] + ','
+ txtout += ques + ans + '},\n'
+ txtout += ' ]\n'
+ txtout += '};\n\n'
+ txtout += 'var options = {\n'
+ txtout += " title: '" + activity.namebase + "',\n"
+ txtout += ' random: false,\n'
+ txtout += ' allrandom: false,\n'
+ txtout += '};\n'
+ return txtout
+
+def generateLesson(srcpth, pth, screens, imageList, audioList, quiz):
+ #generate lesson.js
+ tag = '<!--SG-->'
+ txtout = ''
+ lessonpth = srcpth / 'lesson.js'
+ if lessonpth.exists():
+ fin = open(lessonpth, 'r')
+ txtout += fin.read()
+ txtout += '\n'
+ fin.close()
+ if len(screens) == 1:
+ #single screen
+ txtout += 'function initialize(karma) {\n'
+ txtout += " host = 'http://localhost:8008/'\n"
+ txtout += " pth='"+pth+"/'\n"
+ txtout += " if(mode=='Faculty'){\n $('#ĺinkEdit').addClass('linkEdit');\n"
+ txtout += " }else{\n"
+ txtout += " $('#linkApply')\n .addClass('linkApply')\n .attr('score','70');\n }\n"
+ txtout += " $('<div id="
+ txtout += '"txtMain"/>'
+ txtout += "')\n"
+ txtout += " .appendTo('#content')\n"
+ txtout += " .load(host+'cgi-bin/getFile.py',\n"
+ txtout += " {'filename':pth+'a.txt'},\n"
+ txtout += " function(){\n"
+ if imageList and imageList[0]:
+ txtout += generate_image(imageList[0])
+ if audioList and audioList[0]:
+ txtout += generate_audio(audioList[0])
+ txtout += ' });\n'
+ if tag in screens[0]:
+ txtout += " $('#linkStart')\n"
+ txtout += " .addClass('linkStart')\n"
+ txtout += " .click(function(){startGame(karma);\n"
+ txtout += " });\n"
+ txtout += '};\n\n'
+ if not 'startGame' in txtout:
+ txtout += 'function startGame(karma) {\n'
+ if quiz:
+ txtout += " $('#linkStart').addClass('linkStart');\n"
+ txtout += " $('<div id=" + '"quizArea"/>' + "')\n"
+ txtout += ' .appendTo("#content")\n'
+ txtout += ' $("#quizArea")\n'
+ txtout += ' .jQuizMe(quiz, options)\n'
+ txtout += '};\n\n'
+ txtout += 'setUpLesson(initialize, startGame);\n'
+ else:
+ #multiple screen
+ txtout += 'var currentScreen;\n\n'
+ for i in range(len(screens)):
+ screen = i+1
+ txtout += 'function generateScreen'+str(screen)+'(karma) {\n'
+ txtout += ' currentScreen = ' + str(i+1) + '\n'
+ txtout += " host = 'http://localhost:8008/'\n"
+ txtout += " pth='"+pth+"/'\n"
+ txtout += " if(mode=='Faculty'){\n $('#ĺinkEdit').addClass('linkEdit');\n"
+ txtout += " }else{\n"
+ txtout += " $('#linkApply')\n .addClass('linkApply')\n .attr('score','70');\n }\n"
+ txtout += " $('<div id="
+ txtout += '"txtMain"/>'
+ txtout += "')\n"
+ txtout += " .appendTo('#content')\n"
+ txtout += " .load(host+'cgi-bin/getFile.py',\n"
+ txtout += " {'filename':pth+'a" + str(screen) + ".txt'},\n"
+ txtout += " function(){\n"
+ if imageList[i]:
+ txtout += generate_image(imageList[i])
+ txtout += ' });\n'
+ if audioList[i]:
+ txtout += generate_audio(audioList[i])
+ if tag in screens[i]:
+ txtout += " $('#linkStart')\n"
+ txtout += " .addClass('linkStart')\n"
+ txtout += " .click(function(){startGame(karma);});\n"
+ txtout += '};\n'
+ txtout += '\n'
+ txtout += 'function initialize() {};\n\n'
+ if not 'startGame' in txtout:
+ txtout += 'function startGame(karma){\n'
+ if quiz:
+ if not srcpth / 'quiz.js':
+ txtout += " url = window.location+'';\n"
+ txtout += " tmp = url.split('/');\n"
+ txtout += " tmp.pop();\n"
+ txtout += " tmp.shift();\n"
+ txtout += " tmp.shift();\n"
+ txtout += " tmp.shift();\n"
+ txtout += " pth = tmp.join('/');\n"
+ txtout += " $('div id = " + '"workArea"'+"'/>)\n"
+ txtout += " .load(host+'cgi/writeQuiz.py',\n"
+ txtout += " {'activity':pth,'text':quiztxt});\n"
+ txtout += " $('#linkStart').addClass('linkStart');\n"
+ txtout += " $('<div id=" + '"quizArea"/>' + "')\n"
+ txtout += ' .appendTo("#content")\n'
+ txtout += ' $("#quizArea")\n'
+ txtout += ' .jQuizMe(quiz[currentScreen], options)\n'
+ txtout += '};\n\n'
+ txtout += '\nsetUpMultiScreenLesson([\n'
+ for i in range(len(screens)):
+ txtout += ' generateScreen' + str(i+1) + ',\n'
+ txtout += ']);\n'
+ return txtout
+
+def makeScreens(txt):
+ screens = []
+ tag = '<hr />'
+ while tag in txt:
+ pos = txt.find(tag)
+ screen = txt[:pos]
+ txt = txt[pos+len(tag):]
+ screens.append(screen)
+ if len(txt)>0:
+ screens.append(txt)
+ return screens
+
+def addSpans(screen):
+ tag = '<!--I'
+ endTag = '-->'
+ start = 0
+ while screen[start:].find(tag)>-1:
+ pos1 = screen[start:].find(tag)
+ pos2 = screen[start:].find(endTag)
+ end = start + pos2
+ comment = screen[start+pos1+len(tag):end]
+ if len(comment) > 0:
+ imgno, name, height, width, ifloat = getImageData(comment)
+ span = "<span id = 'I" + imgno + "'></span>"
+ screen = screen[:end+len(endTag)] + span + screen[end+len(endTag):]
+ end = end + len(span)
+ start = end + len(endTag)
+ return screen
+
+def generateBasicLesson(actpth, srcpth, dstpth):
+ #read source.txt
+ txtpth = srcpth / 'source.txt'
+ fin = open(txtpth,'r')
+ txt = fin.read()
+ fin.close()
+ #copy index.html to dst
+ subprocess.call('cp ' + MENUS / 'index.html ' + dstpth, shell=True)
+ #copy source.txt to dst
+ subprocess.call('cp ' + srcpth / 'source.txt ' + dstpth, shell=True)
+ #copy assets to dst
+ images = path(srcpth).files('*.png')
+ for image in images:
+ subprocess.call('cp ' + image + ' ' + dstpth, shell=True)
+ images = path(srcpth).files('*.ogg')
+ for image in images:
+ subprocess.call('cp ' + image + ' ' + dstpth, shell=True)
+ #we don't copy gif because Karma doesn't handle them - they need to be converted to png
+ #subprocess.call('cp ' + srcpth / '*.gif ' + dstpth, shell = True)
+ #scan source text returning list of screens (a*.txt)
+ screens = makeScreens(txt)
+ quiz = []
+ imageList = []
+ audioList = []
+ quizFlag = False #True when activity contains generated quiz
+ for i in range(len(screens)):
+ screen = screens[i]
+ flag, quizText = makeQuiz(screen)
+ if flag:
+ quizFlag = True
+ if quizText:
+ quiz.append(quizText)
+ imageList.append(findImages(screen))
+ audioList.append(findAudio(screen))
+ #write screen
+ #but first add spans for images
+ screen = addSpans(screen)
+ if len(screens) == 1:
+ fout = open(dstpth / 'a.txt','w')
+ else:
+ fout = open(dstpth / 'a' + str(i+1) + '.txt','w')
+ fout.write(screen)
+ fout.close()
+ #create lesson.js
+ txtout = generateLesson(srcpth, actpth, screens, imageList, audioList, quiz)
+ fout = open(dstpth / 'lesson.js','w')
+ fout.write(txtout)
+ fout.close()
+ if imageList:
+ totalList = []
+ for list in imageList:
+ for image in list:
+ totalList.append(image)
+ #create lesson-karma.js
+ txtout = generateLessonKarma(totalList)
+ fout = open(dstpth / 'lesson-karma.js','w')
+ fout.write(txtout)
+ fout.close()
+ #create lesson.css
+ txtout = generateLessonCSS(totalList)
+ fout = open(dstpth / 'lesson.css','w')
+ fout.write(txtout)
+ fout.close()
+ if quiz and not quizFlag:
+ #create quiz.js
+ txtout = generateQuiz(dstpth, quiz)
+ fout = open(dstpth / 'quiz.js','w')
+ fout.write(txtout)
+ fout.close()
+
+