diff --git a/src/app/app.py b/src/app/app.py index 9eed382c0bd812b539c3a925b8e522aa0d582c52..42b953ebab6465ca80d213a161cea447c026f415 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 0cc815374bd1f5e0cde562c126bf2c4a83abe0c9..f09169d69a2c958ee6083c7b2ef26f02da076bb3 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 %}