forked from loafle/openapi-generator-original
Add post processing to files generated by Python generators (#1072)
* add post process file to python generators * update python samples with yapf * revert sample change
This commit is contained in:
@@ -19,6 +19,7 @@ package org.openapitools.codegen.languages;
|
||||
|
||||
import io.swagger.v3.oas.models.media.ArraySchema;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.openapitools.codegen.CliOption;
|
||||
import org.openapitools.codegen.CodegenConfig;
|
||||
@@ -170,6 +171,11 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) {
|
||||
LOGGER.info("Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE=\"/usr/local/bin/yapf -i\"' (Linux/Mac)");
|
||||
}
|
||||
|
||||
Boolean excludeTests = false;
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.EXCLUDE_TESTS)) {
|
||||
@@ -709,4 +715,31 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
return input.replace("'''", "'_'_'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessFile(File file, String fileType) {
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
String pythonPostProcessFile = System.getenv("PYTHON_POST_PROCESS_FILE");
|
||||
if (StringUtils.isEmpty(pythonPostProcessFile)) {
|
||||
return; // skip if PYTHON_POST_PROCESS_FILE env variable is not defined
|
||||
}
|
||||
|
||||
// only process files with py extension
|
||||
if ("py".equals(FilenameUtils.getExtension(file.toString()))) {
|
||||
String command = pythonPostProcessFile + " " + file.toString();
|
||||
try {
|
||||
Process p = Runtime.getRuntime().exec(command);
|
||||
int exitValue = p.waitFor();
|
||||
if (exitValue != 0) {
|
||||
LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue);
|
||||
} else {
|
||||
LOGGER.info("Successfully executed: " + command);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import io.swagger.v3.oas.models.PathItem.HttpMethod;
|
||||
import io.swagger.v3.oas.models.media.ArraySchema;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.utils.ModelUtils;
|
||||
import org.slf4j.Logger;
|
||||
@@ -153,6 +154,11 @@ public class PythonFlaskConnexionServerCodegen extends DefaultCodegen implements
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) {
|
||||
LOGGER.info("Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE=\"/usr/local/bin/yapf -i\"' (Linux/Mac)");
|
||||
}
|
||||
|
||||
//apiTemplateFiles.clear();
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
|
||||
@@ -692,4 +698,31 @@ public class PythonFlaskConnexionServerCodegen extends DefaultCodegen implements
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessFile(File file, String fileType) {
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
String pythonPostProcessFile = System.getenv("PYTHON_POST_PROCESS_FILE");
|
||||
if (StringUtils.isEmpty(pythonPostProcessFile)) {
|
||||
return; // skip if PYTHON_POST_PROCESS_FILE env variable is not defined
|
||||
}
|
||||
|
||||
// only process files with py extension
|
||||
if ("py".equals(FilenameUtils.getExtension(file.toString()))) {
|
||||
String command = pythonPostProcessFile + " " + file.toString();
|
||||
try {
|
||||
Process p = Runtime.getRuntime().exec(command);
|
||||
int exitValue = p.waitFor();
|
||||
if (exitValue != 0) {
|
||||
LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue);
|
||||
} else {
|
||||
LOGGER.info("Successfully executed: " + command);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
3.2.2-SNAPSHOT
|
||||
3.3.0-SNAPSHOT
|
||||
@@ -6,7 +6,7 @@ Name | Type | Description | Notes
|
||||
**map_map_of_string** | **dict(str, dict(str, str))** | | [optional]
|
||||
**map_of_enum_string** | **dict(str, str)** | | [optional]
|
||||
**direct_map** | **dict(str, bool)** | | [optional]
|
||||
**indirect_map** | [**StringBooleanMap**](StringBooleanMap.md) | | [optional]
|
||||
**indirect_map** | **dict(str, bool)** | | [optional]
|
||||
|
||||
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ class MapTest(object):
|
||||
'map_map_of_string': 'dict(str, dict(str, str))',
|
||||
'map_of_enum_string': 'dict(str, str)',
|
||||
'direct_map': 'dict(str, bool)',
|
||||
'indirect_map': 'StringBooleanMap'
|
||||
'indirect_map': 'dict(str, bool)'
|
||||
}
|
||||
|
||||
attribute_map = {
|
||||
@@ -138,7 +138,7 @@ class MapTest(object):
|
||||
|
||||
|
||||
:return: The indirect_map of this MapTest. # noqa: E501
|
||||
:rtype: StringBooleanMap
|
||||
:rtype: dict(str, bool)
|
||||
"""
|
||||
return self._indirect_map
|
||||
|
||||
@@ -148,7 +148,7 @@ class MapTest(object):
|
||||
|
||||
|
||||
:param indirect_map: The indirect_map of this MapTest. # noqa: E501
|
||||
:type: StringBooleanMap
|
||||
:type: dict(str, bool)
|
||||
"""
|
||||
|
||||
self._indirect_map = indirect_map
|
||||
|
||||
Reference in New Issue
Block a user