50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
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 in the current folder.""",
|
|
required=False,
|
|
default="*.tmpl")
|
|
args = parser.parse_args()
|
|
|
|
|
|
with open(args.schema) as schemaFile:
|
|
schema = json.load(schemaFile)
|
|
|
|
for table in schema["tables"]:
|
|
for column in table["columns"]:
|
|
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'
|
|
|
|
for f in glob.glob(args.template):
|
|
print(f"process {f}")
|
|
tmpl = Template(file=f, searchList=[schema])
|
|
with open(f[:-5], 'w') as outFile:
|
|
outFile.write(str(tmpl))
|
|
|
|
|