#!/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
#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):
#
#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 = ''
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 = '