Loading src/doc2tosca.py +10 −7 Original line number Diff line number Diff line Loading @@ -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" Loading @@ -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} Loading Loading @@ -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'): Loading @@ -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): Loading Loading @@ -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) Loading @@ -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 Loading src/tosca2doc.py +3 −3 Original line number Diff line number Diff line Loading @@ -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 = [ Loading src/web_app.py +25 −11 Original line number Diff line number Diff line #!/bin/python3 import os from flask import Flask, flash, request, redirect, url_for, send_file, render_template from werkzeug.utils import secure_filename import docx import shutil import tempfile from zipfile import ZipFile import os from flask import Flask, flash, request, redirect, send_file, render_template, g from werkzeug.utils import secure_filename import docx import config import tosca2doc Loading Loading @@ -37,6 +36,9 @@ def allowed_file(filename): @app.route("/") def hello(): ''' Render home page ''' return render_template("./home.html", VERSION=config.VERSION) @app.route("/tosca2doc", methods=['POST']) Loading Loading @@ -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: Loading @@ -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") Loading @@ -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(): Loading @@ -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) Loading @@ -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__': Loading Loading
src/doc2tosca.py +10 −7 Original line number Diff line number Diff line Loading @@ -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" Loading @@ -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} Loading Loading @@ -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'): Loading @@ -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): Loading Loading @@ -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) Loading @@ -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 Loading
src/tosca2doc.py +3 −3 Original line number Diff line number Diff line Loading @@ -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 = [ Loading
src/web_app.py +25 −11 Original line number Diff line number Diff line #!/bin/python3 import os from flask import Flask, flash, request, redirect, url_for, send_file, render_template from werkzeug.utils import secure_filename import docx import shutil import tempfile from zipfile import ZipFile import os from flask import Flask, flash, request, redirect, send_file, render_template, g from werkzeug.utils import secure_filename import docx import config import tosca2doc Loading Loading @@ -37,6 +36,9 @@ def allowed_file(filename): @app.route("/") def hello(): ''' Render home page ''' return render_template("./home.html", VERSION=config.VERSION) @app.route("/tosca2doc", methods=['POST']) Loading Loading @@ -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: Loading @@ -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") Loading @@ -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(): Loading @@ -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) Loading @@ -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__': Loading