diff --git a/src/doc2tosca.py b/src/doc2tosca.py index 9bb0f0a5305149ef03e7954505a0740dfb2a7bd9..070c1fa7e048c39acb8b7b9349517d8a5b82e3b4 100644 --- a/src/doc2tosca.py +++ b/src/doc2tosca.py @@ -12,7 +12,7 @@ import docx from docx.table import Table from docx.text.paragraph import Paragraph -BASE_FILENAME = "try-tosca-export_{}.yaml" +BASE_FILENAME = "generated_etsi_nfv_sol001_{}_types.yaml" TOSCA_VERSION = "tosca_simple_yaml_1_2" SPEC_VERSION = "2.6.1" @@ -21,7 +21,7 @@ MODEL_NAMES = ['vnfd', 'nsd', 'pnfd', 'common'] HDR = '''tosca_definitions_version: {tosca_version} description: ETSI NFV SOL 001 {model} types definitions version {spec_version} metadata: - - template_name: {model} + - template_name: etsi_nfv_sol001_{model}_types - template_name: ETSI_NFV - template_version: {spec_version} @@ -108,7 +108,7 @@ def write_table_to_file(tab, buf): return " " + txt txt = tab.rows[0].cells[0].text - # print("+++++ Included in: " + tab.rows[0].cells[0].text.split("\n")[0]) + # print("# Included in: " + tab.rows[0].cells[0].text.split("\n")[0]) buf.write("\n".join([pad2(x) for x in txt.split("\n")])) # buf.write('\n# -------------------- #\n') if not txt.endswith('\n'): @@ -121,6 +121,7 @@ def generate_tables_between(a_id, b_id, content, buf): fdesc file. Returns the number of written definitions ''' definitions_count = 0 + for idx in range(a_id, b_id): tmp_elem = content[idx] if isinstance(tmp_elem, Table) and is_tosca_def(tmp_elem): @@ -153,12 +154,15 @@ def dump_header(model_name, buf, imports=None): MODELS = {} for mn in MODEL_NAMES: - MODELS[mn] = tosca_model_info(mn, '- etsi_nfv_sol001_common_types.yaml') + MODELS[mn] = tosca_model_info( + mn, + '- https://forge.etsi.org/rep/nfv/SOL001/raw/v{}/etsi_nfv_sol001_common_types.yaml'.format(SPEC_VERSION) + ) def generate_templates(filename): ''' - Takes a filename and loads the definition into the MODELS dictionary + Takes a filename or file object and loads the definition into the MODELS dictionary ''' if isinstance(filename, str): print("Opening " + filename) @@ -166,8 +170,7 @@ def generate_templates(filename): sol_001 = docx.Document(filename) for m in MODELS: - # MODELS[m]['imports'] - dump_header(MODELS[m]['name'], MODELS[m]['buf']) + dump_header(MODELS[m]['name'], MODELS[m]['buf'], MODELS[m]['imports']) p_id = 0 diff --git a/src/tosca2doc.py b/src/tosca2doc.py index d176ac13c59748b5b1453d452811fa95ef87c990..c5f83f21cb7ab0cbf95d0688668bc24df901b252 100755 --- a/src/tosca2doc.py +++ b/src/tosca2doc.py @@ -14,9 +14,9 @@ PRINT_TRESHOLD = 1 TEMPLATES = {} FNS = { - 'VNFD' : 'etsi_nfv_sol001_vnfd_2_5_1_types.yaml', - 'NSD' : 'etsi_nfv_sol001_nsd_2_5_1_types.yaml', - 'PNFD' : 'etsi_nfv_sol001_pnfd_2_5_1_types.yaml' + 'VNFD' : 'etsi_nfv_sol001_vnfd_types.yaml', + 'NSD' : 'etsi_nfv_sol001_nsd_types.yaml', + 'PNFD' : 'etsi_nfv_sol001_pnfd_types.yaml' } TOSCA_TYPES = [ diff --git a/src/web_app.py b/src/web_app.py index 27be46609a63baa4fe74fcf794df6e8778b30e4e..cdec5613bf29a13ff58f130d1dd9916643817e31 100644 --- a/src/web_app.py +++ b/src/web_app.py @@ -1,14 +1,13 @@ #!/bin/python3 import os +import shutil +import tempfile +from zipfile import ZipFile -from flask import Flask, flash, request, redirect, url_for, send_file, render_template +from flask import Flask, flash, request, redirect, send_file, render_template, g from werkzeug.utils import secure_filename import docx -import tempfile -from zipfile import ZipFile - -import os import config import tosca2doc @@ -25,7 +24,7 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.secret_key = 'super secret key' app.config['SESSION_TYPE'] = 'filesystem' -TOSCA_DEFS=['VNFD', 'NSD', 'PNFD'] +TOSCA_DEFS = ['VNFD', 'NSD', 'PNFD'] def allowed_file(filename): ''' @@ -37,7 +36,10 @@ def allowed_file(filename): @app.route("/") def hello(): - return render_template("./home.html", VERSION=config.VERSION) + ''' + Render home page + ''' + return render_template("./home.html", VERSION=config.VERSION) @app.route("/tosca2doc", methods=['POST']) def mk_tosca2doc(): @@ -69,6 +71,9 @@ def mk_tosca2doc(): flash('No selected file') return redirect("/") + tmp_dir = tempfile.mkdtemp() + g.tmp_dir = tmp_dir + doc = docx.Document() for tosca_def in TOSCA_DEFS: @@ -82,8 +87,10 @@ def mk_tosca2doc(): tosca2doc.generate_from_file(tosca_def, doc, filepath) - doc.save('/tmp/myfile.docx') - return send_file('/tmp/myfile.docx') + outfilename = os.path.join(tmp_dir, 'myfile.docx') + + doc.save(outfilename) + return send_file(outfilename) #return ("No content found") @@ -104,6 +111,14 @@ def get_all_yaml_file_paths(directory): # returning all file paths return file_paths +@app.after_request +def after_request(response): + if request.path != '/doc2tosca': + return response + shutil.rmtree(g.tmp_dir, ignore_errors=True) + print("Deleted {}\n\n".format(g.tmp_dir)) + return response + @app.route("/doc2tosca", methods=['POST']) def mk_doc2tosca(): @@ -115,8 +130,6 @@ def mk_doc2tosca(): ufiles = request.files.getlist("file") - # uploaded_file.filename - # uploaded_file.read() sol001_file = ufiles[0] doc2tosca.generate_templates(sol001_file) @@ -133,6 +146,7 @@ def mk_doc2tosca(): for myfile in file_paths: archive.write(myfile, arcname=os.path.basename(myfile)) + g.tmp_dir = tmp_dir return send_file(zip_path, as_attachment=True) if __name__ == '__main__':