Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] fix ModuleNotFoundError when packageName contains dots #2992

Merged
merged 5 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down Expand Up @@ -176,8 +176,6 @@ public void processOpts() {

if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
setPackageName("openapi_client");
}

if (additionalProperties.containsKey(CodegenConstants.PROJECT_NAME)) {
Expand All @@ -190,9 +188,7 @@ public void processOpts() {

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)) {
Expand Down Expand Up @@ -240,10 +236,22 @@ public void processOpts() {
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"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saigiridhar21 can you share more on changing __init_test.mustache to init.mustache?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 In line 248, I need an empty _init__.mustache. Here, we are already having an empty __init__test.mustache. So, instead of having two empty __init__ mustache files, I renamed __init__test.mustache to __init__.mustache and utilized it at both the places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. Didn't realise we've 2 templates for empty __init__

}

supportingFiles.add(new SupportingFile("api_client.mustache", packagePath(), "api_client.py"));
Expand Down