Commit 70837d2e authored by carignani's avatar carignani
Browse files

doc2tosca supported in webapp

parent dc7f185f
Loading
Loading
Loading
Loading

config.py

0 → 100644
+1 −0
Original line number Original line Diff line number Diff line
VERSION = "0.0.1"
 No newline at end of file
+39 −16
Original line number Original line Diff line number Diff line
#!/bin/python3.5
#!/bin/python3
'''
'''
Generate tosca definitions from Docx specfication
Generate tosca definitions from Docx specfication
'''
'''


import sys
import sys
import os
import re
import re
from io import StringIO
from io import StringIO


@@ -139,7 +140,7 @@ def generate_tables_between(a_id, b_id, content, buf):
                print("       Regex != 1 ")
                print("       Regex != 1 ")
    return definitions_count
    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
    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')
    MODELS[mn] = tosca_model_info(mn, '- etsi_nfv_sol001_common_types.yaml')




if __name__ == "__main__":
def generate_templates(filename):

    '''
    try:
    Takes a filename and loads the definition into the MODELS dictionary
        SOL001_FN = sys.argv[1]
    '''
    except:
    if isinstance(filename, str):
        print('Error: Filename missing or filename not a docx document')
        print("Opening " + filename)
        print('Usage: doc2tosca <docx-with-tosca-definitions>')
        sys.exit(1)

    print( "Opening " + SOL001_FN)


    SOL001 = docx.Document(SOL001_FN)
    sol_001 = docx.Document(filename)


    for m in MODELS:
    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
    p_id = 0


    cur_sect = "0"
    cur_sect = "0"


    CONTENT = get_content(SOL001)
    CONTENT = get_content(sol_001)
    tables=0
    tables=0


    while p_id < len(CONTENT):
    while p_id < len(CONTENT):
@@ -204,9 +202,34 @@ if __name__ == "__main__":
    count = generate_tables_between(sect_8_id, annex_a_id, CONTENT, MODELS['pnfd']['buf'])
    count = generate_tables_between(sect_8_id, annex_a_id, CONTENT, MODELS['pnfd']['buf'])
    print("Printed " + str(count) + " types to " + "PNFD\n\n\n")
    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:
    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]['fd'] = open(MODELS[m]['fn'], 'w')
        MODELS[m]['buf'].seek(0)
        MODELS[m]['buf'].seek(0)
        MODELS[m]['fd'].write(MODELS[m]['buf'].read())
        MODELS[m]['fd'].write(MODELS[m]['buf'].read())
        MODELS[m]['fd'].write('\n')
        MODELS[m]['fd'].write('\n')
        MODELS[m]['fd'].close()
        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()

+5 −4
Original line number Original line Diff line number Diff line
@@ -24,10 +24,11 @@
{% endwith %}
{% 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="row">
	<div class="col-sm" >
	<div class="col-sm" >
		<h2>tosca2doc</h2>
		<h2 style="text-align:center">tosca2doc</h2>
		<form action="/tosca2doc" method="post" enctype="multipart/form-data">
		<form action="/tosca2doc" method="post" enctype="multipart/form-data">
			Tosca YAML files:<br />
			Tosca YAML files:<br />
			<input type="file" name="file" multiple=""/><br />
			<input type="file" name="file" multiple=""/><br />
@@ -36,7 +37,7 @@
	</div>
	</div>


	<div class="col-sm">
	<div class="col-sm">
		<h2>doc2tosca</h2>
		<h2 style="text-align:center"	>doc2tosca</h2>
			<form action="/doc2tosca" method="post" enctype="multipart/form-data">
			<form action="/doc2tosca" method="post" enctype="multipart/form-data">
			Docx file:<br />
			Docx file:<br />
			<input type="file" name="file" /><br />
			<input type="file" name="file" /><br />
+58 −3
Original line number Original line Diff line number Diff line
#!/bin/bash
#!/bin/python3


import os
import os


from flask import Flask, flash, request, redirect, url_for, send_file, render_template
from flask import Flask, flash, request, redirect, url_for, send_file, render_template
from werkzeug.utils import secure_filename
from werkzeug.utils import secure_filename
import docx
import docx
import tempfile
from zipfile import ZipFile 


import os

import config
import tosca2doc
import tosca2doc
import doc2tosca


app = Flask(__name__)
app = Flask(__name__)



UPLOAD_FOLDER = '/tmp/upload'
UPLOAD_FOLDER = '/tmp/upload'


ALLOWED_EXTENSIONS = set(['txt', 'yaml', 'docx'])
ALLOWED_EXTENSIONS = set(['txt', 'yaml', 'docx'])
@@ -30,7 +37,7 @@ def allowed_file(filename):


@app.route("/")
@app.route("/")
def hello():
def hello():
    return render_template("./home.html") 
    return render_template("./home.html", VERSION=config.VERSION) 


@app.route("/tosca2doc", methods=['POST'])
@app.route("/tosca2doc", methods=['POST'])
def mk_tosca2doc():
def mk_tosca2doc():
@@ -78,4 +85,52 @@ def mk_tosca2doc():
    doc.save('/tmp/myfile.docx')
    doc.save('/tmp/myfile.docx')
    return send_file('/tmp/myfile.docx')
    return send_file('/tmp/myfile.docx')


    return ("No content found")
    #return ("No content found")
 No newline at end of file

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)