From 8374ed65923a523b5c48823b605dae1e81c96a3b Mon Sep 17 00:00:00 2001 From: Michele Carignani <michele.carignani@etsi.org> Date: Mon, 17 Jun 2019 14:19:47 +0200 Subject: [PATCH] custom version of YAML files for doc2tosca enabled --- src/config.py | 2 +- src/doc2tosca.py | 12 ++++++++---- src/templates/home.html | 5 ++++- src/test_after_request.py | 33 +++++++++++++++++++++++++++++++++ src/web_app.py | 12 +++++++++++- 5 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 src/test_after_request.py diff --git a/src/config.py b/src/config.py index bc4ffb3..4d38941 100644 --- a/src/config.py +++ b/src/config.py @@ -1 +1 @@ -VERSION = "0.0.2" +VERSION = "0.0.3" diff --git a/src/doc2tosca.py b/src/doc2tosca.py index cd061f2..1322b35 100644 --- a/src/doc2tosca.py +++ b/src/doc2tosca.py @@ -14,9 +14,9 @@ from docx.text.paragraph import Paragraph BASE_FILENAME = "generated_etsi_nfv_sol001_{}_types.yaml" TOSCA_VERSION = "tosca_simple_yaml_1_2" -SPEC_VERSION = "2.6.1" +SPEC_VERSION = "v2.6.1" -allowed_versions = ["2.6.1","2.7.1"] +allowed_versions = ["v2.6.1","v2.6.3", "v2.7.1"] MODEL_NAMES = ['vnfd', 'nsd', 'pnfd', 'common'] @@ -165,10 +165,14 @@ def generate_templates(filename, spec_version=SPEC_VERSION): for mn in MODEL_NAMES: MODELS[mn] = tosca_model_info( mn, - '- https://forge.etsi.org/rep/nfv/SOL001/raw/v{}/etsi_nfv_sol001_common_types.yaml'.format(spec_version) + '- https://forge.etsi.org/rep/nfv/SOL001/raw/{}/etsi_nfv_sol001_common_types.yaml'.format(spec_version) ) - sol_001 = docx.Document(filename) + try: + sol_001 = docx.Document(filename) + except: + print("Error opening the submitted Docx file") + raise ValueError("Cannot open the submitted Docx file") for m in MODELS: dump_header( diff --git a/src/templates/home.html b/src/templates/home.html index 4c3563e..294e280 100644 --- a/src/templates/home.html +++ b/src/templates/home.html @@ -1,4 +1,4 @@ -<!doctype html> +<!DOCTYPE HTML> <html> @@ -83,6 +83,9 @@ For any other inquiry, contact <a href="mailto:cti_support@etsi.org">ETSI CTI</a <option value="{{version}}">{{version}}</option> {% endfor %} </select> + <br /> + Custom version (overrides the selector): + <input type="text" name="custom-doc-version"> <br /><br /> <center> <input type="submit" class="btn btn-primary" value="Upload" /> diff --git a/src/test_after_request.py b/src/test_after_request.py new file mode 100644 index 0000000..aefb24d --- /dev/null +++ b/src/test_after_request.py @@ -0,0 +1,33 @@ +#!/bin/python3 +''' +just a test +''' + +import os +from flask import Flask, flash, request, send_file, g + +app = Flask(__name__) + +@app.after_request +def after_request(response): + if request.path != '/ciao': + return response + os.remove(g.fname) + print("Deleted {}\n\n".format(g.fname)) + return response + +@app.route("/") +def hello(): + ''' + noting + ''' + return 'Hello world' + +@app.route("/ciao") +def ciao(): + fname='/tmp/MIOMIOMIO.txt' + myfile=open(fname, 'w') + myfile.write('PROVA\n'* 100) + myfile.close() + g.fname = fname + return send_file(fname, as_attachment=True) diff --git a/src/web_app.py b/src/web_app.py index 1bc36a4..60c993e 100644 --- a/src/web_app.py +++ b/src/web_app.py @@ -173,6 +173,7 @@ def mk_doc2tosca(): try: print(request.form) doc_version = request.form.get('doc-version') + custom_doc_version = request.form.get('custom-doc-version') except: flash("Something went wrong :/") return redirect("/tosca-ie") @@ -183,7 +184,16 @@ def mk_doc2tosca(): sol001_file = ufiles[0] - doc2tosca.generate_templates(sol001_file, doc_version) + if custom_doc_version != "": + doc_version = custom_doc_version + try: + doc2tosca.generate_templates(sol001_file, doc_version) + except ValueError as e: + flash(str(e)) + return redirect("/tosca-ie") + except: + flash("Unknown error in the generation of the files. Please contact the support.") + return redirect("/tosca-ie") tmp_dir = tempfile.mkdtemp() print("TMP DIR: " + tmp_dir) -- GitLab