- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathextract_code.py
More file actions
Latest commit
executable file
·62 lines (53 loc) · 2.1 KB
/
Copy pathextract_code.py
File metadata and controls
executable file
·62 lines (53 loc) · 2.1 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
importos
importre
importsubprocess
defdoctest_2to3_results(src_iterator):
"""transforms the results sections of a doctests source to fit with python3"""
forlineinsrc_iterator:
ifline.startswith(">>>") orline.startswith("..."):
yieldline
else:
# TODO: um, this happens to work for the tutorial, but isn't the most robust...
if"u'"inline:
line=line.replace("u'", "'")
if'u"'inline:
line=line.replace('u"', '"')
yieldline
defextract_code(src_iterator):
"""extracts the code from a doctests source"""
forlineinsrc_iterator:
line=line.replace("echo=False", "echo=True")
if"doctest:"inlineand"#"inline:
comment_start=line.rfind("#", 0, line.rfind("doctest:"))
ifcomment_start!=-1:
line=line[:comment_start].rstrip()
ifline.startswith(">>>") orline.startswith("..."):
line=line[4:]
ifnotline:
yield" \n"
have_break=False
else:
yieldline.rstrip()+"\n"
have_break=notline.strip()
elifline.startswith("#"):
yieldline.rstrip()+"\n"
have_break=False
elifnotline.strip() andnothave_break:
yield"\n"
have_break=True
subprocess.call(["2to3", "-d", "-w", "-n", "--add-suffix=3", "-o", ".", "steps2.txt"])
withopen("steps3.txt", "w") asfix_file, open("steps2.txt3", "r") assrc_file:
forlineindoctest_2to3_results(src_file):
fix_file.write(line)
os.remove("steps2.txt3")
withopen("blogapp2.py", "w") aspy_file, open("steps2.txt") assteps_file:
py_file.write("#!/usr/bin/env python\n\n")
have_break=True
forlineinextract_code(steps_file):
py_file.write(line)
withopen("blogapp3.py", "w") aspy_file, open("steps3.txt") assteps_file:
py_file.write("#!/usr/bin/env python\n\n")
have_break=True
forlineinextract_code(steps_file):
py_file.write(line)