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
00ac479b
Commit
00ac479b
authored
4 years ago
by
carignani
Browse files
Options
Downloads
Patches
Plain Diff
add example module
parent
b0213f02
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/example.py
+99
-0
99 additions, 0 deletions
src/example.py
with
99 additions
and
0 deletions
src/example.py
0 → 100644
+
99
−
0
View file @
00ac479b
#!/bin/python3
'''
Class and utilities to parse and export examples of data models in TOSCA
'''
import
re
import
traceback
from
docx.text.paragraph
import
Paragraph
class
Example
():
def
__init__
(
self
,
filename
,
text
):
self
.
filename
=
filename
self
.
text
=
text
def
is_start_of_example
(
line
:
str
):
'''
Returns true if the line marks the start of an examples.
Check if lines starts with
"
tosca_definitions_version:
"
'''
if
not
isinstance
(
line
,
str
):
raise
ValueError
(
"
NOT A STRING
"
)
return
line
.
startswith
(
"
tosca_definitions_version:
"
)
def
is_body_of_example
(
line
:
str
):
'''
Returns true if the line is part of the body of an example.
'''
return
line
.
startswith
(
"
imports:
"
)
or
\
line
.
startswith
(
"
node_types:
"
)
or
\
line
.
startswith
(
"
topology_template:
"
)
or
\
line
.
startswith
(
"
description:
"
)
or
\
line
.
startswith
(
"
data_types:
"
)
or
\
line
.
startswith
(
"
"
)
or
\
line
.
startswith
(
"
"
)
or
\
line
==
""
def
get_example_file_name
(
line
:
str
):
'''
'''
matches
=
re
.
search
(
r
'
[a-zA-Z0-9_.]*.yaml
'
,
line
)
if
matches
is
not
None
:
return
matches
.
group
(
0
)
return
""
def
parse_all_examples
(
txt
):
'''
Parses TOSCA examples. Txt is a list of Docx items (Paragraph, etc.).
Returns a list of Example objects
'''
res
=
[]
new_example
=
""
filename
=
""
i
=
1
clause
=
""
for
line
in
txt
:
if
isinstance
(
line
,
Paragraph
):
linetext
=
str
(
line
.
text
)
if
"
Heading
"
in
line
.
style
.
name
:
clause
=
linetext
.
split
(
"
\t
"
)[
0
]
i
=
1
elif
isinstance
(
line
,
str
):
linetext
=
line
else
:
continue
if
is_start_of_example
(
linetext
):
filename
=
get_example_file_name
(
previous_line
)
if
filename
==
""
:
filename
=
"
{:02d}
"
.
format
(
i
)
+
"
.yaml
"
i
=
i
+
1
filename
=
clause
+
"
-
"
+
filename
new_example
=
"
#
"
+
filename
+
"
\n
"
+
linetext
elif
new_example
!=
""
and
is_body_of_example
(
linetext
):
new_example
=
new_example
+
"
\n
"
+
linetext
elif
len
(
new_example
)
>
0
:
res
.
append
(
Example
(
filename
,
new_example
))
new_example
=
""
previous_line
=
linetext
return
res
def
generate_examples_between
(
a_id
,
b_id
,
content
,
EXAMPLES
):
try
:
examples
=
parse_all_examples
(
content
[
a_id
:
b_id
])
except
:
track
=
traceback
.
format_exc
()
print
(
track
)
return
0
for
example
in
examples
:
EXAMPLES
[
example
.
filename
]
=
example
return
len
(
examples
)
\ No newline at end of file
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