Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

v2 use modern python packaging #115

Merged
merged 8 commits into from
Jan 2, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ protected Map<String, Schema> getModelNameToSchemaCache() {

@Override
public String packageName() {
// used to generate imports
return packageName;
}

Expand Down Expand Up @@ -4063,7 +4064,7 @@ public String responseFilename(String templateName, String jsonPath) {
// #/paths/somePath/get/responses/200 -> length 6
String pathModuleName = toPathFilename(ModelUtils.decodeSlashes(pathPieces[2]));
String httpVerb = pathPieces[3];
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + pathPieces[5] + File.separatorChar + writtenFilename;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + pathPieces[5] + File.separatorChar + writtenFilename;
}
return null;
}
Expand All @@ -4080,7 +4081,7 @@ public String requestBodyFilename(String templateName, String jsonPath) {
// #/paths/somePath/get/requestBody -> length 5
String pathModuleName = toPathFilename(ModelUtils.decodeSlashes(pathPieces[2]));
String httpVerb = pathPieces[3];
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "request_body" + File.separatorChar + writtenFilename;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "request_body" + File.separatorChar + writtenFilename;
}
return null;
}
Expand All @@ -4098,7 +4099,7 @@ public String parameterFilename(String templateName, String jsonPath) {
String pathModuleName = toPathFilename(ModelUtils.decodeSlashes(pathPieces[2]));
String httpVerb = pathPieces[3];
String i = pathPieces[5];
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + writtenFilename;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + writtenFilename;
}
return null;
}
Expand All @@ -4121,7 +4122,7 @@ public String headerFilename(String templateName, String jsonPath) {
String httpVerb = pathPieces[3];
String code = pathPieces[5];
String headerModule = toHeaderFilename(pathPieces[7]);
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + writtenFilename;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + writtenFilename;
}
return null;
}
Expand Down Expand Up @@ -4177,36 +4178,36 @@ public String schemaFilename(String templateName, String jsonPath) {
if (pathPieces.length == 7) {
// #/paths/somePath/get/parameters/1/schema -> length 7
String i = pathPieces[5];
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + getKey(pathPieces[6]).getSnakeCaseName() + suffix;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + getKey(pathPieces[6]).getSnakeCaseName() + suffix;
} else if (pathPieces.length == 8) {
// #/paths/somePath/get/requestBody/content/application-json/schema -> length 8
String contentType = ModelUtils.decodeSlashes(pathPieces[6]);
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "request_body" + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "request_body" + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
} else if (pathPieces.length == 9) {
String parametersOrResponses = pathPieces[4];
if (parametersOrResponses.equals("parameters")) {
// #/paths/somePath/get/parameters/1/content/application-json/schema -> length 9
String i = pathPieces[5];
String contentType = ModelUtils.decodeSlashes(pathPieces[7]);
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + toParameterFilename(i) + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
} else if (parametersOrResponses.equals("responses")) {
String contentOrHeaders = pathPieces[6];
String code = pathPieces[5];
if (contentOrHeaders.equals("headers")) {
// #/paths/somePath/get/responses/200/headers/someHeader/schema -> length 9
String headerModule = toHeaderFilename(pathPieces[7]);
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + getKey(pathPieces[8]).getSnakeCaseName() + suffix;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + getKey(pathPieces[8]).getSnakeCaseName() + suffix;
}
// #/paths/somePath/get/responses/200/content/application-json/schema -> length 9
String contentType = ModelUtils.decodeSlashes(pathPieces[7]);
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
}
} else if (pathPieces.length == 11) {
// #/paths/somePath/get/responses/200/headers/someHeader/content/application-json/schema -> length 11
String code = pathPieces[5];
String headerModule = toHeaderFilename(pathPieces[7]);
String contentType = ModelUtils.decodeSlashes(pathPieces[9]);
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "paths" + File.separatorChar + pathModuleName + File.separatorChar + httpVerb + File.separatorChar + "response_for_" + code + File.separatorChar + headerModule + File.separatorChar + getKey(contentType).getSnakeCaseName() + suffix;
}
}
return null;
Expand Down Expand Up @@ -5555,12 +5556,12 @@ private List<CodegenSchema> getComposedProperties(List<Schema> xOfCollection, St

@Override
public String requestBodyFileFolder(String componentName) {
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "components" + File.separatorChar + "request_bodies" + toRequestBodyFilename(componentName);
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "components" + File.separatorChar + "request_bodies" + toRequestBodyFilename(componentName);
}

@Override
public String responseFileFolder(String componentName) {
return outputFolder + File.separatorChar + packageName() + File.separatorChar + "components" + File.separatorChar + "responses" + File.separatorChar + toResponseModuleName(componentName);
return outputFolder + File.separatorChar + packagePath() + File.separatorChar + "components" + File.separatorChar + "responses" + File.separatorChar + toResponseModuleName(componentName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,6 @@ public void processOpts() {
generateSourceCodeOnly = Boolean.valueOf(additionalProperties.get(CodegenConstants.SOURCECODEONLY_GENERATION).toString());
}

if (generateSourceCodeOnly) {
// tests in test
testFolder = packagePath() + File.separatorChar + testFolder;
// api docs in <package>/docs/apis/tags/
apiDocPath = packagePath() + File.separatorChar + apiDocPath;
// model docs in <package>/docs/models/
modelDocPath = packagePath() + File.separatorChar + modelDocPath;
}
// make api and model doc path available in templates
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);
Expand Down Expand Up @@ -410,13 +402,12 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("tox." + templateExtension, "", "tox.ini"));
supportingFiles.add(new SupportingFile("test-requirements." + templateExtension, "", "test-requirements.txt"));
supportingFiles.add(new SupportingFile("requirements." + templateExtension, "", "requirements.txt"));
supportingFiles.add(new SupportingFile("setup_cfg." + templateExtension, "", "setup.cfg"));

supportingFiles.add(new SupportingFile("git_push.sh." + templateExtension, "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore." + templateExtension, "", ".gitignore"));
supportingFiles.add(new SupportingFile("travis." + templateExtension, "", ".travis.yml"));
supportingFiles.add(new SupportingFile("gitlab-ci." + templateExtension, "", ".gitlab-ci.yml"));
supportingFiles.add(new SupportingFile("setup." + templateExtension, "", "setup.py"));
supportingFiles.add(new SupportingFile("pyproject." + templateExtension, "", "pyproject.toml"));
}
supportingFiles.add(new SupportingFile("configuration." + templateExtension, packagePath(), "configuration.py"));
supportingFiles.add(new SupportingFile("__init__package." + templateExtension, packagePath(), "__init__.py"));
Expand Down Expand Up @@ -1865,8 +1856,9 @@ public void setPackageUrl(String packageUrl) {
}

