[Python] ModuleNotFoundError when packagename contains dots (#2992)

* feat(python): Support package names with dots

* feat(python): Fixing tests

* feat(python): Adding comment

* fix(python): Fixing indentation

* fix(python): Fixing indentation
This commit is contained in:
Sai Giridhar P 2019-05-29 17:08:42 +05:30 committed by William Cheng
parent 44e4dc3ff4
commit 7756296c25
2 changed files with 16 additions and 8 deletions

View File

@ -41,8 +41,8 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
public static final String PACKAGE_URL = "packageUrl";
public static final String DEFAULT_LIBRARY = "urllib3";
protected String packageName; // e.g. petstore_api
protected String packageVersion;
protected String packageName = "openapi_client";
protected String packageVersion = "1.0.0";
protected String projectName; // for setup.py, e.g. petstore-api
protected String packageUrl;
protected String apiDocPath = "docs/";
@ -177,8 +177,6 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
setPackageName("openapi_client");
}
if (additionalProperties.containsKey(CodegenConstants.PROJECT_NAME)) {
@ -191,9 +189,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) {
setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION));
} else {
setPackageVersion("1.0.0");
}
}
Boolean generateSourceCodeOnly = false;
if (additionalProperties.containsKey(CodegenConstants.SOURCECODEONLY_GENERATION)) {
@ -241,10 +237,22 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
supportingFiles.add(new SupportingFile("__init__package.mustache", packagePath(), "__init__.py"));
supportingFiles.add(new SupportingFile("__init__model.mustache", packagePath() + File.separatorChar + modelPackage, "__init__.py"));
supportingFiles.add(new SupportingFile("__init__api.mustache", packagePath() + File.separatorChar + apiPackage, "__init__.py"));
// If the package name consists of dots(openapi.client), then we need to create the directory structure like openapi/client with __init__ files.
String[] packageNameSplits = packageName.split("\\.");
String currentPackagePath = "";
for (int i = 0; i < packageNameSplits.length-1; i++) {
if (i > 0) {
currentPackagePath = currentPackagePath + File.separatorChar;
}
currentPackagePath = currentPackagePath + packageNameSplits[i];
supportingFiles.add(new SupportingFile("__init__.mustache", currentPackagePath, "__init__.py"));
}
supportingFiles.add(new SupportingFile("exceptions.mustache", packagePath(), "exceptions.py"));
if (Boolean.FALSE.equals(excludeTests)) {
supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py"));
supportingFiles.add(new SupportingFile("__init__.mustache", testFolder, "__init__.py"));
}
supportingFiles.add(new SupportingFile("api_client.mustache", packagePath(), "api_client.py"));