2020-07-27 18:57:36 +00:00
|
|
|
#!/usr/bin/env python -B
|
|
|
|
"""
|
|
|
|
Generates a JS schema file from sandbox/grist/schema.py.
|
|
|
|
"""
|
|
|
|
|
2021-06-30 14:46:38 +00:00
|
|
|
import schema # pylint: disable=import-error
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
# These are the types that appear in Grist metadata columns.
|
|
|
|
_ts_types = {
|
|
|
|
"Bool": "boolean",
|
|
|
|
"DateTime": "number",
|
|
|
|
"Int": "number",
|
|
|
|
"PositionNumber": "number",
|
|
|
|
"Ref": "number",
|
2021-10-07 21:02:51 +00:00
|
|
|
"RefList": "[GristObjCode.List, ...number[]]|null", # Non-primitive values are encoded
|
|
|
|
"ChoiceList": "[GristObjCode.List, ...string[]]|null",
|
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 ***/
|
2021-10-07 21:02:51 +00:00
|
|
|
|
|
|
|
import { GristObjCode } from "app/plugin/GristData";
|
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
// tslint:disable:object-literal-key-quotes
|
|
|
|
|
2021-10-01 13:45:27 +00:00
|
|
|
export const SCHEMA_VERSION = %d;
|
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
export const schema = {
|
2023-01-17 20:54:41 +00:00
|
|
|
""" % ('core/sandbox/gen_js_schema.py', schema.SCHEMA_VERSION))
|
|
|
|
# The script name is hardcoded since the Grist sandbox can be
|
|
|
|
# at different paths depending on how Grist is installed, and
|
|
|
|
# we don't want unnecessary changes to generated files.
|
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()
|