From 893876bae3bdf9edd00b814d5802efdd259162c8 Mon Sep 17 00:00:00 2001 From: David Beniamine <david.beniamine@tetras-libre.fr> Date: Tue, 5 May 2020 17:25:31 +0200 Subject: [PATCH] WIP : call jar parser --- src/app/app.py | 41 +++++++++++++++++++++++++++++----- src/app/templates/unl2rdf.html | 39 +++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/app/app.py b/src/app/app.py index 70ed396..5b2e6b2 100644 --- a/src/app/app.py +++ b/src/app/app.py @@ -1,6 +1,10 @@ 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 diff --git a/src/app/templates/unl2rdf.html b/src/app/templates/unl2rdf.html index d6aa2a1..0cc8153 100644 --- a/src/app/templates/unl2rdf.html +++ b/src/app/templates/unl2rdf.html @@ -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 %} -- GitLab