Skip to content

Commit 74f70a1

Browse files
kenjones-ciscowing328
authored andcommitted
[python-client] Modify python templates to resolve linting errors (#6839)
The linting results for the generated samples are as follows where the first number is the BEFORE and the second is AFTER. pyclient 7714 vs. 120 pyclient3 7717 vs. 120 pyclient3-asyncio 7584 vs. 120 pyclient-tornado 7633 vs. 120 pyclient3-tornado 7633 vs. 120 For the complete details please see the following gist. https://gist.github.com/kenjones-cisco/2eb69a7e8db75e9fd53789f01570d9f2 Enforces linting for python clients by running flake8 for the generated python client.
1 parent eba4aff commit 74f70a1

File tree

339 files changed

+10156
-11745
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

339 files changed

+10156
-11745
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public PythonClientCodegen() {
6767
languageSpecificPrimitives.add("int");
6868
languageSpecificPrimitives.add("float");
6969
languageSpecificPrimitives.add("list");
70+
languageSpecificPrimitives.add("dict");
7071
languageSpecificPrimitives.add("bool");
7172
languageSpecificPrimitives.add("str");
7273
languageSpecificPrimitives.add("datetime");
@@ -189,21 +190,16 @@ public void processOpts() {
189190
setPackageUrl((String) additionalProperties.get(PACKAGE_URL));
190191
}
191192

192-
String swaggerFolder = packageName;
193-
194-
modelPackage = swaggerFolder + File.separatorChar + "models";
195-
apiPackage = swaggerFolder + File.separatorChar + "apis";
196-
197193
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
198194

199195
supportingFiles.add(new SupportingFile("tox.mustache", "", "tox.ini"));
200196
supportingFiles.add(new SupportingFile("test-requirements.mustache", "", "test-requirements.txt"));
201197
supportingFiles.add(new SupportingFile("requirements.mustache", "", "requirements.txt"));
202198

203-
supportingFiles.add(new SupportingFile("configuration.mustache", swaggerFolder, "configuration.py"));
204-
supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFolder, "__init__.py"));
205-
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py"));
206-
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
199+
supportingFiles.add(new SupportingFile("configuration.mustache", packageName, "configuration.py"));
200+
supportingFiles.add(new SupportingFile("__init__package.mustache", packageName, "__init__.py"));
201+
supportingFiles.add(new SupportingFile("__init__model.mustache", packageName + File.separatorChar + modelPackage, "__init__.py"));
202+
supportingFiles.add(new SupportingFile("__init__api.mustache", packageName + File.separatorChar + apiPackage, "__init__.py"));
207203

