Run this code in the same directory as the Linux BibUtils executables.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
#!/usr/bin/python2.4
#!c:\Python23\python.exe
class convertError(Exception):
pass
# inputs: searchTerms, count, startIndex, format
# ignore startPage
def getArgs(form):
if form.has_key('from'):
fromValue = form['from'].value
else:
raise convertError('No from')
if form.has_key('to'):
toValue = form['to'].value
else:
raise convertError('No to')
if form.has_key('url'):
url = form['url'].value
else:
raise convertError('No url')
if form.has_key('mimetype'):
mimetype = form['mimetype'].value
else:
mimetype = 'text/plain'
return (fromValue, toValue,url,mimetype)
def convert(fromValue, toValue,url,mimetype):
import os, urllib, tempfile, time
#binDir = r'C:\utils\bibutils'
binDir = '/home/yee/web/raymondyee.net/projects/bibutils'
# can this conversion be done? does it require one or two steps?
# first, convert the input file to xml, unless the input is already xml
#execFile1 = binDir + '\%s2%s.exe' % (fromValue,'xml')
#execFile2 = binDir + '\%s2%s.exe' % ('xml',toValue)
execFile1 = binDir + '/%s2%s' % (fromValue,'xml')
execFile2 = binDir + '/%s2%s' % ('xml',toValue)
if ((fromValue == 'xml') and os.path.exists(execFile2)) or (os.path.exists(execFile1) and os.path.exists(execFile2)) or \
((toValue == 'xml') and os.path.exists(execFile1)):
# read the file from the url
h = urllib.urlopen(url)
s = h.read()
h.close()
# make the temporary directory
fd = tempfile.mkdtemp()
xmlfname = fd + '/xml_' + str(int(time.time()))
# step1 input -> xml (if necessary)
if fromValue == 'xml':
f = open(xmlfname, 'w')
f.write(s)
f.close()
else:
fname = fd + '/input_' + str(int(time.time()))
f = open(fname, 'w')
f.write(s)
f.close()
# step1 input -> xml (if necessary)
# now run conversion1
commandLine1 = "%s %s" % (execFile1,fname)
outputHandle = os.popen(commandLine1)
output = outputHandle.read()
outputHandle.close()
os.remove(fname)
f = open(xmlfname, 'w')
f.write(output)
f.close()
# step2 xml -> output (if necessary)
if toValue == 'xml': # just return what is the xml file
f = open(xmlfname)
output = f.read()
f.close()
else:
commandLine2 = "%s %s" % (execFile2,xmlfname)
outputHandle = os.popen(commandLine2)
output = outputHandle.read()
outputHandle.close()
os.remove(xmlfname) # get rid of temp file and tempdir
os.rmdir(fd)
return output
else:
raise convertError('requested conversion not possible')
def do_cgi():
import cgi
form = cgi.FieldStorage()
import sys
sys.stderr = sys.stdout
try:
try:
(fromValue, toValue,url,mimetype) = getArgs(form)
except Exception, e:
print "Content-type: text/html\n"
print "<html><body>%s</body></html>" % (str(e))
raise convertError('exception thrown by getArgs')
#(fromValue, toValue,url,mimetype)=('bib','xml','http://raymondyee.net/wiki/BibTex_2fSampleFile1?action=raw&mimetype=text/plain','application/xml')
print "Content-type: %s\n" % (mimetype)
print convert(fromValue, toValue,url,mimetype)
except convertError, e:
print "Content-type: text/html\n"
print "<html><body>%s</body></html>" % (str(e))
def main():
(fromValue, toValue,url,mimetype)=('bib','end','http://raymondyee.net/wiki/BibTex_2fSampleFile1?action=raw&mimetype=text/plain','application/xml')
print convert(fromValue, toValue,url,mimetype)
if __name__ == '__main__':
do_cgi()
#main()
else:
try:
do_cgi()
except Exception, e:
print "Content-type: text/html\n"
print "<html><body>Error: %s</body></html>" % (str(e)) |
