diff --git a/config.py b/config.py new file mode 100644 index 0000000000000000000000000000000000000000..052374acc1676006c518d6a396ef50922ef284fc --- /dev/null +++ b/config.py @@ -0,0 +1 @@ +VERSION = "0.0.1" \ No newline at end of file diff --git a/doc2tosca.py b/doc2tosca.py index 46f3be717f77d0b4d2438410dbf5e0b713af42f3..9bb0f0a5305149ef03e7954505a0740dfb2a7bd9 100644 --- a/doc2tosca.py +++ b/doc2tosca.py @@ -1,9 +1,10 @@ -#!/bin/python3.5 +#!/bin/python3 ''' Generate tosca definitions from Docx specfication ''' import sys +import os import re from io import StringIO @@ -139,7 +140,7 @@ def generate_tables_between(a_id, b_id, content, buf): print(" Regex != 1 ") return definitions_count -def dump_header(model_name, buf, imports): +def dump_header(model_name, buf, imports=None): ''' Writes the header to the file for a specific model ''' @@ -155,27 +156,24 @@ for mn in MODEL_NAMES: MODELS[mn] = tosca_model_info(mn, '- etsi_nfv_sol001_common_types.yaml') -if __name__ == "__main__": - - try: - SOL001_FN = sys.argv[1] - except: - print('Error: Filename missing or filename not a docx document') - print('Usage: doc2tosca <docx-with-tosca-definitions>') - sys.exit(1) - - print( "Opening " + SOL001_FN) +def generate_templates(filename): + ''' + Takes a filename and loads the definition into the MODELS dictionary + ''' + if isinstance(filename, str): + print("Opening " + filename) - SOL001 = docx.Document(SOL001_FN) + sol_001 = docx.Document(filename) for m in MODELS: - dump_header(MODELS[m]['name'], MODELS[m]['buf'], MODELS[m]['imports']) + # MODELS[m]['imports'] + dump_header(MODELS[m]['name'], MODELS[m]['buf']) p_id = 0 cur_sect = "0" - CONTENT = get_content(SOL001) + CONTENT = get_content(sol_001) tables=0 while p_id < len(CONTENT): @@ -185,7 +183,7 @@ if __name__ == "__main__": p_id = p_id + 1 if p_id >= len(CONTENT): - print( "FOREWORD NOT FOUND") + print("FOREWORD NOT FOUND") sect_6_id = find_sect("6\tVNFD TOSCA model", p_id, CONTENT) @@ -204,9 +202,34 @@ if __name__ == "__main__": count = generate_tables_between(sect_8_id, annex_a_id, CONTENT, MODELS['pnfd']['buf']) print("Printed " + str(count) + " types to " + "PNFD\n\n\n") + +def print_to_files(prefix=None): + ''' + Prefix is a path to a folder to work into + ''' for m in MODELS: + + if prefix != None: + MODELS[m]['fn'] = os.path.join(prefix, MODELS[m]['fn']) + + print("Writing to " + MODELS[m]['fn']) MODELS[m]['fd'] = open(MODELS[m]['fn'], 'w') MODELS[m]['buf'].seek(0) MODELS[m]['fd'].write(MODELS[m]['buf'].read()) MODELS[m]['fd'].write('\n') MODELS[m]['fd'].close() + +if __name__ == "__main__": + + try: + SOL001_FN = sys.argv[1] + except: + print('Error: Filename missing or filename not a docx document') + print('Usage: doc2tosca <docx-with-tosca-definitions>') + sys.exit(1) + + generate_templates(SOL001_FN) + + print_to_files() + + diff --git a/templates/home.html b/templates/home.html index f3aa2619a9f71e2a77f2983185ff13f56d64da3d..3f4317fc98b130c8f94959066042cd705ad2f63b 100644 --- a/templates/home.html +++ b/templates/home.html @@ -24,10 +24,11 @@ {% endwith %} -<h1 style="text-align:center">TOSCA export</h1> +<h1 style="text-align:center">TOSCA import/export</h1> +<h5 style="text-align:center; margin-bottom: 50px">v {{ VERSION }}</h5> <div class="row"> - <div class="col-sm"> - <h2>tosca2doc</h2> + <div class="col-sm" > + <h2 style="text-align:center">tosca2doc</h2> <form action="/tosca2doc" method="post" enctype="multipart/form-data"> Tosca YAML files:<br /> <input type="file" name="file" multiple=""/><br /> @@ -36,7 +37,7 @@ </div> <div class="col-sm"> - <h2>doc2tosca</h2> + <h2 style="text-align:center" >doc2tosca</h2> <form action="/doc2tosca" method="post" enctype="multipart/form-data"> Docx file:<br /> <input type="file" name="file" /><br /> diff --git a/web_app.py b/web_app.py index e6593d3e6f7471452d4b32f42f770dfe3e651f47..0c8c6174a48a3234dc597399cf4a9f871ef46cdc 100644 --- a/web_app.py +++ b/web_app.py @@ -1,15 +1,22 @@ -#!/bin/bash +#!/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 tempfile +from zipfile import ZipFile +import os + +import config import tosca2doc +import doc2tosca app = Flask(__name__) + UPLOAD_FOLDER = '/tmp/upload' ALLOWED_EXTENSIONS = set(['txt', 'yaml', 'docx']) @@ -30,7 +37,7 @@ def allowed_file(filename): @app.route("/") def hello(): - return render_template("./home.html") + return render_template("./home.html", VERSION=config.VERSION) @app.route("/tosca2doc", methods=['POST']) def mk_tosca2doc(): @@ -78,4 +85,52 @@ def mk_tosca2doc(): doc.save('/tmp/myfile.docx') return send_file('/tmp/myfile.docx') - return ("No content found") \ No newline at end of file + #return ("No content found") + +def get_all_yaml_file_paths(directory): + + # initializing empty file paths list + file_paths = [] + + # crawling through directory and subdirectories + for root, directories, files in os.walk(directory): + for filename in files: + if ".yaml" in filename: + # join the two strings in order to form the full filepath. + filepath = os.path.join(root, filename) + file_paths.append(filepath) + print(filepath) + + # returning all file paths + return file_paths + +@app.route("/doc2tosca", methods=['POST']) +def mk_doc2tosca(): + + zip_fn = "tosca_defs.zip" + + if 'file' not in request.files: + flash('No file part') + return redirect(request.url) + + ufiles = request.files.getlist("file") + + # uploaded_file.filename + # uploaded_file.read() + sol001_file = ufiles[0] + + doc2tosca.generate_templates(sol001_file) + + tmp_dir = tempfile.mkdtemp() + print("TMP DIR: " + tmp_dir) + doc2tosca.print_to_files(tmp_dir) + + file_paths = get_all_yaml_file_paths(tmp_dir) + + zip_path = os.path.join(tmp_dir, zip_fn) + + with ZipFile( zip_path, 'w') as archive: + for myfile in file_paths: + archive.write(myfile, arcname=os.path.basename(myfile)) + + return send_file(zip_path, as_attachment=True)