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
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Possible CGI entry point for the owl package.
@author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">}
@license: This software is available for use under the
U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">}
@contact: Ivan Herman, ivan@w3.org
"""
"""
$Id: RDFConvertService.py,v 1.3 2009/07/10 08:45:14 ivan Exp $
"""
import os
import sys
import cgi
import cgitb
# prevent accidentally importing `scripts/owlrl` file when calling
# `from owlrl import ...` below.
HERE_DIR = os.path.abspath(os.path.dirname(__file__))
if HERE_DIR in sys.path:
sys.path.remove(HERE_DIR)
# Add 'owlrl' module from the parent directory into the path if it exists.
PARENT_DIR = os.path.dirname(HERE_DIR)
parent_dir_list = os.listdir(PARENT_DIR)
if 'owlrl' in parent_dir_list:
possible_owlrl = os.path.join(PARENT_DIR, 'owlrl')
if os.path.isdir(possible_owlrl):
sys.path.append(possible_owlrl)
__version__ = "4.0"
cgitb.enable()
form = cgi.FieldStorage()
from owlrl import convert_graph, RDFXML, TURTLE, AUTO
# ---------------------------------------------------------------------------------------------------------------------
class Options:
def __init__(self, frm):
self.iformat = AUTO
self.owlClosure = "no"
self.rdfsClosure = "no"
self.owlExtras = "no"
self.axioms = "no"
self.daxioms = "no"
self.sources = []
self.text = None
self.format = TURTLE
if "source_1" in list(frm.keys()):
self.sources.append(frm["source_1"].value)
if "source_2" in list(frm.keys()):
self.sources.append(frm["source_2"].value)
if "text" in list(frm.keys()):
self.text = frm["text"].value
if "format" in list(frm.keys()):
self.format = frm["format"].value
if "iformat" in list(frm.keys()):
v = frm["iformat"].value
if v == "xml" or v == "turtle":
self.iformat = v
if "fullClosure" in list(frm.keys()) and frm["fullClosure"].value == "yes":
self.owlClosure = "yes"
self.rdfsClosure = "yes"
self.axioms = "no"
self.daxioms = "no"
self.owlExtras = "no"
else:
if "owlClosure" in list(frm.keys()):
self.owlClosure = frm["owlClosure"].value
if "rdfsClosure" in list(frm.keys()):
self.rdfsClosure = frm["rdfsClosure"].value
if "owlExtras" in list(frm.keys()):
self.owlExtras = frm["owlExtras"].value
if "axioms" in list(frm.keys()):
self.axioms = frm["axioms"].value
if "daxioms" in list(frm.keys()):
self.daxioms = frm["daxioms"].value
# this one is for backward compatibility...
if "uri" in list(frm.keys()):
self.sources.append(frm["uri"].value)
if "source" in list(frm.keys()):
self.sources.append(frm["source"].value)
def to_html(self):
print('<dl>')
print('<dt>Sources:</dt>')
print('<dd>')
if len(self.sources) == 0:
print("none")
elif len(self.sources) == 1:
print(cgi.escape(self.sources[0]))
else:
print(cgi.escape(self.sources[0]), ", ", cgi.escape(self.sources[1]))
print('</dd>')
print('<dt>Input format:</dt>')
print('<dd>%s</dd>' % self.iformat)
print('<dt>Output format:</dt><dd>%s</dd>' % self.format)
print('<dt>OWL 2 RL Processing:</dt><dd>%s</dd>' % self.owlClosure)
print('<dt>RDFS Processing:</dt><dd>%s</dd>' % self.rdfsClosure)
print('<dt>Extra OWL Processing:</dt><dd>%s</dd>' % self.owlExtras)
print('<dt>Axiomatic triples added:</dt><dd>%s</dd>' % self.axioms)
print('<dt>Datatype Axiomatic triples added:</dt><dd>%s</dd>' % self.daxioms)
if self.text is not None:
print('<dt>Turtle code added to the graph:</dt>')
print('<dl>')
if self.text is not None:
print(cgi.escape(self.text).replace('\n', '<br/>'))
# ---------------------------------------------------------------------------------------------------------------------
options = Options(form)
try:
retval = convert_graph(options)
if options.format == TURTLE:
print('Content-Type: text/turtle; charset=utf-8')
else:
print('Content-Type: application/rdf+xml; charset=utf-8')
print()
print(retval)
except:
(typ, value, traceback) = sys.exc_info()
print('Content-type: text/html; charset=utf-8')
print('Status: 400 Invalid Input')
print()
print("<html>")
print("<head>")
print("<title>Error in RDF Closure processing</title>")
print("</head><body>")
print("<h1>Error in RDF Closure processing:</h1>")
print("<pre>%s</pre>" % value)
print("<h1>For reference, the input arguments were:</h1>")
options.to_html()
print("</body>")
print("</html>")
|