forked from ilanschnell/bitarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_doc.py
140 lines (106 loc) · 3.77 KB
/
update_doc.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
132
133
134
135
136
137
138
139
140
import sys
assert sys.version_info[0] == 3, "This program requires Python 3"
import re
import doctest
from io import StringIO
import bitarray
import bitarray.util
BASE_URL = "https://github.com/ilanschnell/bitarray"
sig_pat = re.compile(r'(\w+\([^()]*\))( -> (.+))?')
def write_doc(fo, name):
doc = eval('bitarray.%s.__doc__' % name)
assert doc, name
lines = doc.splitlines()
m = sig_pat.match(lines[0])
if m is None:
raise Exception("signature line invalid: %r" % lines[0])
s = '``%s``' % m.group(1)
if m.group(3):
s += ' -> %s' % m.group(3)
fo.write('%s\n' % s)
assert lines[1] == ''
for line in lines[2:]:
out = line.rstrip()
fo.write(" %s\n" % out.replace('`', '``') if out else "\n")
fo.write('\n\n')
def write_reference(fo):
fo.write("""\
Reference
=========
bitarray version: %s -- `change log <%s>`__
In the following, ``item`` and ``value`` are usually a single bit -
an integer 0 or 1.
The bitarray object:
--------------------
""" % (bitarray.__version__, "%s/blob/master/doc/changelog.rst" % BASE_URL))
write_doc(fo, 'bitarray')
fo.write("**A bitarray object supports the following methods:**\n\n")
for method in sorted(dir(bitarray.bitarray)):
if method.startswith('_'):
continue
write_doc(fo, 'bitarray.%s' % method)
fo.write("Other objects:\n"
"--------------\n\n")
write_doc(fo, 'frozenbitarray')
write_doc(fo, 'decodetree')
fo.write("Functions defined in the `bitarray` module:\n"
"-------------------------------------------\n\n")
for func in sorted(['test', 'bits2bytes', 'get_default_endian']):
write_doc(fo, func)
fo.write("Functions defined in `bitarray.util` module:\n"
"--------------------------------------------\n\n")
for func in bitarray.util.__all__:
write_doc(fo, 'util.%s' % func)
def update_readme(path):
ver_pat = re.compile(r'(bitarray.+?)(\d+\.\d+\.\d+)')
with open(path, 'r') as fi:
data = fi.read()
with StringIO() as fo:
for line in data.splitlines():
if line == 'Reference':
break
line = ver_pat.sub(lambda m: m.group(1) + bitarray.__version__,
line)
fo.write("%s\n" % line.rstrip())
write_reference(fo)
new_data = fo.getvalue()
if new_data == data:
print("already up-to-date")
else:
with open(path, 'w') as f:
f.write(new_data)
def write_changelog(fo):
ver_pat = re.compile(r'(\d{4}-\d{2}-\d{2})\s+(\d+\.\d+\.\d+)')
issue_pat = re.compile(r'#(\d+)')
link_pat = re.compile(r'\[(.+)\]\((.+)\)')
def issue_replace(match):
url = "%s/issues/%s" % (BASE_URL, match.group(1))
return "`%s <%s>`__" % (match.group(0), url)
fo.write("Change log\n"
"==========\n\n")
for line in open('./CHANGE_LOG'):
line = line.rstrip()
match = ver_pat.match(line)
if match:
line = match.expand(r'**\2** (\1):')
elif line.startswith('-----'):
line = ''
elif line.startswith(' '):
line = line[2:]
line = line.replace('`', '``')
line = issue_pat.sub(issue_replace, line)
line = link_pat.sub(
lambda m: "`%s <%s>`__" % (m.group(1), m.group(2)), line)
fo.write(line + '\n')
def main():
if len(sys.argv) > 1:
sys.exit("no arguments expected")
update_readme('./README.rst')
with open('./doc/reference.rst', 'w') as fo:
write_reference(fo)
with open('./doc/changelog.rst', 'w') as fo:
write_changelog(fo)
doctest.testfile('./README.rst')
doctest.testfile('./doc/represent.rst')
if __name__ == '__main__':
main()