Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
BornAgain
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mlz
BornAgain
Commits
8eb62ad6
Commit
8eb62ad6
authored
9 years ago
by
Pospelov, Gennady
Browse files
Options
Downloads
Patches
Plain Diff
Testing class for docstring.
parent
2b47c568
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dev-tools/python-bindings/a.txt
+31164
-0
31164 additions, 0 deletions
dev-tools/python-bindings/a.txt
dev-tools/python-bindings/test_docstring.py
+154
-0
154 additions, 0 deletions
dev-tools/python-bindings/test_docstring.py
with
31318 additions
and
0 deletions
dev-tools/python-bindings/a.txt
0 → 100644
+
31164
−
0
View file @
8eb62ad6
This diff is collapsed.
Click to expand it.
dev-tools/python-bindings/test_docstring.py
0 → 100644
+
154
−
0
View file @
8eb62ad6
class
DocObject
:
"""
The DocObject class holds continuous block of raw C++/Doxygen documentation strings related to some declaration.
This information is taken from BornAgain source file and later used to generate meaningful Python docstring.
:Example:
For the constructor of FormFactorCone, member self.doc_lines will contain following lines.
"
//! @brief Cone constructor
"
"
//! @param radius of Cone
'
s base
"
"
//! @param height of Cone
"
"
//! @param angle in radians between base and facet
"
while self.start_line, self.end_line will contain line numbers (starting from 0) of this block in the source file.
"""
def
__init__
(
self
):
self
.
start_line
=
0
self
.
end_line
=
0
self
.
doc_lines
=
[]
self
.
decl_string
=
""
def
__len__
(
self
):
return
len
(
self
.
doc_lines
)
def
addLine
(
self
,
line
,
line_number
):
line
=
line
.
replace
(
"
//!
"
,
""
).
strip
()
self
.
doc_lines
.
append
(
line
)
if
not
self
.
start_line
:
self
.
start_line
=
line_number
self
.
end_line
=
line_number
def
setDeclString
(
self
,
text
):
"""
Sets declaration string. This is done from master class using information provided by pygccxml machinery.
Given string will be used later on to find out types of variables during python docstring generation
"""
self
.
decl_string
=
text
def
getBriefDescription
(
self
):
"""
Parse @brief description from a multi line doxygen block
"""
brief
=
""
for
line
in
self
.
doc_lines
:
brief
+=
line
if
brief
[
len
(
brief
)
-
1
]
!=
'
'
:
brief
+=
'
'
if
"
@brief
"
in
brief
:
brief
=
brief
.
split
(
"
@brief
"
)[
1
].
strip
()
pos
=
brief
.
find
(
'
.
'
)
if
pos
<
0
:
pos
=
brief
.
find
(
'
@
'
)
brief
=
brief
[:
pos
].
strip
()
+
"
.
"
else
:
if
len
(
self
.
doc_lines
)
==
1
:
brief
=
self
.
doc_lines
[
0
]
if
not
brief
[
len
(
brief
)
-
1
]
==
"
.
"
:
brief
+=
"
.
"
return
brief
def
getParamList
(
self
):
result
=
[]
for
line
in
self
.
doc_lines
:
if
"
@param
"
in
line
:
result
.
append
(
line
.
replace
(
"
@param
"
,
""
).
strip
())
return
result
def
getPythonDocString
(
self
):
"""
Constructs and returns Python doc string basing on gathered Doxygen comments.
:Example:
For Cone constructor from example above it will be the following text:
"
Cone constructor
\n
"
"
\n
"
"
:Parameters:
\n
"
"
- radius (float) - of Cone
'
s base
\n
"
"
- height (float) - of Cone
'
s base
\n
"
"
- angle (float) - in radians between base and facet
\n
"
"""
brief
=
self
.
getBriefDescription
()
params
=
self
.
getParamList
()
eol
=
"
\n
"
result
=
""
if
len
(
brief
):
result
+=
brief
+
eol
+
eol
if
len
(
params
):
result
+=
"
:Parameters:
"
+
eol
for
p
in
params
:
param_name
=
p
.
split
()[
0
]
param_description
=
p
[
len
(
param_name
):].
strip
()
result
+=
"
-
\'
"
+
param_name
+
"
\'
-
"
+
param_description
+
eol
return
result
class
DoxygenDocExtractor
:
"""
Extracts Doxygen styled documentation from source. As a result of extraction list of DocObjects will be created.
"""
def
__init__
(
self
):
self
.
file_name
=
None
self
.
source
=
None
self
.
doc_list
=
[]
def
parse_file
(
self
,
filename
):
self
.
file_name
=
filename
self
.
source
=
open
(
filename
).
readlines
()
print
self
.
source
docObject
=
DocObject
()
for
i_line
in
range
(
0
,
len
(
self
.
source
)):
line
=
self
.
source
[
i_line
]
if
self
.
is_doxygen_comment
(
line
):
docObject
.
addLine
(
line
,
i_line
)
else
:
if
len
(
docObject
):
self
.
doc_list
.
append
(
docObject
)
docObject
=
DocObject
()
def
getDocObject
(
self
,
line_number
):
"""
Returns DocObject which is located above given line number and the distance between given line number
and doc block is not larger than 1 line.
"""
for
doc
in
self
.
doc_list
:
diff
=
line_number
-
doc
.
end_line
print
"
line_number:
"
,
line_number
,
"
doc_end_line
"
,
doc
.
end_line
,
diff
if
diff
>=
0
and
diff
<=
2
:
return
doc
return
None
def
is_doxygen_comment
(
self
,
text
):
return
text
.
lstrip
()[:
3
]
==
"
//!
"
if
__name__
==
'
__main__
'
:
# filename = "/home/pospelov/development/BornAgain/source/Core/Tools/inc/CustomBinAxis.h"
# filename = "/home/pospelov/development/BornAgain/source/Core/FormFactors/inc/FormFactorCone.h"
filename
=
"
/home/pospelov/development/BornAgain/source/Core/Samples/inc/ParticleLayout.h
"
extractor
=
DoxygenDocExtractor
()
extractor
.
parse_file
(
filename
)
print
extractor
.
getDocObject
(
27
).
getPythonDocString
()
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