def testOpenApiSchema(self): modelFile = 'resources/models/json/yacg_openapi_paths.json' modelFileExists = os.path.isfile(modelFile) self.assertTrue('model file exists: ' + modelFile, modelFileExists) model = config.Model() model.schema = modelFile modelTypes = getModelFromJson(model, []) templateFile = 'resources/templates/examples/pythonBeans.mako' template = Template(filename=templateFile) templateFileExists = os.path.isfile(modelFile) self.assertTrue('template file exists: ' + templateFile, templateFileExists) templateParameterDict = {} templateParameterDict['baseModelDomain'] = 'yacg.model.openapi' blackListList = [] # all types from the main model should be igrnored ... # blacklisted by domain example entryTag = config.BlackWhiteListEntry() entryTag.name = 'yacgCore' entryTag.type = config.BlackWhiteListEntryTypeEnum.DOMAIN blackListList.append(entryTag) trimmedTypes = generatorHelperFuncs.trimModelTypes(modelTypes, blackListList, None) renderResult = template.render( modelTypes=trimmedTypes, templateParameters=templateParameterDict) self.assertIsNotNone(renderResult) testOutputFile = "tmp/openapi.py" f = open(testOutputFile, "w+") f.write(renderResult) f.close()
def renderSingleFileTemplate(modelTypes, templateFile, output, templateParameterList, blackList, whiteList): """render a template that produce one output file. This file contains content based on every type of the model. A possible example is the creation of a plantUml diagram from a model Keyword arguments: modelTypes -- list of types that build the model, list of yacg.model.model.Type instances (mostly Enum- and ComplexTypes) templateFile -- template file to use output -- output file to create templateParameterList -- list of yacg.model.config.TemplateParam instances, these parameters are passed to the template blackList -- list of yacg.model.config.BlackWhiteListEntry instances to describe types that should be excluded whiteList -- list of yacg.model.config.BlackWhiteListEntry instances to describe types that should be included """ template = Template(filename=templateFile) modelTypesToUse = generatorHelper.trimModelTypes(modelTypes, blackList, whiteList) templateParameterDict = {} for templateParam in templateParameterList: templateParameterDict[templateParam.name] = templateParam.value renderResult = template.render(modelTypes=modelTypesToUse, availableTypes=modelTypes, templateParameters=templateParameterDict) if (output == 'stdout'): print(renderResult) else: outputFile = output f = open(outputFile, "w+") f.write(renderResult) f.close()
def renderSingleFileTemplate(modelTypes, blackList, whiteList, singleFileTask): """render a template that produce one output file. This file contains content based on every type of the model. A possible example is the creation of a plantUml diagram from a model Keyword arguments: modelTypes -- list of types that build the model, list of yacg.model.model.Type instances (mostly Enum- and ComplexTypes) templateFile -- template file to use blackList -- list of yacg.model.config.BlackWhiteListEntry instances to describe types that should be excluded whiteList -- list of yacg.model.config.BlackWhiteListEntry instances to describe types that should be included singleFileTask - configuration for the single file task """ template_dir = os.path.dirname(singleFileTask.template) if not template_dir in sys.path: sys.path.append(template_dir) template = Template(filename=singleFileTask.template) modelTypesToUse = generatorHelper.trimModelTypes(modelTypes, blackList, whiteList) templateParameterDict = {} for templateParam in singleFileTask.templateParams: templateParameterDict[templateParam.name] = templateParam.value renderResult = template.render( modelTypes=modelTypesToUse, availableTypes=modelTypes, templateParameters=templateParameterDict) if (singleFileTask.destFile == 'stdout'): print(renderResult) else: outputFile = singleFileTask.destFile outputDir = os.path.dirname(outputFile) if not os.path.exists(outputDir): os.mkdir(outputDir) f = open(outputFile, "w+") f.write(renderResult) f.close()
def testBackListedPathTypes(self): modelFile = 'resources/models/yaml/userConfig.swagger.yaml' model = config.Model() model.schema = modelFile modelTypes = getModelFromYaml(model, []) blackList = [] blackListEntry = BlackWhiteListEntry() blackListEntry.name = 'PathType' blackListEntry.type = BlackWhiteListEntryTypeEnum.TYPETYPE blackList.append(blackListEntry) modelTypesToUse = generatorHelper.trimModelTypes(modelTypes, blackList, ()) self.assertEqual(18, len(modelTypesToUse))
def testWhiteListedPathTypes(self): modelFile = 'resources/models/yaml/userConfig.swagger.yaml' modelFileExists = os.path.isfile(modelFile) self.assertTrue('model file exists: ' + modelFile, modelFileExists) model = config.Model() model.schema = modelFile modelTypes = getModelFromYaml(model, []) whiteList = [] whiteListEntry = BlackWhiteListEntry() whiteListEntry.name = 'PathType' whiteListEntry.type = BlackWhiteListEntryTypeEnum.TYPETYPE whiteList.append(whiteListEntry) modelTypesToUse = generatorHelper.trimModelTypes(modelTypes, (), whiteList) self.assertEqual(2, len(modelTypesToUse))
def testGetPythonValueForTypeStringType(self): modelFile = 'tests/resources/models/json/examples/swagger_v2_example.json' modelFileExists = os.path.isfile(modelFile) self.assertTrue('model file exists: ' + modelFile, modelFileExists) model = config.Model() model.schema = modelFile ignorePathTypes = False modelTypes = getModelFromJson(model, [], ignorePathTypes) whiteList = [] whiteListEntry = BlackWhiteListEntry() whiteListEntry.name = 'PathType' whiteListEntry.type = BlackWhiteListEntryTypeEnum.TYPETYPE whiteList.append(whiteListEntry) pathTypes = generatorHelper.trimModelTypes(modelTypes, (), whiteList) self.assertEqual(14, len(pathTypes)) multiFileTypes = swaggerFilterByOperationId(pathTypes) self.assertEqual(20, len(multiFileTypes))
def __prepareMultiFileTask(multiFileTask, modelTypes, blackList, whiteList): template = Template(filename=multiFileTask.template) modelTypesToUse = generatorHelper.trimModelTypes(modelTypes, blackList, whiteList) templateParameterDict = {} for templateParam in multiFileTask.templateParams: templateParameterDict[templateParam.name] = templateParam.value if multiFileTask.destDir is None: multiFileTask.destDir = '.' if multiFileTask.destFilePrefix is None: multiFileTask.destFilePrefix = '' if multiFileTask.destFilePostfix is None: multiFileTask.destFilePostfix = '' if multiFileTask.destFileExt is None: multiFileTask.destFileExt = 'txt' Path(multiFileTask.destDir).mkdir(parents=True, exist_ok=True) return template, modelTypesToUse, templateParameterDict
def renderMultiFileTemplate( modelTypes, blackList, whiteList, multiFileTask): """render a template that produce one output file. This file contains content based on every type of the model. A possible example is the creation of a plantUml diagram from a model Keyword arguments: modelTypes -- list of types that build the model, list of yacg.model.model.Type instances (mostly Enum- and ComplexTypes) blackList -- list of yacg.model.config.BlackWhiteListEntry instances to describe types that should be excluded whiteList -- list of yacg.model.config.BlackWhiteListEntry instances to describe types that should be included multiFileTask -- container object with the parameters """ template = Template(filename=multiFileTask.template) modelTypesToUse = generatorHelper.trimModelTypes(modelTypes, blackList, whiteList) templateParameterDict = {} for templateParam in multiFileTask.templateParams: templateParameterDict[templateParam.name] = templateParam.value if multiFileTask.destDir is None: multiFileTask.destDir = '.' if multiFileTask.destFilePrefix is None: multiFileTask.destFilePrefix = '' if multiFileTask.destFilePostfix is None: multiFileTask.destFilePostfix = '' if multiFileTask.destFileExt is None: multiFileTask.destFileExt = 'txt' Path(multiFileTask.destDir).mkdir(parents=True, exist_ok=True) if multiFileTask.fileFilterType == MultiFileTaskFileFilterTypeEnum.OPENAPIOPERATIONID: __renderOneFilePerOpenApiOperationId( modelTypesToUse, modelTypes, templateParameterDict, template, multiFileTask) else: __renderOneFilePerType( modelTypesToUse, modelTypes, templateParameterDict, template, multiFileTask)
def renderRandomData(modelTypes, blackList, whiteList, randomDataTask): """render a template that produce one output file. This file contains content based on every type of the model. A possible example is the creation of a plantUml diagram from a model Keyword arguments: modelTypes -- list of types that build the model, list of yacg.model.model.Type instances (mostly Enum- and ComplexTypes) blackList -- list of yacg.model.config.BlackWhiteListEntry instances to describe types that should be excluded whiteList -- list of yacg.model.config.BlackWhiteListEntry instances to describe types that should be included randomDataTask -- container object with the parameters """ modelTypesToUse = generatorHelper.trimModelTypes(modelTypes, blackList, whiteList) Path(randomDataTask.destDir).mkdir(parents=True, exist_ok=True) # TODO create dict with random data randomDataDict, keyValueDict = __prepareTypeObjects( modelTypesToUse, randomDataTask) __fillRandomValues(modelTypesToUse, randomDataTask, randomDataDict, keyValueDict) __writeRandomValues(randomDataTask, randomDataDict) return randomDataDict