Skip to content
Snippets Groups Projects
Verified Commit 893876ba authored by David Beniamine's avatar David Beniamine
Browse files

WIP : call jar parser

parent d208eac0
Branches
No related tags found
No related merge requests found
from flask import Flask
from flask import request
from flask import render_template
import tempfile
import subprocess
import os
app = Flask(__name__)
......@@ -13,13 +17,38 @@ def main():
def unl2rdf():
if request.method == 'POST':
unl = request.form['unl']
rdf = convertUnl2Rdf(unl)
# Validate formats list
formats = list(set(['rdf', 'dot']) & set(request.form.getlist('outputs')))
output = convertUnl(unl, formats)
else:
unl = None
rdf = None
return render_template('unl2rdf.html', unl=unl, rdf=rdf)
output = None
return render_template('unl2rdf.html', unl=unl, output=output)
def convertUnl(unl, outputs):
with tempfile.NamedTemporaryFile() as temp:
out_name = temp.name
with tempfile.NamedTemporaryFile() as in_file:
in_file.write(unl)
unl2rdf_path = "/opt/UnlParser-1.0-SNAPSHOT.jar"
cmd = ['java', '-jar', unl2rdf_path, '--input-file', in_file.name,
'--output-filename', out_name, '--output-type', outputs]
subprocess.run(cmd)
res = {}
if 'rdf' in outputs:
fname = '{}.rdf'.format(out_name)
with open('{}.rdf'.format(out_name), 'r') as f:
res['rdf'] = f.read()
os.remove(fname)
if 'dot' in outputs:
fname = '{}.dot'.format(out_name)
cmd = ['dot', '-Tsvg', fname]
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
res['dot'] = proc.stdout.read()
os.remove(fname)
def convertUnl2Rdf(unl):
# TODO handle text here, do not call bash command or sanitize input first
return "Please implement me so I can convert '{}' to RDF".format(unl)
return res
......@@ -2,26 +2,39 @@
{% block title %}UNL 2 RDF demo{% endblock %}
{% block content %}
{% if not unl %}
<h1>Enter some UNL here I will convert it to RDF for you</h1>
<div class="form-group">
<form action="{{request.path}}" method="POST">
<h2>Enter some UNL here I will convert it for you</h2>
<textarea class="form-control" name="unl" cols="80" rows="10" style="width: 300px; height: 150px;"></textarea>
<h2>Tell me which output(s) you want</h2>
<ul>
<li><label><input type="checkbox" name="outputs" value="rdf" checked>RDF</label></li>
<li><label><input type="checkbox" name="outputs" value="dot" checked>Graph (dot)</label></li>
</ul>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>
{% else %}
<h2> UNL </h2>
<pre>
<code>
{{ unl }}
</code>
</pre>
<h2> RDF </h2>
<pre>
<code>
{{ rdf }}
</code>
</pre>
<h2> UNL </h2>
<pre>
<code>
{{ unl }}
</code>
</pre>
{% if output['dot'] %}
<h2> Unl graph </h2>
{{ output['dot'] }}
{% endif %}
{% if output['rdf'] %}
<h2> RDF </h2>
<pre>
<code>
{{ output['rdf'] }}
</code>
</pre>
{% endif %}
{% endif %}
{% endblock %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment