This commit is contained in:
2021-11-18 19:27:39 +01:00
commit e616a7cb60
7 changed files with 229 additions and 0 deletions

90
generate.py Normal file
View File

@ -0,0 +1,90 @@
import yaml
from Cheetah.Template import Template
import glob
import argparse
import os
from yaml.loader import SafeLoader
from generateHelper import CsOperationNameConverter, OpenApiExtractRefType
parser = argparse.ArgumentParser(description="generate.py")
parser.add_argument('--api', '-a',
help='API definition file. Default: openapi.yaml in the current folder.',
required=False,
default='./openapi.yaml')
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.""",
required=False,
default="**/*.tmpl")
args = parser.parse_args()
with open(args.api) as schemaFile:
schema = yaml.load(schemaFile, Loader=SafeLoader)
schema["GENERATED_SQL_COMMENT"] = """
-- ----------------------------------------
-- THIS FILE HAS BEEN GENERATED
-- DO NOT EDIT MANUALLY
-- ----------------------------------------
"""
schema["GENERATED_PYTHON_COMMENT"] = """
# -----------------------------------------
# THIS FILE HAS BEEN GENERATED
# DO NOT EDIT MANUALLY
# -----------------------------------------
"""
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
// -----------------------------------------
"""
schema["GENERATED_CS_COMMENT"] = """
// -----------------------------------------
// THIS FILE HAS BEEN GENERATED
// DO NOT EDIT MANUALLY
// -----------------------------------------
"""
packageName = os.environ["PACKAGE_NAME"]
schema["env"] = { "packagename": packageName }
operations = []
for path in schema['paths'].values():
for (method, operation) in path.items():
#print(f"{method=}")
#print(f"{CsOperationNameConverter(operation['operationId'])=}")
content = operation['responses'][200]['content']['application/json']['schema']
if ('type' in content) and (content['type'] == 'array'):
isList = True
typ = OpenApiExtractRefType(content['items']['$ref'])
else:
isList = False
typ = OpenApiExtractRefType(content['$ref'])
#print(f"{content=}")
#print(f"{typ=}")
operations.append({
'method':method,
'func':CsOperationNameConverter(operation['operationId']),
'isList': isList,
'type': typ
})
#print(f"{operations=}")
schema["operations"] = operations
for f in glob.glob(args.template, recursive=True):
print(f"process {f}")
tmpl = Template(file=f, searchList=[schema])
with open(f[:-5], 'w') as outFile:
outFile.write(str(tmpl))