Newer
Older
from flask import Flask, flash, request, redirect, url_for, send_file, render_template
UPLOAD_FOLDER = '/tmp/upload'
ALLOWED_EXTENSIONS = set(['txt', 'yaml', 'docx'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'
TOSCA_DEFS=['VNFD', 'NSD', 'PNFD']
def allowed_file(filename):
'''
Check filename is in the list of allowed extensions
'''
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/")
def hello():
return render_template("./home.html", VERSION=config.VERSION)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@app.route("/tosca2doc", methods=['POST'])
def mk_tosca2doc():
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
ufiles=request.files.getlist("file")
for tosca_def in TOSCA_DEFS:
found=False
for uploaded_file in ufiles:
if tosca_def in uploaded_file.filename or \
tosca_def.lower() in uploaded_file.filename:
found=True
#if uploaded_file:
# print "-------------------"
# print uploaded_file.read()
# print "-------------------"
if not found:
flash('No file part')
print('No file part')
return redirect("/")
for file in ufiles:
if file.filename == '':
print('No selected file')
flash('No selected file')
return redirect("/")
doc = docx.Document()
for tosca_def in TOSCA_DEFS:
for file in ufiles:
if tosca_def in uploaded_file.filename or \
tosca_def.lower() in uploaded_file.filename:
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
file.close()
tosca2doc.generate_from_file(tosca_def, doc, filepath)
doc.save('/tmp/myfile.docx')
return send_file('/tmp/myfile.docx')
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#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)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')