-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlive-loop.py
138 lines (124 loc) · 5.35 KB
/
live-loop.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
##
# This is the web service for the Live Loop RDFa editor.
# License: LGPLv3
#
# @author Manu Sporny
import os, os.path, sys
import subprocess
import rdfa
try:
from mod_python import apache
except Exception, e:
pass
##
# Formats a triple in a very special way.
#
# @param request the HTTP request to use when writing the output.
# @param subject the subject of the triple.
# @param predicate the predicate for the triple.
# @param obj the object of the triple.
# @param object_type the type for the object in the triple.
# @param datatype the datatype for the object in the triple.
# @param language the language for the object in the triple.
def writeTriple( \
request, subject, predicate, obj, object_type, datatype, language):
request.write("<%s> <%s> " % (subject, predicate))
if(object_type == rdfa.RDF_TYPE_IRI):
request.write("<%s> . " % (obj,))
else:
ostr = ""%s" ." % (obj,)
if(language != None):
ostr += "@%s" % (language,)
if(datatype != None):
ostr += "^^^<%s>" % (datatype,)
request.write(ostr)
##
# Called whenever a triple is generated for the default graph by the
# underlying implementation.
#
# @param request the HTTP request to use when writing the output.
# @param subject the subject of the triple.
# @param predicate the predicate for the triple.
# @param obj the object of the triple.
# @param object_type the type for the object in the triple.
# @param datatype the datatype for the object in the triple.
# @param language the language for the object in the triple.
def defaultTriple( \
request, subject, predicate, obj, object_type, datatype, language):
request.write("<div class=\"rdfa-default-triple\">")
writeTriple( \
request, subject, predicate, obj, object_type, datatype, language)
request.write("</div>")
##
# Called whenever a triple is generated for the processor graph by the
# underlying implementation.
#
# @param request the HTTP request to use when writing the output.
# @param subject the subject of the triple.
# @param predicate the predicate for the triple.
# @param obj the object of the triple.
# @param object_type the type for the object in the triple.
# @param datatype the datatype for the object in the triple.
# @param language the language for the object in the triple.
def processorTriple( \
request, subject, predicate, obj, object_type, datatype, language):
request.write("<div class=\"rdfa-processor-triple\">")
if(object_type == rdfa.RDF_TYPE_NAMESPACE_PREFIX):
request.write("%s %s: %s ." % (subject, predicate, obj))
else:
writeTriple( \
request, subject, predicate, obj, object_type, datatype, language)
request.write("</div>")
##
# Called whenever the processing buffer for the C-side needs to be re-filled.
#
# @param data the entire file blob
# @param bufferSize the size of the buffer to return. Returning anything less
# than bufferSize will halt execution after the returned
# buffer has been processed.
def handleBuffer(data, bufferSize):
return data.read()
##
# The handler function is what is called whenever an apache call is made.
#
# @param req the HTTP request.
#
# @return apache.OK if there wasn't an error, the appropriate error code if
# there was a failure.
def handler(req):
# File that runs an apache test.
status = apache.OK
puri = req.parsed_uri
service = puri[-3]
argstr = puri[-2]
args = {}
# Retrieve all of the unit tests from the W3C website
if(service.find("/live-loop/triples") != -1):
req.content_type = 'text/html'
if(req.method == "POST"):
parser = rdfa.RdfaParser("http://example.com/sample.html", False)
parser.setDefaultGraphTripleHandler(defaultTriple, req)
parser.setProcessorGraphTripleHandler(processorTriple, req)
parser.setBufferHandler(handleBuffer, req)
parser.parse()
else:
req.content_type = 'text/plain'
req.write("""
You can POST an XHTML+RDFa document to this service. The result will be an HTML snippet that can be placed inside of a <div> element in an HTML page.
For example, try the following using CURL:
curl -d "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><\!DOCTYPE html PUBLIC \\"-//W3C//DTD XHTML+RDFa 1.1//EN\\" \\"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd\\"> <html xmlns=\\"http://www.w3.org/1999/xhtml\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\"><head><title>Test Snippet</title></head><body><p>This HTML snippet was written by <span about=\\"\\" property=\\"dc:creator\\">Manu Sporny</span>.</p></body></html>" %s
""" % (req.construct_url(req.unparsed_uri),))
# Perform a git update in the current directory
elif(service.find("/live-loop/git-update") != -1):
testSuitePath = os.path.dirname(req.canonical_filename)
gitUpdatePath = os.path.join(testSuitePath, ".git")
p = subprocess.Popen(["git", "--git-dir", gitUpdatePath, "pull"],
bufsize=4096, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True, cwd=testSuitePath)
(so, se) = p.communicate()
req.write("GIT status: %s%s" % (so, se))
else:
req.content_type = 'text/html'
req.write("<strong>ERROR: Unknown Live Loop service: %s</strong>" % \
(service,))
return status