-
Notifications
You must be signed in to change notification settings - Fork 12
/
converter-cgi.py
executable file
·149 lines (108 loc) · 3.54 KB
/
converter-cgi.py
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
143
144
145
146
147
148
#! /usr/bin/python /devel/WWW/2000/10/swap/cwm.py
"""
$Id$
convert-cgi
An CGI server applet which provides a form for playing with N3 syntax,
allowing conversion from N3 to conventional RDF.
This is an application which knows a certian amount of stuff and can manipulate it.
It uses llyn, a (forward chaining) query engine, not an (backward chaining) inference engine:
that is, it will apply all rules it can but won't figure out which ones to apply to prove something.
http://www.w3.org/DesignIssues/Notation3
Date: 2000/07/17 21:46:13
Agenda:
=======
"""
import string
import urlparse
# import re
# import StringIO
#import urllib # for log:content
import md5, binascii # for building md5 URIs
urlparse.uses_fragment.append("md5") #@@kludge/patch
urlparse.uses_relative.append("md5") #@@kludge/patch
import notation3 # N3 parsers and generators, and RDF generator
############################################################## Web service
# Author: DanC
#
import random
import time
import cgi
import sys
import StringIO
def serveRequest(env):
import random #for message identifiers. Hmm... should seed from request
#sys.stderr = open("/tmp/connolly-notation3-log", "w")
form = cgi.FieldStorage()
if form.has_key('data'):
try:
convert(form, env)
except BadSyntax, e:
print "Status: 500 syntax error in input data"
print "Content-type: text/plain"
print
print e
except:
import traceback
print "Status: 500 error in python script. traceback follows"
print "Content-type: text/plain"
print
traceback.print_exc(sys.stdout)
else:
showForm()
def convert(form, env):
""" raises KeyError if the form data is missing required fields."""
serviceDomain = 'w3.org' #@@ should compute this from env['SCRIPT_NAME']
# or whatever; cf. CGI spec
thisMessage = 'mid:t%s-r%f@%s' % (time.time(), random.random(), serviceDomain)
data = form['data'].value
if form.has_key('genspace'):
genspace = form['genspace'].value
else: genspace = thisMessage + '#_'
if form.has_key('baseURI'): baseURI = form['baseURI'].value
elif env.has_key('HTTP_REFERER'): baseURI = env['HTTP_REFERER']
else: baseURI = None
# output is buffered so that we only send
# 200 OK if all went well
buf = StringIO.StringIO()
xlate = notation3.ToRDFParser(buf, baseURI, thisMessage, genspace)
xlate.startDoc()
xlate.feed(data)
xlate.endDoc()
print "Content-Type: text/xml"
#hmm... other headers? last-modified?
# handle if-modified-since? i.e. handle input by reference?
print # end of HTTP response headers
print buf.getvalue()
def showForm():
print """Content-Type: text/html
<html>
<title>Notation3 to RDF Service</title>
<body>
<form method="GET">
<textarea name="data" rows="4" cols="40">
bind dc: <http://purl.org/dc/elements/1.1/>
</textarea>
<input type="submit"/>
</form>
<div>
<h2>References</h2>
<ul>
<li><a href="http://www.w3.org/DesignIssues/Notation3">Notation 3</a></li>
<li><a href="http://www.python.org/doc/">python documentation</a></li>
<li><a href="http://www.w3.org/2000/01/sw/">Semantic Web Development</a></li>
</ul>
</div>
<address>
<a href="http://www.w3.org/People/Connolly/">Dan Connolly</a>
</address>
</body>
</html>
"""
############################################################ Main program
if __name__ == '__main__':
import os
# import urlparse
# if os.environ.has_key('SCRIPT_NAME'):
serveRequest(os.environ)
# else:
# doCommand()