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

First working version for dot

+ Fix inputfile containing CRLF
+ Fix Bad arguments for jar
+ Fix handle and print java errors
+ Fix failing on temporary file removal
+ Fix svg was escaped
+ TODO RDF still not tested
parent 84ee6867
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ from flask import Flask ...@@ -4,7 +4,7 @@ from flask import Flask
from flask import request from flask import request
from flask import render_template from flask import render_template
import tempfile import tempfile
import subprocess from subprocess import Popen, PIPE, STDOUT
import os import os
app = Flask(__name__) app = Flask(__name__)
...@@ -29,28 +29,51 @@ def unl2rdf(): ...@@ -29,28 +29,51 @@ def unl2rdf():
def convertUnl(unl, outputs): def convertUnl(unl, outputs):
res = {}
# Get a temporary filename
with tempfile.NamedTemporaryFile() as temp: with tempfile.NamedTemporaryFile() as temp:
out_name = temp.name out_name = os.path.basename(temp.name)
out_dir = os.path.dirname(temp.name)
with tempfile.NamedTemporaryFile(mode="w") as in_file: with tempfile.NamedTemporaryFile(mode="w") as in_file:
in_file.write(unl) # Remove CRLF and flush output to avoid java errors
unl2rdf_path = "/opt/unl2rdf.jar" in_file.write(unl.replace("\r\n", "\n"))
cmd = ['java', '-jar', unl2rdf_path, '--input-file', in_file.name, in_file.flush()
'--output-file', out_name, '--output-type', ','.join(outputs)]
subprocess.run(cmd)
res = {} # Run java parser
cmd = ['java', '-jar', '/opt/unl2rdf.jar',
'--input-file', in_file.name,
'--output-Dir', out_dir, '--output-file', out_name,
'--output-type', ','.join(outputs)]
with Popen(cmd, stdout=PIPE, stderr=STDOUT) as p:
p.wait()
if p.returncode != 0:
res['error'] = "unl2rdf: \n\n"+p.stdout.read().decode()
# Handle rdf output if any
if 'rdf' in outputs: if 'rdf' in outputs:
fname = '{}.rdf'.format(out_name) fname = '{}/{}.rdf'.format(out_dir, out_name)
with open(fname, 'r') as f: with open(fname, 'r') as f:
res['rdf'] = f.read() res['rdf'] = f.read()
#os.remove(fname) try:
os.remove(fname)
except FileNotFoundError:
pass
# Handle dot output if any
if 'dot' in outputs: if 'dot' in outputs:
fname = '{}.dot'.format(out_name) fname = '{}/{}.dot'.format(out_dir, out_name)
cmd = ['dot', '-Tsvg', fname] cmd = ['dot', '-Tsvg', fname]
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: with Popen(cmd, stdout=PIPE, stderr=PIPE) as p:
res['dot'] = proc.stdout.read() p.wait()
#os.remove(fname) if p.returncode != 0:
res['error'] += "Dot: \n\n"+p.stderr.read().decode()
else:
res['dot'] = p.stdout.read().decode()
try:
os.remove(fname)
except FileNotFoundError:
pass
return res return res
...@@ -8,23 +8,23 @@ ...@@ -8,23 +8,23 @@
<textarea class="form-control" name="unl" cols="80" rows="10" style="width: 300px; height: 150px;"></textarea> <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> <h2>Tell me which output(s) you want</h2>
<ul> <ul>
<li><label><input type="checkbox" name="outputs" value="rdf" checked>RDF</label></li> <li><label><input type="checkbox" name="outputs" value="rdf">RDF</label></li>
<li><label><input type="checkbox" name="outputs" value="dot" checked>Graph (dot)</label></li> <li><label><input type="checkbox" name="outputs" value="dot" checked>Graph (dot)</label></li>
</ul> </ul>
<button class="btn btn-primary" type="submit">Submit</button> <button class="btn btn-primary" type="submit">Submit</button>
</form> </form>
</div> </div>
{% else %} {% else %}
<h2> UNL </h2> {% if output['error'] %}
<h2> Error </h2>
<pre> <pre>
<code> {{ output['error'] }}
{{ unl }}
</code>
</pre> </pre>
{% endif %}
{% if output['dot'] %} {% if output['dot'] %}
<h2> Unl graph </h2> <h2> Unl graph </h2>
{{ output['dot'] }} {{ output['dot'] | safe }}
{% endif %} {% endif %}
{% if output['rdf'] %} {% if output['rdf'] %}
...@@ -36,5 +36,12 @@ ...@@ -36,5 +36,12 @@
</pre> </pre>
{% endif %} {% endif %}
<h2> UNL </h2>
<pre>
<code>
{{ unl }}
</code>
</pre>
{% endif %} {% endif %}
{% endblock %} {% endblock %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment