forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
executable file
·130 lines (110 loc) · 3.4 KB
/
update.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
#!/usr/bin/env python
# Brute-force Sphinx documentation from Solidity source.
# TODO: Reimplement as proper Sphinx domain?
sourcedir = '../contracts'
destdir = './gen'
import os
import re
dox = re.compile(
"""
( /\*\*.*\*/ # /** ... */
| ///[^\n]* ) # /// ...
( [^;{]* ) # signature
( ;|{ ) # end
""",
re.DOTALL | re.VERBOSE)
fsig = re.compile(
"""
( function ) {1} # function
( [^\(]* ) ? # functionName
( \( [^\)]* \) ) {1} # (args)
( .* ) ? # EVERYTHING ELSE
""",
re.DOTALL | re.VERBOSE)
class Contract(object):
"""TODO"""
pass
class Function(object):
"""TODO"""
pass
class Documented(object):
"""TODO"""
def __init__(self, rematch):
self.comment = self.parse_comment(rematch.group(1))
self.type_, self.name, self.other = self.parse_signature(rematch.group(2))
self.endsym = rematch.group(3)
return
def parse_comment(self, c):
"""TODO"""
if c.startswith('///'):
c = c[3:] # remove ///
elif c.startswith('/**'):
# remove /** and */
c = c[3:-2]
# remove in-the-middle *s
lines = c.split('\n')
res = []
for l in lines:
l = l.strip()
if l.startswith('* '):
l = l[2:]
if l == '*':
l = ''
res.append(l)
c = '\n'.join(res)
return c.strip() # remove leading/trailing whitespace
def parse_signature(self, s):
s = s.strip()
type_ = s.partition(' ')[0]
name = ''
other = {}
if type_ == 'contract':
words = s.split(' ')
name = words[1]
if len(words) > 2:
parents = []
# skip "is"
for i in range(3,len(words)):
parent = words[i].strip(',')
parents.append(parent)
other = {'parents': parents}
elif type_ == 'function':
sig = fsig.findall(s)[0]
name = sig[1].strip()
argstr = sig[2].strip('()')
modret = sig[3].strip()
# args = {}
# for a in argstr.strip('()').split(','):
# a = a.split()
# argtype = a[0]
# argname = a[1]
other = {'args': argstr, 'modret': modret}
return (type_, name, other)
def get_documented(data):
docstrings = []
iterator = dox.finditer(data)
for match in iterator:
docstrings.append(Documented(match))
return docstrings
def main():
for root, dirs, files in os.walk(sourcedir):
for f in files:
filename = os.path.join(root, f)
print('Trying ' + filename + ' ...')
if not filename.endswith('.sol'):
print('Not a Solidity file!')
continue
with open(filename, 'r') as fd:
filestring = fd.read()
docstrings = get_documented(filestring)
for d in docstrings:
print('=====')
print(d.type_)
print(d.name)
print(d.other)
print('-----')
print(d.comment)
print('-----')
return
if __name__ == '__main__':
main()