"
raise ImportError(msg)
except Exception :
# Something nasty happened:-(
e = sys.exc_info()[1]
self.http_status = 400
if not rdfOutput : raise e
return self._generate_error_graph(graph, str(e), uri=name)
except Exception :
# Something nasty happened:-(
e = sys.exc_info()[1]
if isinstance(e, ImportError) :
self.http_status = None
else :
self.http_status = 500
if not rdfOutput : raise e
return self._generate_error_graph(graph, str(e), uri=name)
def rdf_from_sources(self, names, outputFormat = "pretty-xml", rdfOutput = False) :
"""
Extract and RDF graph from a list of RDFa sources and serialize them in one graph. The sources are parsed, the RDF
extracted, and serialization is done in the specified format.
@param names: list of sources, each can be a URI, a file name, or a file-like object
@keyword outputFormat: serialization format. Can be one of "turtle", "n3", "xml", "pretty-xml", "nt". "xml" and "pretty-xml", as well as "turtle" and "n3" are synonyms.
@return: a serialized RDF Graph
@rtype: string
"""
try :
from pyRdfaExtras import MyGraph
graph = MyGraph()
except :
graph = Graph()
for prefix in _bindings :
graph.bind(prefix,Namespace(_bindings[prefix]))
# the value of rdfOutput determines the reaction on exceptions...
for name in names :
self.graph_from_source(name, graph, rdfOutput)
return graph.serialize(format=outputFormat)
def rdf_from_source(self, name, outputFormat = "pretty-xml", rdfOutput = False) :
"""
Extract and RDF graph from an RDFa source and serialize it in one graph. The source is parsed, the RDF
extracted, and serialization is done in the specified format.
@param name: a URI, a file name, or a file-like object
@keyword outputFormat: serialization format. Can be one of "turtle", "n3", "xml", "pretty-xml", "nt". "xml" and "pretty-xml", as well as "turtle" and "n3" are synonyms.
@return: a serialized RDF Graph
@rtype: string
"""
return self.rdf_from_sources([name], outputFormat, rdfOutput)
################################################# CGI Entry point
def processURI(uri, outputFormat, form) :
"""The standard processing of a microdata uri options in a form, ie, as an entry point from a CGI call.
The call accepts extra form options (eg, HTTP GET options) as follows:
@param uri: URI to access. Note that the "text:" and "uploaded:" values are treated separately; the former is for textual intput (in which case a StringIO is used to get the data) and the latter is for uploaded file, where the form gives access to the file directly.
@param outputFormat: serialization formats, as understood by RDFLib. Note that though "turtle" is
a possible parameter value, some versions of the RDFLib turtle generation does funny (though legal) things with
namespaces, defining unusual and unwanted prefixes...
@param form: extra call options (from the CGI call) to set up the local options (if any)
@type form: cgi FieldStorage instance
@return: serialized graph
@rtype: string
"""
def _get_option(param, compare_value, default) :
param_old = param.replace('_','-')
if param in list(form.keys()) :
val = form.getfirst(param).lower()
return val == compare_value
elif param_old in list(form.keys()) :
# this is to ensure the old style parameters are still valid...
# in the old days I used '-' in the parameters, the standard favours '_'
val = form.getfirst(param_old).lower()
return val == compare_value
else :
return default
if uri == "uploaded:" :
input = form["uploaded"].file
base = ""
elif uri == "text:" :
input = StringIO(form.getfirst("text"))
base = ""
else :
input = uri
base = uri
vocab_cache = _get_option( "vocab_cache", "true", True)
vocab_expansion = _get_option( "vocab_expansion", "true", False)
processor = pyMicrodata(base = base, vocab_expansion = vocab_expansion, vocab_cache = vocab_cache)
# Decide the output format; the issue is what should happen in case of a top level error like an inaccessibility of
# the html source: should a graph be returned or an HTML page with an error message?
# decide whether HTML or RDF should be sent.
htmlOutput = False
#if 'HTTP_ACCEPT' in os.environ :
# acc = os.environ['HTTP_ACCEPT']
# possibilities = ['text/html',
# 'application/rdf+xml',
# 'text/turtle; charset=utf-8',
# 'application/json',
# 'application/ld+json',
# 'text/rdf+n3']
#
# # this nice module does content negotiation and returns the preferred format
# sg = httpheader.acceptable_content_type(acc, possibilities)
# htmlOutput = (sg != None and sg[0] == httpheader.content_type('text/html'))
# os.environ['rdfaerror'] = 'true'
try :
graph = processor.rdf_from_source(input, outputFormat, rdfOutput = ("forceRDFOutput" in list(form.keys())) or not htmlOutput)
if outputFormat == "n3" :
retval = 'Content-Type: text/rdf+n3; charset=utf-8\n'
elif outputFormat == "nt" or outputFormat == "turtle" :
retval = 'Content-Type: text/turtle; charset=utf-8\n'
elif outputFormat == "json-ld" or outputFormat == "json" :
retval = 'Content-Type: application/json; charset=utf-8\n'
else :
retval = 'Content-Type: application/rdf+xml; charset=utf-8\n'
retval += '\n'
retval += graph
return retval
except HTTPError :
import cgi
h = sys.exc_info()[1]
retval = 'Content-type: text/html; charset=utf-8\nStatus: %s \n\n' % h.http_code
retval += "\n"
retval += "\n"
retval += "HTTP Error in Microdata processing\n"
retval += "\n"
retval += "HTTP Error in distilling Microdata
\n"
retval += "HTTP Error: %s (%s)
\n" % (h.http_code,h.msg)
retval += "On URI: '%s'
\n" % cgi.escape(uri)
retval +="\n"
retval +="\n"
return retval
except :
# This branch should occur only if an exception is really raised, ie, if it is not turned
# into a graph value.
(type,value,traceback) = sys.exc_info()
import traceback, cgi
retval = 'Content-type: text/html; charset=utf-8\nStatus: %s\n\n' % processor.http_status
retval += "\n"
retval += "\n"
retval += "Exception in Microdata processing\n"
retval += "\n"
retval += "Exception in distilling Microdata
\n"
retval += "\n"
strio = StringIO()
traceback.print_exc(file=strio)
retval += strio.getvalue()
retval +="
\n"
retval +="%s
\n" % value
retval +="Distiller request details
\n"
retval +="\n"
if uri == "text:" and "text" in form and form["text"].value != None and len(form["text"].value.strip()) != 0 :
retval +="- Text input:
- %s
\n" % cgi.escape(form["text"].value).replace('\n','
')
elif uri == "uploaded:" :
retval +="- Uploaded file
\n"
else :
retval +="- URI received:
'%s'
\n" % cgi.escape(uri)
retval +="- Output serialization format:
- %s
\n" % outputFormat
retval +="
\n"
retval +="\n"
retval +="\n"
return retval
###################################################################################################