#!/usr/bin/python

"""Simple web demo .... read from form, perform action
based on data entered onto form, output results"""

import cgi
import os
import commands
import re

inputs = cgi.FieldStorage()
fill = {}
for key in inputs:
   fill[key] = inputs[key].value

userinput = fill.get("query")

print "content-type: text/html\n"

print "<html><head><title>SPARQL to N3 Conversion</title></head>"
print "<body bgcolor=#DDDDDD><h1>SPARQL to N3 Query Conversion</h1>"
print """
<h3>Part of the <a href="http://dig.csail.mit.edu/2009/IARPA-PIR/">
IARPA-PIR Project</a></h3>
"""
print """
<p>Please enter the <a href="http://www.w3.org/TR/rdf-sparql-query/">
SPARQL</a> query you would like to translate.
Here's an example, or enter your own.
<a href="http://www.w3.org/2001/sw/DataAccess/tests/r2">
Here are some great test cases.</a>
Please note that this is a work in progress, and some features
are not yet implemented.</p>
<form><textarea name=query cols=60 rows=9>"""

if userinput > -1:
       print userinput.rstrip('\n') + '</textarea>'
else:
       print """# Sample SPARQL query that queries for SSN, age, OpenID.
PREFIX example: &lt;http://example.com/#&gt;
PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
SELECT * WHERE {
     ?s example:ssn ?n. ?s foaf:age ?a. ?s foaf:openid ?id.
     FILTER (?a &gt; 18) }"""

print """
</textarea><br>
<input type="submit" value="Translate">
</form>
  <form method="post" action="sparql2n3.py">
  <input name="url" type="hidden" value="sparql2n3.py">
  <input type="submit" value="Reset">
  </form>
"""
if userinput > -1:
       # DEBUG: We can print the user input as a sanity check here.
       # This should not be necessary as it stays in the input form.
       #print "You entered:<br><pre>"
       #inputcopy = userinput
       #inputcopy = inputcopy.replace("<", "&lt;")
       #inputcopy = inputcopy.replace(">", "&gt;")
       #print inputcopy
       print "</pre>The translated output is:<br>"
       #f = open('/tmp/sparql-input-query.rq', 'w')
       # We now support true tempfile creation.
       fname = '/tmp/sparql_%s.txt' % os.getpid()
       f = open(fname, 'w+b')
       f.write(userinput)
       f.close()
       #print commands.getoutput('cat /tmp/hard-query.rq')
       #print commands.getoutput('./sparql2n3 /tmp/hard-query.rq')
       #commands.getoutput('./sparql2n3 /tmp/sparql-input-query.rq > /tmp/n3output')
       gname = '/tmp/n3_%s.txt' % os.getpid()
       g = open(gname, 'w+b')
       arg = "./sparql2n3 " + f.name + " > " + g.name
       #print arg
       commands.getoutput(arg)
       #f = open('/tmp/n3output', 'r')
       print "<pre>"
       for line in g:
           # Go line by line
           linetemp = line
           # Strip tags
           linetemp = linetemp.replace("<", "&lt;")
           linetemp = linetemp.replace(">", "&gt;")
           # Strip newline
           linetemp = linetemp[0:len(linetemp)-1]
           print linetemp
           #print "<br>"
       g.close()
       os.remove(fname)
       os.remove(gname)
       print "</pre>"

print """
<address><small>Maintained by <a href="mailto:jsoltren@csail.mit.edu">Jose Hiram Soltren</a></small></address>
"""
print "</body></html>"
