-
Notifications
You must be signed in to change notification settings - Fork 28
/
generate_proptypes.py
365 lines (291 loc) · 9.32 KB
/
generate_proptypes.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
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
356
357
358
359
360
361
362
363
364
365
from bs4 import BeautifulSoup
import json
import re
import requests
import sys
MODE = "GRID"
if len(sys.argv) == 2 and sys.argv[1] == "--col-defs":
MODE = "COL"
DASH_PROPS = """
/**
* The ID used to identify this component in Dash callbacks.
*/
id: PropTypes.string,
/**
* Dash-assigned callback that gets fired when the input changes
*/
setProps: PropTypes.func,
/**
* The children of this component
*/
children: PropTypes.node,
/**
* The CSS style for the component
*/
style: PropTypes.object,
/**
* Used to allow user interactions in this component to be persisted when
* the component - or the page - is refreshed. If `persisted` is truthy and
* hasn't changed from its previous value, a `value` that the user has
* changed while using the app will keep that change, as long as
* the new `value` also matches what was given originally.
* Used in conjunction with `persistence_type`.
*/
persistence: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
PropTypes.number,
]),
/**
* Properties whose user interactions will persist after refreshing the
* component or the page. Since only `value` is allowed this prop can
* normally be ignored.
*/
persisted_props: PropTypes.arrayOf(PropTypes.oneOf(['value'])),
/**
* Where persisted user changes will be stored:
* memory: only kept in memory, reset on page refresh.
* local: window.localStorage, data is kept after the browser quit.
* session: window.sessionStorage, data is cleared once the browser quit.
*/
persistence_type: PropTypes.oneOf(['local', 'session', 'memory']),
"""
# Several props do not exist in the documentation or are custom, add them manually
GRID_PROPS = """
/**
* Size the columns automatically or to fit their contents
*/
columnSize: PropTypes.oneOf(['sizeToFit', 'autoSizeAll']),
/**
* The ag-grid provided theme to use. More info here: https://www.ag-grid.com/javascript-grid/themes-provided/
*/
theme: PropTypes.oneOf([
'alpine',
'balham',
'material',
'bootstrap',
]),
/**
* Serverside model data request object.
* See https://www.ag-grid.com/react-grid/server-side-model-datasource/
*/
getRowsRequest: PropTypes.shape({
/**
* for Infinite Scroll (i.e. Partial Store) only, first row requested
*/
startRow: PropTypes.number,
/**
* for Infinite Scroll (i.e. Partial Store) only, last row requested
*/
endRow: PropTypes.number,
/**
* row group columns
*/
rowGroupCols: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
displayName: PropTypes.string,
field: PropTypes.string,
aggFunc: PropTypes.string,
})),
/**
* value columns
*/
valueCols: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
displayName: PropTypes.string,
field: PropTypes.string,
aggFunc: PropTypes.string,
})),
/**
* pivot columns
*/
pivotCols: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
displayName: PropTypes.string,
field: PropTypes.string,
aggFunc: PropTypes.string,
})),
/**
* true if pivot mode is one, otherwise false
*/
pivotMode: PropTypes.bool,
/**
* what groups the user is viewing
*/
groupKeys: PropTypes.arrayOf(PropTypes.string),
/**
* if filtering, what the filter model is
*/
filterModel: PropTypes.any,
/**
* if sorting, what the sort model is
*/
sortModel: PropTypes.any,
}),
/**
* Serverside model data response object.
* See https://www.ag-grid.com/react-grid/server-side-model-datasource/
*/
getRowsResponse: PropTypes.shape({
/**
* data retreived from the server
*/
rowData: PropTypes.arrayOf(PropTypes.any),
/**
* for Infinite Scroll (i.e. Partial Store) only, the last row, if known
*/
rowCount: PropTypes.number,
/**
* any extra info for the grid to associate with this load
*/
storeInfo: PropTypes.any,
}),
/**
* License key for ag-grid enterprise. If using Enterprise modules,
* enableEnterpriseModules must also be true.
*/
licenseKey: PropTypes.string,
/**
* If True, enable ag-grid Enterprise modules. Recommended to use with licenseKey.
*/
enableEnterpriseModules: PropTypes.bool,
"""
COL_PROPS = """
/**
* boolean. Set to true to show a checkbox in the header of a column.
* Default Value: false
*/
headerCheckboxSelection: PropTypes.bool,
/**
* boolean. Set to true for checkbox selections to only affect filtered data.
* Default Value: false
*/
headerCheckboxSelectionFilteredOnly: PropTypes.bool,
"""
GRID_URLS = [
("GRID PROPS", "https://www.ag-grid.com/react-grid/grid-properties/"),
("EVENT PROPS", "https://www.ag-grid.com/react-grid/grid-events/"),
]
COL_URLS = [
("COLDEF PROPS", "https://www.ag-grid.com/react-grid/column-properties/"),
]
def print_header(title):
print(
"""
/********************************
* {}
*******************************/
""".format(
title
)
)
def dequote(s):
s = re.sub(r"'", "", s)
s = re.sub(r'"', "", s)
s = s.strip()
return s
def parse_value(v):
try:
v = json.loads(v)
except json.decoder.JSONDecodeError:
pass
if type(v) == str:
if v[0] == "[":
v = re.sub(r"\[", "", v)
v = re.sub(r"\]", "", v)
v = [dequote(v) for v in v.split(",")]
else:
v = dequote(v)
return v
def pprinter(string):
s = str(string)
return s.replace("False", "false").replace("None", "null")
def process_description(td):
htmlstr = str(td)
brs = re.sub(r"<br\s?\/?>", "\n", htmlstr).split("\n")
td_desc = brs.pop(0)
desc = BeautifulSoup(td_desc, "html.parser").get_text()
default_value = None
options = None
for br in brs:
if "Default" in br:
default_value = parse_value(BeautifulSoup(br, "html.parser").code.text)
elif "Options" in br:
html_codes = BeautifulSoup(br, "html.parser").find_all("code")
options = []
for html_code in html_codes:
code_value = html_code.text
options.append(parse_value(code_value))
return desc, default_value, options
def to_proptypes(value):
proptype = None
if type(value) == int:
proptype = "number"
elif type(value) == str:
proptype = "string"
elif type(value) == bool:
proptype = "bool"
elif type(value) == list:
proptype = f"oneOf({value})"
else:
proptype = "any"
return f"PropTypes.{proptype}"
def print_comment_sentences(comment, pad=" "):
words = comment.split(" ")
while len(words) > 0:
line = " *"
while len(words) and len(line) < 80:
line += " " + words.pop(0)
print(pad + line)
if __name__ == "__main__":
if MODE == "GRID":
urls = GRID_URLS
else:
urls = COL_URLS
pad = " "
if MODE == "GRID":
print("DashAgGrid.propTypes = {")
else:
print("DashAgGridColumn.propTypes = {")
print_header("DASH PROPS")
print(DASH_PROPS)
print_header("CUSTOM PROPS")
if MODE == "GRID":
print(GRID_PROPS)
else:
print(COL_PROPS)
for (prop_category, url) in urls:
print_header(prop_category)
page = requests.get(url)
soup = BeautifulSoup(page.text, "html.parser")
for table in soup.find_all("table"):
if MODE == "COL":
print_header(table.previous_element)
tbody = table.tbody
for row in tbody.find_all("tr"):
tds = row.find_all("td")
if len(tds) != 2:
continue
# Some props have <br> as there are more than
# one name per property. Take the first
tdstr = str(tds[0])
if "<br" in tdstr:
first = re.sub(r"<br\s?\/?>", "\n", tdstr).split("\n")[0]
prop = BeautifulSoup(first, "html.parser").get_text()
else:
prop = tds[0].get_text()
# Skip callback / function prop types
if "(" and ")" in prop:
continue
desc, default_value, options = process_description(tds[1])
if options is not None:
if default_value in options:
default_value = options
print(pad + "/**")
print_comment_sentences(desc, pad=pad)
if default_value is not None:
print(pad + f" * Default Value: {pprinter(default_value)}")
print(pad + " */")
print(f"{pad}{prop}: {pprinter(to_proptypes(default_value))},")
print()
print("};")