File: make_doctests.py

package info (click to toggle)
astroquery 0.4.6%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,420 kB
  • sloc: xml: 56,574; python: 43,303; makefile: 145; ansic: 69
file content (39 lines) | stat: -rw-r--r-- 1,222 bytes parent folder | download | duplicates (4)
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)