From c42a08cdfc030f4b0235298651566672c2ffbadd Mon Sep 17 00:00:00 2001 From: David Beniamine <david.beniamine@tetras-libre.fr> Date: Fri, 8 May 2020 19:14:55 +0200 Subject: [PATCH] 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 --- src/app/app.py | 51 ++++++++++++++++++++++++---------- src/app/templates/unl2rdf.html | 23 +++++++++------ 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/src/app/app.py b/src/app/app.py index 9eed382..42b953e 100644 --- a/src/app/app.py +++ b/src/app/app.py @@ -4,7 +4,7 @@ from flask import Flask from flask import request from flask import render_template import tempfile -import subprocess +from subprocess import Popen, PIPE, STDOUT import os app = Flask(__name__) @@ -29,28 +29,51 @@ def unl2rdf(): def convertUnl(unl, outputs): + res = {} + + # Get a temporary filename 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: - in_file.write(unl) - unl2rdf_path = "/opt/unl2rdf.jar" - cmd = ['java', '-jar', unl2rdf_path, '--input-file', in_file.name, - '--output-file', out_name, '--output-type', ','.join(outputs)] - subprocess.run(cmd) + # Remove CRLF and flush output to avoid java errors + in_file.write(unl.replace("\r\n", "\n")) + in_file.flush() - 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: - fname = '{}.rdf'.format(out_name) + fname = '{}/{}.rdf'.format(out_dir, out_name) with open(fname, 'r') as f: res['rdf'] = f.read() - #os.remove(fname) + try: + os.remove(fname) + except FileNotFoundError: + pass + # Handle dot output if any if 'dot' in outputs: - fname = '{}.dot'.format(out_name) + fname = '{}/{}.dot'.format(out_dir, out_name) cmd = ['dot', '-Tsvg', fname] - with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: - res['dot'] = proc.stdout.read() - #os.remove(fname) + with Popen(cmd, stdout=PIPE, stderr=PIPE) as p: + p.wait() + 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 diff --git a/src/app/templates/unl2rdf.html b/src/app/templates/unl2rdf.html index 0cc8153..f09169d 100644 --- a/src/app/templates/unl2rdf.html +++ b/src/app/templates/unl2rdf.html @@ -8,23 +8,23 @@ <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="rdf">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> + {% if output['error'] %} + <h2> Error </h2> + <pre> + {{ output['error'] }} + </pre> + {% endif %} {% if output['dot'] %} <h2> Unl graph </h2> - {{ output['dot'] }} + {{ output['dot'] | safe }} {% endif %} {% if output['rdf'] %} @@ -36,5 +36,12 @@ </pre> {% endif %} + <h2> UNL </h2> + <pre> + <code> + {{ unl }} + </code> + </pre> + {% endif %} {% endblock %} -- GitLab