-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert_reference.py
131 lines (101 loc) · 3.14 KB
/
convert_reference.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
131
"""
Script used to convert reference.txt file into many *.rst files and *.csv
under source/06/03 folder.
"""
import csv
def get_function_name(f):
line = f.readline()
if not line:
return None
fname = line.replace('\\_', '_').strip()
# scrap line "~~~~~~~~"
line = f.readline()
return fname
def get_description(f):
line = f.readline().strip()
while not line:
line = f.readline().strip()
return line
def get_format(f):
line = f.readline().strip()
while not line == '[Format]':
line = f.readline().strip()
line = f.readline().strip()
while not line:
line = f.readline().strip()
line = line.replace('\\_', '_').replace('**', '').replace('\\ ', '')
line2 = f.readline().strip()
if not line2 == '':
line = line + ' ' + line2
line = line.replace('_f (', '_f(')
return line
def _clean_frags(frags):
ret = []
for s in frags:
s = s.strip()
if s == '':
continue
s = s.replace('**', '')
s = s.replace('\\*', '*')
ret.append(s)
if len(ret) < 4:
return None
reordered_ret = []
reordered_ret.append(ret[1])
reordered_ret.append(ret[0])
reordered_ret.append(ret[2])
reordered_ret.append(ret[3])
return reordered_ret
def get_args(f):
data = []
line = f.readline().strip()
while not line == '[Arguments]':
line = f.readline().strip()
line = f.readline().strip()
line = f.readline().strip()
while not line == '':
if line[0] == '+':
line = f.readline().strip()
continue
frags = line.split('|')
cleaned_frags = _clean_frags(frags)
if cleaned_frags is None:
line = f.readline().strip()
continue
data.append(cleaned_frags)
line = f.readline().strip()
return data
fnames_f = open('ref.txt', 'w')
with open('reference.txt', 'r', encoding='utf-8') as f:
while True:
fname = get_function_name(f)
if fname is None:
exit()
desc = get_description(f)
format = get_format(f)
args = get_args(f)
print(fname)
fnames_f.write(' 03/' + fname + '\n')
with open('source/06/03/' + fname + '.rst', 'w', encoding='utf-8') as f2:
f2.write(fname + '\n')
f2.write('=' * len(fname) + '\n')
f2.write('\n')
f2.write(desc + '\n')
f2.write('\n')
f2.write('Format\n')
f2.write('------\n')
f2.write('.. code-block:: fortran\n')
f2.write('\n')
f2.write(' ' + format + '\n')
f2.write('\n')
f2.write('Arguments\n')
f2.write('---------\n')
f2.write('\n')
f2.write('.. csv-table:: Arguments of ' + fname + '\n')
f2.write(' :file: ' + fname + '_args.csv\n')
f2.write(' :header-rows: 1\n')
f2.write('\n')
with open('source/06/03/' + fname + '_args.csv', 'w', encoding='utf-8') as f2:
w = csv.writer(f2, lineterminator='\n')
w.writerows(args)
fnames_f.close()