Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tosca2doc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CTI Tools
tosca2doc
Commits
70837d2e
Commit
70837d2e
authored
6 years ago
by
carignani
Browse files
Options
Downloads
Patches
Plain Diff
doc2tosca supported in webapp
parent
dc7f185f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
config.py
+1
-0
1 addition, 0 deletions
config.py
doc2tosca.py
+39
-16
39 additions, 16 deletions
doc2tosca.py
templates/home.html
+5
-4
5 additions, 4 deletions
templates/home.html
web_app.py
+58
-3
58 additions, 3 deletions
web_app.py
with
103 additions
and
23 deletions
config.py
0 → 100644
+
1
−
0
View file @
70837d2e
VERSION
=
"
0.0.1
"
\ No newline at end of file
This diff is collapsed.
Click to expand it.
doc2tosca.py
+
39
−
16
View file @
70837d2e
#!/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
)
SOL
001
=
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
(
SOL
001
)
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
\t
VNFD 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
()
This diff is collapsed.
Click to expand it.
templates/home.html
+
5
−
4
View file @
70837d2e
...
...
@@ -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
/>
...
...
This diff is collapsed.
Click to expand it.
web_app.py
+
58
−
3
View file @
70837d2e
#!/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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment