-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_docstring.py
More file actions
34 lines (28 loc) · 1.09 KB
/
Copy pathparse_docstring.py
File metadata and controls
34 lines (28 loc) · 1.09 KB
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
from textwrap import dedent
def parse_docstring(docstring):
"""Grab nicely formatted sections from test docstring
Args:
docstring (str): test docstring
Returns:
dict where keys are title (for test title, the 1st line of docstring),
pre (for preconditions), step (for steps to execute),
expect (for expected results) and values are dedented texts
from corresponding docstring sections
"""
isdash = lambda s: all('-' == i for i in s.strip())
head, _, tail = docstring.partition('\n')
head = head.strip()
text = (line for line in dedent(tail).splitlines() if line and not isdash(line))
sections = dict(title=head if head else text.next(), pre=[], step=[], expect=[])
cur_section = sections['pre']
for line in text:
if line.endswith(':'):
for k in sections:
if line.lower().startswith(k):
cur_section = sections[k]
break
else:
cur_section.append(line)
else:
cur_section.append(line)
return sections