Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
88
89
90
91
92
93
94
#!/bin/bash
import os
from flask import Flask, flash, request, redirect, url_for, send_file
from werkzeug.utils import secure_filename
import docx
import tosca2doc
app = Flask(__name__)
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'
HOME='''
<!doctype html>
<title>tosca2doc</title>
<h1>tosca2doc & doc2tosca</h1>
<h2>tosca2doc</h2>
<form action="/tosca2doc" method="post" enctype="multipart/form-data">
Tosca YAML file:
<input type="file" name="file" multiple=""/>
<input type="submit" value="Upload" />
</form>
<h2>doc2tosca</h2>
'''
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 HOME
@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')
return ("No content found")