""" process.py

* Rewrites an XML file as another using an XSL transform stylsheet.

* Joeri Honnef
* Aitor Azcarate Onaindia
* Abdullah Zeki Oszoy
* Paul Koppen

"""

import os
import sys

# http://www.xmlsoft.org/
import libxml2
import libxslt


"""
Example usage:
	
	python process.py emotion.xsl alice.emsable.xml output.sable

"""


def doTransform( source_xsl, source_xml, target_xml ):
	""" Transforms source_xml with source_xsl to target_xml. """
	# build objects
	styledoc = libxml2.parseFile( source_xsl )
	style    = libxslt.parseStylesheetDoc( styledoc )
	doc      = libxml2.parseFile( source_xml )
	
	# transform
	result   = style.applyStylesheet( doc, None )
	
	# save
	style.saveResultToFilename( target_xml, result, 0 )
	
	# clean up
	style.freeStylesheet()
	doc.freeDoc()
	result.freeDoc()

def runSable( sable_xml ):
	""" Runs festival on sable_xml (only on Unix). """
	# Can only be run on Unix.
	os.system( 'festival --tts %s &' %filename )



""" Call from command prompt. """
if len( sys.argv ) == 4:
	doTransform( sys.argv[1], sys.argv[2], sys.argv[3] )
	runSable( sys.argv[3] )
else:
	print "When running this script from command prompt, use:"
	print sys.argv[0], "xsltfile xmlfile outputfile"
	print "From within python, call doTransform( s_xsl, s_xml, s_target )."