public String packagePath() {
return packageName.replace('.', File.separatorChar);
}
// src is needed for modern packaging per
// https://packaging.python.org/en/latest/tutorials/packaging-projects/
return "src" + File.separatorChar + packageName.replace('.', File.separatorChar); }

/**
* Generate Python package name from String `packageName`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ import {{{packageName}}}
Install via [Setuptools](http://pypi.python.org/pypi/setuptools).

```sh
python setup.py install --user
python -m pip install . --user
```
(or `sudo python setup.py install` to install the package for all users)
(or `python -m pip install .` to install the package for all users)

Then import the package:
```python
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

[build-system]
requires = [
{{> required_libraries quoted=true }}
]
build-backend = "setuptools.build_meta"

[project]
name = "{{{projectName}}}"
version = "{{packageVersion}}"
{{#with apiInfo}}
authors = [
{ name="{{#if infoName}}{{infoName}}{{else}}OpenAPI JSON Schema Generator community{{/if}}"{{#if infoEmail}}, email="{{infoEmail}}"{{/if}} },
]
description = "{{appName}}"
readme = "README.md"
requires-python = "{{{generatorLanguageVersion}}}"
classifiers = [
"Programming Language :: Python :: 3",
{{#if licenseInfo}}"License :: {{licenseInfo}}",{{/if}}
"Operating System :: OS Independent",
"Topic :: Software Development :: Code Generators"
]

{{#or packageUrl infoUrl}}
[project.urls]
{{#if packageUrl}}"Homepage" = "{{{packageUrl}}}"{{/if}}
{{#if infoUrl}}"Bug Tracker" = "{{{infoUrl}}}"{{/if}}
{{/or}}
{{/with}}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{{#if quoted}}"{{/if}}pycryptodome >= 3.9.0{{#if quoted}}",{{/if}}
{{/if}}
{{#if quoted}}"{{/if}}python-dateutil ~= 2.7.0{{#if quoted}}",{{/if}}
{{#if quoted}}"{{/if}}setuptools >= 21.0.0{{#if quoted}}",{{/if}}
{{#if quoted}}"{{/if}}setuptools >= 61.0{{#if quoted}}",{{/if}}
{{#if tornado}}
{{#if quoted}}"{{/if}}tornado >= 4.2{{#if quoted}}",{{/if}}
{{/if}}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,10 @@ public void testProcessUserDefinedTemplatesWithConfig() throws IOException {
// Assert.assertTrue(new File(output, "sampleConfig.json").exists());

// Generator should report api_client.py as a generated file
TestUtils.ensureContainsFile(files, output, "io/something/api_client.py");
TestUtils.ensureContainsFile(files, output, "src/io/something/api_client.py");

// Generated file should exist on the filesystem after generation
File apiClient = new File(output, "io/something/api_client.py");
File apiClient = new File(output, "src/io/something/api_client.py");
Assert.assertTrue(apiClient.exists());

// Generated file should contain our custom packageName
Expand Down
Loading