forked from astropy/astroquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_doctests.py
39 lines (31 loc) · 1.19 KB
/
make_doctests.py
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
"""
A hacktackular (tm) script for extracting runnable code from the docs and
writing them to 'doctests.py'. This was easier than figuring out how to
manipulate the doctest system to do the same thing, but it is an unsustainable
model. As such, this file should be deleted once we have functioning doctests.
"""
import os
import re
leftarrows = re.compile(r"^ *>>> ")
leftdots = re.compile(r"^ *\.\.\. ")
docskip = re.compile(r'doctest: \+SKIP')
def test_line(line):
if docskip.search(line):
return False
elif leftarrows.search(line):
return True
elif leftdots.search(line):
if line.count('...') == 1:
return True
def strip_line(line):
return leftdots.sub("", leftarrows.sub("", line))
with open('doctests.py', 'w') as doctests:
for root, dirs, files in os.walk('.'):
for fn in files:
if os.path.splitext(fn)[1] == '.rst':
with open(os.path.join(root, fn), 'r') as f:
lines = f.read().splitlines()
pylines = [strip_line(L)
for L in lines if test_line(L)]
doctests.write("# {fn}\n".format(fn=fn))
doctests.writelines(pylines)