-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgenerate_examples_readme.py
130 lines (102 loc) · 4.07 KB
/
generate_examples_readme.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# SymBeam examples suit
# README.md generation script
# ==========================================================================================
# António Carneiro <[email protected]> 2020
import glob
import os
import re
# Human sorting: from https://stackoverflow.com/questions/5967500/
# how-to-correctly-sort-a-string-with-a-number-inside
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
"""
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
"""
return [atoi(c) for c in re.split(r"(\d+)", text)]
# Get list of examples and remove the current script
list_examples = glob.glob("./*.py")
list_examples.remove("./" + __file__)
list_examples.sort(key=natural_keys)
# Create the directory for SVG files
svg_dir = "./svg"
if not os.path.exists(svg_dir):
os.makedirs(svg_dir)
# Start writing the README.md
with open("README.md", "w") as file:
file.write("# Examples\n")
file.write(
"Here you can find a comprehensive but by no means exhaustive list of"
+ " examples exploring the capabilities of SymBeam. In each example,"
+ " you will find a hyperlink to the associated file in the repository,"
+ " the respective source code and output: both console and plot.\n"
)
# Loop over all examples
for example in list_examples:
name = os.path.basename(example)
file.write("\n## [{0}](./{1})".format(name, name))
print("\nRunning {0}".format(example))
# Create an emtpy temporary file
tmp_file = "./tmp_symbeam"
open(tmp_file, "w").close()
# Run the script and capture the terminal output
os.system("python3 " + example + " >> " + tmp_file)
# Check if a .svg file has been produced and move it into the svg/ directory
has_svg = False
svg_path = os.path.splitext(example)[0] + ".svg"
if os.path.exists(svg_path):
found = True
print("Found {0}".format(svg_path))
new_svg_path = os.path.join(svg_dir, svg_path)
os.rename(svg_path, new_svg_path)
# Read the Markdown content in each script
with open(example, "r") as example_file:
example_lines = example_file.readlines()
with open(tmp_file, "r") as tfile:
output_lines = tfile.readlines()
# Find the documentation and source code
doc_start = 0
doc_end = 0
for i, line in enumerate(example_lines):
if len(line.split()) > 1:
if line.split()[0] == "#":
if line.split()[1] == "Features:":
doc_start = i
else:
doc_end = i - 1
break
source_start = doc_end + 1
source_end = len(example_lines) - 1
doc_lines = []
doc_lines.append(" ".join(example_lines[doc_start].split()[2:]))
for i in range(doc_start + 1, doc_end + 1):
doc_lines.append(" ".join(example_lines[i].split()[1:]))
# Write the example features
for line in doc_lines:
file.write("\n" + line)
# Write the source code and output to the README.md file
file.write("\n```python\n")
found_blank = 0
for line in example_lines[source_start + 2 : source_end + 1]:
# Stop when an emtpy line is found
if len(line.split()) == 0:
found_blank = found_blank + 1
if len(line.split()) == 0 and found_blank > 2:
break
file.write(line)
file.write("```")
# Write image code
file.write(
'\n<p align="center">\n <img src="{0}" width="70%">\n</p>\n'.format(
new_svg_path
)
)
# Wite terminal output
file.write("\n```")
for line in output_lines:
file.write(line)
file.write("\n```")
if os.path.exists(tmp_file):
os.remove(tmp_file)