SPARQL Parser for INSERT and DELETE queries
<p>I need to test if a certain <code>SPARQL</code> queries are indeed an <code>INSERT</code> or <code>DELETE</code> statements. Therefore, I have been using different parsers (mainly, <code>fyzz</code> and <code>SPARQLWrapper</code>) as follow: </p>

<pre><code>try:
    sparql = parse(Query_String)
except Exception as e:
    raise ContentsUnacceptable(
        " The query attribute does not conform to the SPARQL query syntax !!")
</code></pre>

<p>and :</p>

<pre><code>try:
    sparql = SPARQLWrapper("local/graph/file")
    sparql.setQuery(Query_String)
    results = sparql.query().convert()
except Exception as e:
    raise ContentsUnacceptable(
        " The query does not conform to the SPARQL query syntax !!")
</code></pre>

<p>Both of my code work fine with <code>SELECT</code> statements, but as soon as I try an <code>INSERT</code> or <code>DELETE</code> statements, I got the exception written on my code <code>"The query does not conform to the SPARQL query syntax !!"</code> which it may means that the INSERT and DELETE statement are not recognizable by the parser. According to my research there are mainly those two parser for SPARQL queries. is there a way to make those statements recognizable by the fyzz and SPARQLWrapper parser?</p>

<p>SPARQLWrapper is not actually intended as a SPARQL parser. All it does is act as a client for a SPARQL endpoint service - AFAICT the actual parsing of the query string is done by the endpoint itself.</p>

<p>In your specific example, you're pointing SPARQLWrapper to a local file, which simply won't work, because a local file is not a SPARQL endpoint. In the attempt where you <em>are</em> pointing to a SPARQL endpoint, that endpoint is not accepting SPARQL updates (because, understandably, they don't allow remote clients to update their data). </p>

<p>If you wish to stick with SPARQLWrapper, a (<em>very</em> clunky) solution would be to set up your own SPARQL endpoint service, configure it to allow remote updates, and then use that endpoint to validate your queries/updates (there's plenty of free SPARQL endpoint implementations available including RDF4J Server and Jena Fuseki, to name but two popular ones).</p>

<p>But as said, this is a clunky solution: if your goal is simply to parse the string to validate its syntax, you should use an actual SPARQL parser in Python. <a href="http://rdflib3.readthedocs.io/en/latest/apidocs/rdflib.plugins.sparql.html#module-rdflib.plugins.sparql.parser" rel="nofollow">RDFLib's SPARQL Parser</a> is probably a good choice here. I'm no Python expert but I imagine something along these lines:</p>

<pre><code>import rdflib.plugins.sparql.parser

parser.parseQuery("SELECT ...")
</code></pre>

<p>and </p>

<pre><code>parser.parseUpdate("INSERT ...")
</code></pre>

<p>can help you out.</p>

python parsing sparql rdf