This repository was archived by the owner on Aug 30, 2023. It is now read-only.
forked from ChrisBeaumont/scidbpy-aflgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
355 lines (273 loc) · 8.62 KB
/
generate.py
File metadata and controls
355 lines (273 loc) · 8.62 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from subprocess import PIPE, Popen
import re
def manpage(file):
"""
Read a manfile into a string
Parameters
----------
file : str
Path to the file to read
Returns
-------
The formatted manpage
"""
p = Popen(['nroff', '-man', '-Tascii', file], stdout=PIPE)
p.wait()
result = p.stdout.read()
# remove backspaces
result = re.sub('.\x08', '', result)
return result
def parse_sections(manpage):
"""
Parse a manpage into section header -> section text pairs
Parameters
----------
manpage : str
The manpage content
Returns
-------
A dict mapping section headers to section contents
Notes
-----
This parses the Detailed Description subsection
"""
# Extract the Detailed Description section
manpage = re.split('\nDetailed Description', manpage)[1]
manpage = re.split('\n\w', manpage)[0]
# convert bullet points from o to -
manpage = re.sub(' o ', ' - ', manpage)
# extract sub-section titles
section_re = '(?<=\n )\w[\w ]+?(?=:)'
headers = re.findall(section_re, manpage)
sections = re.split(section_re, manpage)
replace = {"Output": "Output Array"}
for i, h in enumerate(headers):
headers[i] = replace.get(h, h).title()
# first character of section is an unwanted colon.
return dict((h, s[1:].rstrip()) for
h, s in zip(headers, sections[1:]))
def build_docstring(sections):
"""
Build a numpydoc-style docstring from a section dict
Parameters
----------
sections : dict
A dictionary returned from parse_sections
Returns
-------
docstring : str
A numpy-formatted docstring
"""
headers = ['Input', 'Output array',
'Examples', 'Notes']
labels = ['Parameters', 'Returns', 'Examples', 'Notes']
result = []
if 'Synopsis' in sections:
result.append(sections['Synopsis'].strip())
if 'Summary' in sections:
sec = sections['Summary']
summary = re.sub('\n +', ' ', sec).strip()
result.append(summary.strip())
for hdr, lbl in zip(headers, labels):
if hdr not in sections:
continue
sec = sections[hdr]
sec = collapse_newlines(sec)
sec = trim_indentation(sec)
if empty(sec):
continue
sec = labeled_section(lbl, sec)
result.append(sec)
return '\n\n'.join(result)
def indent(text, level):
"""Indent a block of text by a `level` of 4 spaces"""
try: # text is str
text = text.split('\n')
except AttributeError: # list
pass
return '\n'.join(' ' * level + line for line in text)
def build_rst(sections):
"""
Build a sphinx documentation directive from a section dict
Parameters
----------
sections : dict
A dictionary returned from parse_sections
Returns
-------
entry : str
A sphinx description of the operator
"""
headers = ['Input', 'Output array',
'Examples', 'Notes']
labels = ['Parameters', 'Returns', 'Examples', 'Notes']
result = ['']
if 'Summary' in sections:
sec = sections['Summary']
summary = re.sub('\n +', ' ', sec).strip()
summary = summary.replace(':\n -', ':\n\n -')
summary = summary.replace('\n -', '\n *')
result.extend(['', summary, ''])
if 'Synopsis' in sections:
result.append('::')
result.append(indent(['', sections['Synopsis'].strip(), ''], 2))
for hdr, lbl in zip(headers, labels):
if hdr not in sections:
continue
lbl = lbl.lower()
sec = sections[hdr]
sec = collapse_newlines(sec)
sec = trim_indentation(sec)
if empty(sec):
continue
sec = indent(sec, 1)
if lbl == 'examples':
result.extend(['', ':%s:' % lbl, '', '::'])
result.append(sec)
else:
result.extend(['', ':%s:' % lbl, sec, ''])
return ".. function:: %s" + indent(result, 1)
def man2doc(pth):
"""
Parse a numpy-style docstring from a SciDB manpage
Parameters
----------
pth : str
The path to a SciDB Operator docstring
Returns
-------
docstring : str
The numpy-style docstring
"""
return build_docstring(parse_sections(manpage(pth)))
def man2rst(pth):
return build_rst(parse_sections(manpage(pth)))
def collapse_newlines(str):
return re.sub('\n+', '\n', str)
def trim_indentation(str):
return re.sub(' ', ' ', str)
def empty(str):
return str.strip() in ['', 'n/a']
def labeled_section(title, contents):
result = [title, '-' * len(title), contents]
return '\n'.join(result)
def signature(srcpth):
"""
Return signature information for an operator, given its source
Parameters
----------
srcpath : str
Path to a SciDB operator C++ source file
Returns
-------
signature: list of strs
Each entry is one of
- array
- aggregate
- attribute
- constant
- dimanme
- expression
- schema
- args
args indicates 0 or more positional arguments
The other keywords refer to SciDB data types
Notes
-----
This just scans for ADD_PARAM_* macro occurances,
which seem to spell out the operator schema in order.
I don't know how brittle this is.
"""
with open(srcpth) as infile:
data = infile.read()
params = re.findall('(?<=ADD_PARAM_)[A-Z_0-9]+', data)
results = {
'input': 'array',
'in_array_name': 'array',
'out_array_name': 'array',
'out_dimension_name': 'dimname',
'out_attribute_name': 'attribute',
'expression': 'expression',
'schema': 'schema',
'aggregate_call': 'aggregate',
'in_array_name2': 'array',
'in_attribute_name': 'attribute',
'in_dimension_name': 'dimname',
'varies': 'args',
'constant': 'constant',
}
return [results[p.lower()] for p in params]
def operator_name(srcpath):
"""
Parse source file to discover the AFL operator declared
Parameters
----------
srcpath: str
path to a SciDB operator source .cpp file
Returns
-------
operator_name : str
"""
with open(srcpath) as infile:
data = infile.read()
regex = r'DECLARE_LOGICAL_OPERATOR_FACTORY\(.*, *"(.*)" *\)'
result = re.findall(regex, data)
assert len(result) == 1
return result[0]
def discover_operators(srcbase):
"""
Determine which operators are installed in SciDB by
scanning src/query/ops/BuildInOps.inc
Parameters
----------
srcbase : str
Path to src of SciDB source tree
Returns
-------
ops : set
A set of operator names like LogicalProject
Notes
-----
Not all operator files are actually installed (e.g.,
deprecated operators are still present in the source,
but commented out of BuildInOps.inc)
"""
pth = os.path.join(srcbase, 'query', 'ops', 'BuildInOps.inc')
data = open(pth).readlines()
lines = [line for line in data if line.startswith('LOGICAL_BUILDIN')]
return set(l.split('(')[1].split(')')[0] for l in lines)
def main(manbase, srcbase, outpath=None):
import json
from glob import glob
outpath = outpath or 'afldb.py'
result = []
rst = []
ops = discover_operators(srcbase)
for path in glob(srcbase + '/query/ops/*/Logical*cpp'):
sig = signature(path)
name = operator_name(path)
if path.split('/')[-1].split('.')[0] not in ops:
# these operators are likely deprecated and removed
print '%s not installed. Skipping' % name
continue
print '...%s' % name
manpattern = path.split('/')[-1].split('.cpp')[0]
manpath = manbase + 'scidb_' + manpattern + '.3'
try:
doc = man2doc(manpath)
rst.append(man2rst(manpath) % name)
except IndexError:
doc = manpage(manpath)
result.append(dict(name=name, doc=doc, signature=sig))
with open(outpath, 'w') as outfile:
outfile.write("# Automatically generated by scidbpy-aflgen\n")
outfile.write("# DO NOT EDIT -- changes will be overwritten!\n")
outfile.write("operators = ")
outfile.write(json.dumps(result, indent=2))
with open(outpath.strip('.py') + '.rst', 'w') as outfile:
outfile.write('\n\n'.join(rst))
if __name__ == "__main__":
import os
manpath = os.environ.get('SCIDB_MANPATH', 'man/')
srcpath = os.environ.get('SCIDB_SRCPATH', 'src/')
main(manpath, srcpath)