208204
if(Boolean.FALSE.equals(excludeTests)) {
209205
supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py"));
@@ -212,23 +208,42 @@ public void processOpts() {
212208
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
213209
supportingFiles.add(new SupportingFile("travis.mustache", "", ".travis.yml"));
214210
supportingFiles.add(new SupportingFile("setup.mustache", "", "setup.py"));
215-
supportingFiles.add(new SupportingFile("api_client.mustache", swaggerFolder, "api_client.py"));
211+
supportingFiles.add(new SupportingFile("api_client.mustache", packageName, "api_client.py"));
216212

217213
if ("asyncio".equals(getLibrary())) {
218-
supportingFiles.add(new SupportingFile("asyncio/rest.mustache", swaggerFolder, "rest.py"));
214+
supportingFiles.add(new SupportingFile("asyncio/rest.mustache", packageName, "rest.py"));
219215
additionalProperties.put("asyncio", "true");
220216
} else if ("tornado".equals(getLibrary())) {
221-
supportingFiles.add(new SupportingFile("tornado/rest.mustache", swaggerFolder, "rest.py"));
217+
supportingFiles.add(new SupportingFile("tornado/rest.mustache", packageName, "rest.py"));
222218
additionalProperties.put("tornado", "true");
223219
} else {
224-
supportingFiles.add(new SupportingFile("rest.mustache", swaggerFolder, "rest.py"));
220+
supportingFiles.add(new SupportingFile("rest.mustache", packageName, "rest.py"));
225221
}
222+
223+
modelPackage = packageName + "." + modelPackage;
224+
apiPackage = packageName + "." + apiPackage;
225+
226226
}
227227

228228
private static String dropDots(String str) {
229229
return str.replaceAll("\\.", "_");
230230
}
231231

232+
@Override
233+
public String toModelImport(String name) {
234+
String modelImport;
235+
if (StringUtils.startsWithAny(name,"import", "from")) {
236+
modelImport = name;
237+
} else {
238+
modelImport = "from ";
239+
if (!"".equals(modelPackage())) {
240+
modelImport += modelPackage() + ".";
241+
}
242+
modelImport += toModelFilename(name)+ " import " + name;
243+
}
244+
return modelImport;
245+
}
246+
232247
@Override
233248
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
234249
// process enum in models

modules/swagger-codegen/src/main/resources/flaskConnexion/model.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from datetime import date, datetime # noqa: F401
66
from typing import List, Dict # noqa: F401
77

88
from {{modelPackage}}.base_model_ import Model
9-
{{#imports}}{{import}} # noqa: E501
9+
{{#imports}}{{import}} # noqa: F401,E501
1010
{{/imports}}
1111
from {{packageName}} import util
1212

@@ -94,15 +94,15 @@ class {{classname}}(Model):
9494
if not set({{{name}}}).issubset(set(allowed_values)):
9595
raise ValueError(
9696
"Invalid values for `{{{name}}}` [{0}], must be a subset of [{1}]" # noqa: E501
97-
.format(", ".join(map(str, set({{{name}}})-set(allowed_values))), # noqa: E501
97+
.format(", ".join(map(str, set({{{name}}}) - set(allowed_values))), # noqa: E501
9898
", ".join(map(str, allowed_values)))
9999
)
100100
{{/isListContainer}}
101101
{{#isMapContainer}}
102102
if not set({{{name}}}.keys()).issubset(set(allowed_values)):
103103
raise ValueError(
104104
"Invalid keys in `{{{name}}}` [{0}], must be a subset of [{1}]" # noqa: E501
105-
.format(", ".join(map(str, set({{{name}}}.keys())-set(allowed_values))), # noqa: E501
105+
.format(", ".join(map(str, set({{{name}}}.keys()) - set(allowed_values))), # noqa: E501
106106
", ".join(map(str, allowed_values)))
107107
)
108108
{{/isMapContainer}}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from __future__ import absolute_import
22

3+
# flake8: noqa
4+
35
# import apis into api package
4-
{{#apiInfo}}
5-
{{#apis}}
6-
from .{{classVarName}} import {{classname}}
7-
{{/apis}}
8-
{{/apiInfo}}
6+
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classVarName}} import {{classname}}
7+
{{/apis}}{{/apiInfo}}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# coding: utf-8
22

3+
# flake8: noqa
34
{{>partial_header}}
45

56
from __future__ import absolute_import
67

78
# import models into model package
8-
{{#models}}{{#model}}from .{{classFilename}} import {{classname}}{{/model}}
9+
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}{{/model}}
910
{{/models}}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# coding: utf-8
22

3+
# flake8: noqa
4+
35
{{>partial_header}}
46

57
from __future__ import absolute_import
68

7-
# import models into sdk package
8-
{{#models}}{{#model}}from .models.{{classFilename}} import {{classname}}
9-
{{/model}}{{/models}}
109
# import apis into sdk package
11-
{{#apiInfo}}{{#apis}}from .apis.{{classVarName}} import {{classname}}
10+
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classVarName}} import {{classname}}
1211
{{/apis}}{{/apiInfo}}
1312
# import ApiClient
14-
from .api_client import ApiClient
15-
16-
from .configuration import Configuration
13+
from {{packageName}}.api_client import ApiClient
14+
from {{packageName}}.configuration import Configuration
15+
# import models into sdk package
16+
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}
17+
{{/model}}{{/models}}

0 commit comments

Comments
 (0)