2020-07-27 18:57:36 +00:00
|
|
|
#!/usr/bin/env python -B
|
|
|
|
"""
|
|
|
|
Generates a JS schema file from sandbox/grist/schema.py.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), 'grist'))
|
|
|
|
|
|
|
|
import schema # pylint: disable=import-error,wrong-import-position
|
|
|
|
|
|
|
|
# These are the types that appear in Grist metadata columns.
|
|
|
|
_ts_types = {
|
|
|
|
"Bool": "boolean",
|
|
|
|
"DateTime": "number",
|
|
|
|
"Int": "number",
|
|
|
|
"PositionNumber": "number",
|
|
|
|
"Ref": "number",
|
2021-06-29 04:47:59 +00:00
|
|
|
"RefList": "['L', ...number[]]|null", # Non-primitive values are encoded
|
2020-07-27 18:57:36 +00:00
|
|
|
"Text": "string",
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_ts_type(col_type):
|
|
|
|
col_type = col_type.split(':', 1)[0] # Strip suffix for Ref:, DateTime:, etc.
|
|
|
|
return _ts_types.get(col_type, "CellValue")
|
|
|
|
|
|
|
|
def main():
|
2021-06-29 04:47:59 +00:00
|
|
|
print("""\
|
2020-07-27 18:57:36 +00:00
|
|
|
/*** THIS FILE IS AUTO-GENERATED BY %s ***/
|
|
|
|
// tslint:disable:object-literal-key-quotes
|
|
|
|
|
|
|
|
export const schema = {
|
2021-06-22 15:12:25 +00:00
|
|
|
""" % __file__)
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
for table in schema.schema_create_actions():
|
2021-06-22 15:12:25 +00:00
|
|
|
print(' "%s": {' % table.table_id)
|
2020-07-27 18:57:36 +00:00
|
|
|
for column in table.columns:
|
2021-06-22 15:12:25 +00:00
|
|
|
print(' %-20s: "%s",' % (column['id'], column['type']))
|
|
|
|
print(' },\n')
|
2020-07-27 18:57:36 +00:00
|
|
|
|
2021-06-22 15:12:25 +00:00
|
|
|
print("""};
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
export interface SchemaTypes {
|
2021-06-22 15:12:25 +00:00
|
|
|
""")
|
2020-07-27 18:57:36 +00:00
|
|
|
for table in schema.schema_create_actions():
|
2021-06-22 15:12:25 +00:00
|
|
|
print(' "%s": {' % table.table_id)
|
2020-07-27 18:57:36 +00:00
|
|
|
for column in table.columns:
|
2021-06-22 15:12:25 +00:00
|
|
|
print(' %s: %s;' % (column['id'], get_ts_type(column['type'])))
|
|
|
|
print(' };\n')
|
|
|
|
print("}")
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|