-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathnatspec.py
253 lines (199 loc) · 5.78 KB
/
natspec.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
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
"""
Natspec module https://solidity.readthedocs.io/en/latest/natspec-format.html
"""
from typing import Dict, Optional, Union
class UserMethod:
"""
Model the user method
"""
def __init__(self, method: Union[Dict, str]) -> None:
"""Init the object
Args:
method (Union[Dict, str]): Method info (notice)
"""
# Constructors dont have "notice: '..'"
if isinstance(method, str):
self._notice: Optional[str] = method
else:
self._notice = method.get("notice", None)
@property
def notice(self) -> Optional[str]:
"""Return the method notice
Returns:
Optional[str]: method notice
"""
return self._notice
def export(self) -> Dict:
"""Export to a python dict
Returns:
Dict: Exported user method
"""
return {"notice": self.notice}
class DevMethod:
"""
Model the dev method
"""
def __init__(self, method: Dict) -> None:
"""Init the object
Args:
method (Dict): Method infos (author, details, params, return)
"""
self._author: Optional[str] = method.get("author", None)
self._details: Optional[str] = method.get("details", None)
self._params: Dict[str, str] = method.get("params", {})
self._return: Optional[str] = method.get("return", None)
@property
def author(self) -> Optional[str]:
"""Return the method author
Returns:
Optional[str]: method author
"""
return self._author
@property
def details(self) -> Optional[str]:
"""Return the method details
Returns:
Optional[str]: method details
"""
return self._details
@property
def method_return(self) -> Optional[str]:
"""Return the method return
Returns:
Optional[str]: method return
"""
return self._return
@property
def params(self) -> Dict[str, str]:
"""Return the method params
Returns:
Dict[str, str]: method_name => params
"""
return self._params
def export(self) -> Dict:
"""Export to a python dict
Returns:
Dict: Exported dev method
"""
return {
"author": self.author,
"details": self.details,
"params": self.params,
"return": self.method_return,
}
class UserDoc:
"""
Model the user doc
"""
def __init__(self, userdoc: dict):
"""Init the object
Args:
userdoc (dict): User doc (notice, methods)
"""
self._notice: Optional[str] = userdoc.get("notice", None)
self._methods: Dict[str, UserMethod] = {
k: UserMethod(item) for k, item in userdoc.get("methods", {}).items()
}
@property
def notice(self) -> Optional[str]:
"""Return the user notice
Returns:
Optional[str]: user notice
"""
return self._notice
@property
def methods(self) -> Dict[str, UserMethod]:
"""Return the user methods
Returns:
Optional[str]: method_name => UserMethod
"""
return self._methods
def export(self) -> Dict:
"""Export to a python dict
Returns:
Dict: Exported user doc
"""
return {
"methods": {k: items.export() for k, items in self.methods.items()},
"notice": self.notice,
}
class DevDoc:
"""
Model the dev doc
"""
def __init__(self, devdoc: Dict):
"""Init the object
Args:
devdoc (Dict): dev doc (author, details, methods, title)
"""
self._author: Optional[str] = devdoc.get("author", None)
self._details: Optional[str] = devdoc.get("details", None)
self._methods: Dict[str, DevMethod] = {
k: DevMethod(item) for k, item in devdoc.get("methods", {}).items()
}
self._title: Optional[str] = devdoc.get("title", None)
@property
def author(self) -> Optional[str]:
"""Return the dev author
Returns:
Optional[str]: dev author
"""
return self._author
@property
def details(self) -> Optional[str]:
"""Return the dev details
Returns:
Optional[str]: dev details
"""
return self._details
@property
def methods(self) -> Dict[str, DevMethod]:
"""Return the dev methods
Returns:
Dict[str, DevMethod]: method_name => DevMethod
"""
return self._methods
@property
def title(self) -> Optional[str]:
"""Return the dev title
Returns:
Optional[str]: dev title
"""
return self._title
def export(self) -> Dict:
"""Export to a python dict
Returns:
Dict: Exported dev doc
"""
return {
"methods": {k: items.export() for k, items in self.methods.items()},
"author": self.author,
"details": self.details,
"title": self.title,
}
class Natspec:
"""
Model natspec
"""
def __init__(self, userdoc: Dict, devdoc: Dict):
"""Init the object
Args:
userdoc (Dict): user doc
devdoc (Dict): dev doc
"""
self._userdoc: UserDoc = UserDoc(userdoc)
self._devdoc: DevDoc = DevDoc(devdoc)
@property
def userdoc(self) -> UserDoc:
"""Return the userdoc
Returns:
UserDoc: user documentation
"""
return self._userdoc
@property
def devdoc(self) -> DevDoc:
"""Return the devdoc
Returns:
DevDoc: dev documentation
"""
return self._devdoc