This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_rst.py
73 lines (59 loc) · 1.89 KB
/
parse_rst.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
"""
Convert rst to a compressed dictionary suitable for NEURON + Python
help system.
Run via:
python parse_rst.py `find . -name \*.rst -print`
"""
import sys
help_dictionary = {}
filenames = sys.argv[1:]
def handle_identifier(lines, i, identifier):
identifier = '.. %s::' % identifier
line = lines[i]
start = line.find(identifier)
#print line, identifier, start
#print identifier
if start >= 0:
name = line[start + len(identifier):].strip()
print '%s -- %s' % (name, identifier)
indent_line = lines[i + 1]
while not indent_line.strip():
i += 1
indent_line = lines[i + 1]
start = 0
while start < len(indent_line):
if indent_line[start] != ' ': break
start += 1
# TODO: store the body text
body = []
while i < len(lines) - 1:
i += 1
if lines[i].strip():
if lines[i][:start] == indent_line[:start]:
next_line = lines[i][start:]
if next_line[-1] == '\n':
next_line = next_line[: -1]
body.append(next_line)
else:
break
else:
if not body or body[-1] != '\n':
body.append('\n')
help_dictionary[name] = '\n'.join(body)
for filename in filenames:
with open(filename) as f:
lines = []
for line in f:
if line[-1] == '\n':
line = line[:-1]
lines.append(line)
i = 0
while i < len(lines):
for kind in ['method', 'data', 'class', 'function']:
handle_identifier(lines, i, kind)
i += 1
import cPickle
import zlib
compressed = zlib.compress(cPickle.dumps(help_dictionary))
with open('help_data.dat', 'wb') as f:
f.write(compressed)