Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cvtSiyavula.py
diff options
context:
space:
mode:
authorTony Anderson <tony_anderson@usa.net>2011-04-25 10:33:11 (GMT)
committer Tony Anderson <tony_anderson@usa.net>2011-04-25 10:33:11 (GMT)
commitb9a2719691b4c6cf83f31eb0b6c3e7e878524c0e (patch)
treef9c01373e56e9c1c749dfe9b1bf7c7482e05eb77 /cvtSiyavula.py
initial commitHEADmaster
Diffstat (limited to 'cvtSiyavula.py')
-rwxr-xr-xcvtSiyavula.py157
1 files changed, 157 insertions, 0 deletions
diff --git a/cvtSiyavula.py b/cvtSiyavula.py
new file mode 100755
index 0000000..6a6db14
--- /dev/null
+++ b/cvtSiyavula.py
@@ -0,0 +1,157 @@
+#!/usr/bin/python
+#create milestone of basic activities from Siyavula folder
+#source folder has text files a01..a99.txt representing individual activities after conversion from
+#doc format. Folder also has all of the images for the milestone.
+#
+#for each folder in SOURCE create a milestone folder in TARGET
+#for each a*.txt file in SOURCE folder create an activity folder in TARGET / milestone
+#also add an entry in activities.js
+#copy the a*.txt file to the activity folder as source.txt
+#for each image tag in a*.txt, replace the tag with a comment <!--In_imgn.png height xxx width xxx left-->
+#copy the source image to the activity folder renaming it imgn (where n is the number of the image in this activity)
+from path import path
+from PIL import Image
+import subprocess
+import sys
+from optparse import OptionParser
+
+SOURCE = path('../')
+TARGET = path('../trial/')
+
+def getField(element, tag):
+ pos1 = element.find(tag)
+ if pos1 < 0:
+ return ''
+ pos2 = element[pos1:].find(' ')
+ if pos2 < 0:
+ pos2 = len(element)
+ field = element[pos1+len(tag):pos1+pos2].replace('"','')
+ if tag == 'src=':
+ pos1 = field.find('html_')
+ field = '*' + field[pos1+len('html_'):]
+ return field
+
+def parseElement(count, element, folder, activityName):
+ # <img src="Math_Gr1_m1_NUMBER_FUN_html_m3e0e7122.png" border="1" alt="" hspace="12" width="100" height="115" align="LEFT" />
+ #need to build comment
+ tgtimg = 'img' + str(count) + '.png'
+ tgtpth = TARGET / folder /activityName / tgtimg
+ searchImg = getField(element, 'src=')
+ h = getField(element,'height=')
+ w = getField(element,'width=')
+ if len(h) < 1 or len(w) < 1:
+ print element, 'h:',h,'w:',w
+ align = getField(element,'align=')
+ comment = '<!--I' + str(count) +'_' + tgtimg + ' height ' + h + ' width ' + w + ' ' + align.lower() + '-->'
+ srcpth = SOURCE / folder
+ files = srcpth.files(searchImg)
+ srcimg = files[0].name
+ try:
+ im = Image.open(srcpth / srcimg)
+ try:
+ size = (int(w), int(h))
+ imt = im.resize(size,Image.ANTIALIAS)
+ imt.save(tgtpth)
+ except:
+ im.save(tgtpth,'png')
+ except IOError:
+ print 'cannot create thumbnail for', tgtpth, srcpth/srcimg
+ return comment
+
+def processImages(txt, folder, activityName):
+ tag = '<img '
+ endTag = '>'
+ count = 0
+ while tag in txt:
+ count += 1
+ start = txt.find(tag)
+ end = start + txt[start:].find(endTag)
+ element = txt[start+len(tag):end]
+ comment = parseElement(count, element, folder, activityName)
+ txt = txt[:start]+comment+txt[end+len(endTag):]
+ return txt
+
+#get command line options
+parser = OptionParser(usage="Usage: %prog [options] file")
+(options, args) = parser.parse_args()
+if not args:
+ print 'Specify a course (e.g. zs4) as argument.'
+ parser.print_help()
+ sys.exit(1)
+
+SUBJECT = path(args[0])
+COURSE = path(args[1])
+if SUBJECT == 'siyavula_science':
+ sbj = 'sci'
+elif SUBJECT == 'siyavula_technology':
+ sbj = 'tek'
+elif SUBJECT == 'siyavula_mathematics':
+ sbj = 'ma'
+elif SUBJECT == 'siyavula_english':
+ sbj = 'en'
+else:
+ print 'subject not known'
+ sys.exit()
+
+srcpth = SOURCE / SUBJECT / COURSE
+tgtpth = TARGET / SUBJECT / COURSE
+print 'pths', srcpth, tgtpth
+#create clean output folder
+subprocess.call('rm -rf ' + tgtpth, shell=True)
+subprocess.call('mkdir -p ' + tgtpth, shell=True)
+milestones = srcpth.dirs()
+milestones.sort()
+ms_entries = []
+mcount = 0
+for milestone in milestones:
+ subprocess.call('mkdir ' + tgtpth / milestone.namebase, shell=True)
+ fin = open(srcpth / milestone.namebase / 'source.txt', 'r')
+ txt = fin.read()
+ fin.close()
+ entry = ['0',str(mcount),sbj,'milestone',milestone.namebase,'cyan']
+ ms_entries.append(entry)
+ mcount += 1
+ acount = 0
+ entries = []
+ tag = '<hr />'
+ done = False
+ txtout = ''
+ while not done:
+ acount += 1
+ acnt = str(acount)
+ if len(acnt) < 2:
+ acnt = '0' + acnt
+ activityName = milestone.namebase + 'a' + acnt
+ entry = [str(acount),str(acount),'Technology','basic',activityName,'blue']
+ entries.append(entry)
+ tgt = tgtpth / milestone.namebase / activityName
+ print 'tgt', tgt
+ subprocess.call('mkdir ' + tgt, shell=True)
+ pos = txt.find(tag)
+ txtin = txt[:pos]
+ txt = txt[pos+len(tag):]
+ folder = SUBJECT / COURSE / milestone.namebase
+ txtout = processImages(txtin, folder, activityName)
+ fout = open(tgt / 'source.txt','w')
+ fout.write(txtout)
+ fout.close()
+ txtout = ''
+ if not tag in txt:
+ done = True
+ #write out activities.js
+ txtout = 'var activities = [\n'
+ for entry in entries:
+ txtout += str(entry) + ',\n'
+ txtout += ']\n\n'
+ fout=open(TARGET / folder / 'activities.js','w')
+ fout.write(txtout)
+ fout.close()
+#also write out milestones.js
+txtout = 'var activities = [\n'
+for entry in ms_entries:
+ txtout += str(entry) + ',\n'
+txtout += ']\n\n'
+fout = open(TARGET / SUBJECT / COURSE / 'milestones.js','w')
+fout.write(txtout)
+fout.close()
+