#!/usr/bin/env python2 import cgi, os, tempfile, string, re, sys import cgitb cgitb.enable() def cleanf(s): results = re.compile(r'(\"\"\".*?\"\"\")', re.S).findall(s) for m in results: mnew = string.replace(m[3:-3], '"', '\\"') mnew = '"'+string.replace(mnew, '\n', '')+'"' s = string.replace(s, m, mnew) return s def switchval(num): if (num=="1"): return '--rdf' if (num=="2"): return '--n3' if (num=="3"): return '--ntriples' return None def makecontent(): form = cgi.FieldStorage() if not (form.has_key("rdftext") and form.has_key("outputtype") and form.has_key("inputtype")): print "Error:\nMissing a variable, cannot continue." return intype = switchval(form.getfirst("inputtype")) outtype = switchval(form.getfirst("outputtype")) if (intype == None or outtype == None): print "Error:\nWrong input or output type." return tempname = tempfile.mktemp("-rdfconverter") temp = open(tempname, 'w') temp.write(form.getfirst("rdftext")) temp.close() os.chdir('/usr/local/share/cwm') #print("python2 cwm.py %s /tmp/temp.txt %s\n\n"%(intype,outtype)) cwmpipe = os.popen("python2 cwm.py %s %s %s"%(intype,tempname,outtype)) cwmtext = cwmpipe.read() if (form.getfirst("outputtype")=="3"): cwmtext = cleanf(cwmtext) print cgi.escape(cwmtext) print "Content-Type: text/html" # HTML is following print # blank line, end of header print """
""" makecontent() print """"""