hv2-all-in-one/generate.py

77 lines
2.7 KiB
Python
Raw Normal View History

2021-08-02 16:52:31 +02:00
import json
from Cheetah.Template import Template
import glob
import argparse
parser = argparse.ArgumentParser(description="generate.py")
parser.add_argument('--schema', '-s',
help='Schema file. Default: schema.json in the current folder.',
required=False,
default='./schema.json')
parser.add_argument('--template', '-t',
help="""Template file, templates files must be named as the final output file
with an additional .tmpl extension. Default: all template files recursively from the current folder.""",
2021-08-02 16:52:31 +02:00
required=False,
default="**/*.tmpl")
2021-08-02 16:52:31 +02:00
args = parser.parse_args()
with open(args.schema) as schemaFile:
schema = json.load(schemaFile)
for table in schema["tables"]:
columns = table["columns"]
for column in columns:
2021-08-02 16:52:31 +02:00
if column["sqltype"] == 'serial':
column["apitype"] = 'integer'
column["jstype"] = 'number'
elif column["sqltype"] == 'integer':
column["apitype"] = 'integer'
column["jstype"] = 'number'
elif column["sqltype"] == 'date':
column["apitype"] = 'string'
column["jstype"] = 'string'
elif column["sqltype"] == 'timestamp':
column["apitype"] = 'string'
column["jstype"] = 'string'
elif column["sqltype"].startswith('varchar'):
column["apitype"] = 'string'
column["jstype"] = 'string'
elif column["sqltype"].startswith('numeric'):
column["apitype"] = 'number'
column["jstype"] = 'number'
table["selectors"] = [ y["name"] for y in sorted([ x for x in columns if "selector" in x ], key=lambda x: x["selector"])]
2021-08-02 16:52:31 +02:00
2021-08-28 21:12:08 +02:00
schema["GENERATED_SQL_COMMENT"] = """
2021-08-29 12:50:59 +02:00
-- ----------------------------------------
-- THIS FILE HAS BEEN GENERATED
2021-08-28 21:12:08 +02:00
-- DO NOT EDIT MANUALLY
2021-08-29 12:50:59 +02:00
-- ----------------------------------------
2021-08-28 21:12:08 +02:00
"""
schema["GENERATED_PYTHON_COMMENT"] = """
2021-08-29 12:50:59 +02:00
# -----------------------------------------
2021-08-28 21:12:08 +02:00
# THIS FILE HAS BEEN GENERATED
# DO NOT EDIT MANUALLY
2021-08-29 12:50:59 +02:00
# -----------------------------------------
"""
schema["GENERATED_YAML_COMMENT"] = """
# -----------------------------------------
# THIS FILE HAS BEEN GENERATED
# DO NOT EDIT MANUALLY
# -----------------------------------------
"""
schema["GENERATED_TS_COMMENT"] = """
// -----------------------------------------
// THIS FILE HAS BEEN GENERATED
// DO NOT EDIT MANUALLY
// -----------------------------------------
2021-08-28 21:12:08 +02:00
"""
for f in glob.glob(args.template, recursive=True):
2021-08-02 16:52:31 +02:00
print(f"process {f}")
tmpl = Template(file=f, searchList=[schema])
with open(f[:-5], 'w') as outFile:
outFile.write(str(tmpl))