mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 03:18:53 +00:00
Merge pull request #864 from geekerzp/python-cli-option
[Python] Enabling cli config options for python generator
This commit is contained in:
commit
e60bd564cb
@ -1,5 +1,6 @@
|
|||||||
package io.swagger.codegen.languages;
|
package io.swagger.codegen.languages;
|
||||||
|
|
||||||
|
import io.swagger.codegen.CliOption;
|
||||||
import io.swagger.codegen.CodegenConfig;
|
import io.swagger.codegen.CodegenConfig;
|
||||||
import io.swagger.codegen.CodegenType;
|
import io.swagger.codegen.CodegenType;
|
||||||
import io.swagger.codegen.DefaultCodegen;
|
import io.swagger.codegen.DefaultCodegen;
|
||||||
@ -13,25 +14,19 @@ import java.util.Arrays;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig {
|
public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||||
protected String module = "SwaggerPetstore";
|
protected String packageName = null;
|
||||||
protected String invokerPackage;
|
protected String packageVersion = null;
|
||||||
protected String eggPackage;
|
|
||||||
|
|
||||||
public PythonClientCodegen() {
|
public PythonClientCodegen() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
eggPackage = module + "-python";
|
modelPackage = "models";
|
||||||
|
apiPackage = "api";
|
||||||
invokerPackage = eggPackage + File.separatorChar + module;
|
|
||||||
|
|
||||||
outputFolder = "generated-code" + File.separatorChar + "python";
|
outputFolder = "generated-code" + File.separatorChar + "python";
|
||||||
modelTemplateFiles.put("model.mustache", ".py");
|
modelTemplateFiles.put("model.mustache", ".py");
|
||||||
apiTemplateFiles.put("api.mustache", ".py");
|
apiTemplateFiles.put("api.mustache", ".py");
|
||||||
templateDir = "python";
|
templateDir = "python";
|
||||||
|
|
||||||
apiPackage = invokerPackage + File.separatorChar + "apis";
|
|
||||||
modelPackage = invokerPackage + File.separatorChar + "models";
|
|
||||||
|
|
||||||
languageSpecificPrimitives.clear();
|
languageSpecificPrimitives.clear();
|
||||||
languageSpecificPrimitives.add("int");
|
languageSpecificPrimitives.add("int");
|
||||||
languageSpecificPrimitives.add("float");
|
languageSpecificPrimitives.add("float");
|
||||||
@ -60,14 +55,43 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
"print", "class", "exec", "in", "raise", "continue", "finally", "is",
|
"print", "class", "exec", "in", "raise", "continue", "finally", "is",
|
||||||
"return", "def", "for", "lambda", "try"));
|
"return", "def", "for", "lambda", "try"));
|
||||||
|
|
||||||
additionalProperties.put("module", module);
|
cliOptions.clear();
|
||||||
|
cliOptions.add(new CliOption("packageName", "python package name (convension: under_score), default: swagger_client"));
|
||||||
|
cliOptions.add(new CliOption("packageVersion", "python package version, default: 1.0.0"));
|
||||||
|
}
|
||||||
|
|
||||||
supportingFiles.add(new SupportingFile("README.mustache", eggPackage, "README.md"));
|
@Override
|
||||||
supportingFiles.add(new SupportingFile("setup.mustache", eggPackage, "setup.py"));
|
public void processOpts() {
|
||||||
supportingFiles.add(new SupportingFile("api_client.mustache", invokerPackage, "api_client.py"));
|
super.processOpts();
|
||||||
supportingFiles.add(new SupportingFile("rest.mustache", invokerPackage, "rest.py"));
|
|
||||||
supportingFiles.add(new SupportingFile("configuration.mustache", invokerPackage, "configuration.py"));
|
if (additionalProperties.containsKey("packageName")) {
|
||||||
supportingFiles.add(new SupportingFile("__init__package.mustache", invokerPackage, "__init__.py"));
|
setPackageName((String) additionalProperties.get("packageName"));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
setPackageName("swagger_client");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (additionalProperties.containsKey("packageVersion")) {
|
||||||
|
setPackageVersion((String) additionalProperties.get("packageVersion"));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
setPackageVersion("1.0.0");
|
||||||
|
}
|
||||||
|
|
||||||
|
additionalProperties.put("packageName", packageName);
|
||||||
|
additionalProperties.put("packageVersion", packageVersion);
|
||||||
|
|
||||||
|
String swaggerFolder = packageName;
|
||||||
|
|
||||||
|
modelPackage = swaggerFolder + File.separatorChar + "models";
|
||||||
|
apiPackage = swaggerFolder + File.separatorChar + "apis";
|
||||||
|
|
||||||
|
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||||
|
supportingFiles.add(new SupportingFile("setup.mustache", "", "setup.py"));
|
||||||
|
supportingFiles.add(new SupportingFile("api_client.mustache", swaggerFolder, "api_client.py"));
|
||||||
|
supportingFiles.add(new SupportingFile("rest.mustache", swaggerFolder, "rest.py"));
|
||||||
|
supportingFiles.add(new SupportingFile("configuration.mustache", swaggerFolder, "configuration.py"));
|
||||||
|
supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFolder, "__init__.py"));
|
||||||
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py"));
|
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py"));
|
||||||
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
|
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
|
||||||
}
|
}
|
||||||
@ -225,4 +249,23 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
return underscore(operationId);
|
return underscore(operationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPackageName(String packageName) {
|
||||||
|
this.packageName = packageName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPackageVersion(String packageVersion) {
|
||||||
|
this.packageVersion = packageVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate Python package name from String `packageName`
|
||||||
|
*
|
||||||
|
* (PEP 0008) Python packages should also have short, all-lowercase names,
|
||||||
|
* although the use of underscores is discouraged.
|
||||||
|
*/
|
||||||
|
public String generatePackageName(String packageName) {
|
||||||
|
return underscore(packageName.replaceAll("[^\\w]+", ""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import sys
|
import sys
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
|
NAME = "{{packageName}}"
|
||||||
|
VERSION = "{{packageVersion}}"
|
||||||
|
|
||||||
{{#apiInfo}}{{#apis}}{{^hasMore}}
|
{{#apiInfo}}{{#apis}}{{^hasMore}}
|
||||||
|
|
||||||
# To install the library, open a Terminal shell, then run this
|
# To install the library, open a Terminal shell, then run this
|
||||||
@ -15,8 +18,8 @@ from setuptools import setup, find_packages
|
|||||||
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi"]
|
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi"]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="{{module}}",
|
name=NAME,
|
||||||
version="{{version}}",
|
version=VERSION,
|
||||||
description="{{appName}}",
|
description="{{appName}}",
|
||||||
author_email="{{infoEmail}}",
|
author_email="{{infoEmail}}",
|
||||||
url="{{infoUrl}}",
|
url="{{infoUrl}}",
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import sys
|
import sys
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
|
NAME = "swagger_client"
|
||||||
|
VERSION = "1.0.0"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# To install the library, open a Terminal shell, then run this
|
# To install the library, open a Terminal shell, then run this
|
||||||
@ -15,8 +18,8 @@ from setuptools import setup, find_packages
|
|||||||
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi"]
|
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi"]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="SwaggerPetstore",
|
name=NAME,
|
||||||
version="1.0.0",
|
version=VERSION,
|
||||||
description="Swagger Petstore",
|
description="Swagger Petstore",
|
||||||
author_email="apiteam@swagger.io",
|
author_email="apiteam@swagger.io",
|
||||||
url="",
|
url="",
|
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 42 KiB |
@ -11,8 +11,8 @@ import os
|
|||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import SwaggerPetstore
|
import swagger_client
|
||||||
import SwaggerPetstore.configuration
|
import swagger_client.configuration
|
||||||
|
|
||||||
HOST = 'http://petstore.swagger.io/v2'
|
HOST = 'http://petstore.swagger.io/v2'
|
||||||
|
|
||||||
@ -20,20 +20,20 @@ HOST = 'http://petstore.swagger.io/v2'
|
|||||||
class ApiClientTests(unittest.TestCase):
|
class ApiClientTests(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.api_client = SwaggerPetstore.ApiClient(HOST)
|
self.api_client = swagger_client.ApiClient(HOST)
|
||||||
|
|
||||||
def test_configuration(self):
|
def test_configuration(self):
|
||||||
SwaggerPetstore.configuration.api_key['api_key'] = '123456'
|
swagger_client.configuration.api_key['api_key'] = '123456'
|
||||||
SwaggerPetstore.configuration.api_key_prefix['api_key'] = 'PREFIX'
|
swagger_client.configuration.api_key_prefix['api_key'] = 'PREFIX'
|
||||||
SwaggerPetstore.configuration.username = 'test_username'
|
swagger_client.configuration.username = 'test_username'
|
||||||
SwaggerPetstore.configuration.password = 'test_password'
|
swagger_client.configuration.password = 'test_password'
|
||||||
|
|
||||||
header_params = {'test1': 'value1'}
|
header_params = {'test1': 'value1'}
|
||||||
query_params = {'test2': 'value2'}
|
query_params = {'test2': 'value2'}
|
||||||
auth_settings = ['api_key', 'unknown']
|
auth_settings = ['api_key', 'unknown']
|
||||||
|
|
||||||
# test prefix
|
# test prefix
|
||||||
self.assertEqual('PREFIX', SwaggerPetstore.configuration.api_key_prefix['api_key'])
|
self.assertEqual('PREFIX', swagger_client.configuration.api_key_prefix['api_key'])
|
||||||
|
|
||||||
# update parameters based on auth setting
|
# update parameters based on auth setting
|
||||||
self.api_client.update_params_for_auth(header_params, query_params, auth_settings)
|
self.api_client.update_params_for_auth(header_params, query_params, auth_settings)
|
||||||
@ -44,8 +44,8 @@ class ApiClientTests(unittest.TestCase):
|
|||||||
self.assertEqual(query_params['test2'], 'value2')
|
self.assertEqual(query_params['test2'], 'value2')
|
||||||
|
|
||||||
# test basic auth
|
# test basic auth
|
||||||
self.assertEqual('test_username', SwaggerPetstore.configuration.username)
|
self.assertEqual('test_username', swagger_client.configuration.username)
|
||||||
self.assertEqual('test_password', SwaggerPetstore.configuration.password)
|
self.assertEqual('test_password', swagger_client.configuration.password)
|
||||||
|
|
||||||
def test_select_header_accept(self):
|
def test_select_header_accept(self):
|
||||||
accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
|
accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
|
||||||
@ -114,7 +114,7 @@ class ApiClientTests(unittest.TestCase):
|
|||||||
|
|
||||||
data = self.api_client.deserialize(json, 'dict(str, Pet)')
|
data = self.api_client.deserialize(json, 'dict(str, Pet)')
|
||||||
self.assertTrue(isinstance(data, dict))
|
self.assertTrue(isinstance(data, dict))
|
||||||
self.assertTrue(isinstance(data['pet'], SwaggerPetstore.Pet))
|
self.assertTrue(isinstance(data['pet'], swagger_client.Pet))
|
||||||
|
|
||||||
# dict(str, int)
|
# dict(str, int)
|
||||||
json = {
|
json = {
|
||||||
@ -128,6 +128,3 @@ class ApiClientTests(unittest.TestCase):
|
|||||||
def test_deserialize_to_object(self):
|
def test_deserialize_to_object(self):
|
||||||
data = self.api_client.deserialize("", "object")
|
data = self.api_client.deserialize("", "object")
|
||||||
self.assertTrue(type(data) == object)
|
self.assertTrue(type(data) == object)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
|||||||
"""
|
"""
|
||||||
Run the tests.
|
Run the tests.
|
||||||
$ pip install nose (optional)
|
$ pip install nose (optional)
|
||||||
$ cd SwaggerPetstore-python
|
$ cd swagger_client-python
|
||||||
$ nosetests -v
|
$ nosetests -v
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -11,25 +11,25 @@ import os
|
|||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import SwaggerPetstore
|
import swagger_client
|
||||||
from SwaggerPetstore.rest import ApiException
|
from swagger_client.rest import ApiException
|
||||||
|
|
||||||
|
|
||||||
class ApiExceptionTests(unittest.TestCase):
|
class ApiExceptionTests(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.api_client = SwaggerPetstore.ApiClient()
|
self.api_client = swagger_client.ApiClient()
|
||||||
self.pet_api = SwaggerPetstore.PetApi(self.api_client)
|
self.pet_api = swagger_client.PetApi(self.api_client)
|
||||||
self.setUpModels()
|
self.setUpModels()
|
||||||
|
|
||||||
def setUpModels(self):
|
def setUpModels(self):
|
||||||
self.category = SwaggerPetstore.Category()
|
self.category = swagger_client.Category()
|
||||||
self.category.id = int(time.time())
|
self.category.id = int(time.time())
|
||||||
self.category.name = "dog"
|
self.category.name = "dog"
|
||||||
self.tag = SwaggerPetstore.Tag()
|
self.tag = swagger_client.Tag()
|
||||||
self.tag.id = int(time.time())
|
self.tag.id = int(time.time())
|
||||||
self.tag.name = "blank"
|
self.tag.name = "blank"
|
||||||
self.pet = SwaggerPetstore.Pet()
|
self.pet = swagger_client.Pet()
|
||||||
self.pet.id = int(time.time())
|
self.pet.id = int(time.time())
|
||||||
self.pet.name = "hello kity"
|
self.pet.name = "hello kity"
|
||||||
self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"]
|
self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"]
|
@ -3,7 +3,7 @@
|
|||||||
"""
|
"""
|
||||||
Run the tests.
|
Run the tests.
|
||||||
$ pip install nose (optional)
|
$ pip install nose (optional)
|
||||||
$ cd SwaggerPetstore-python
|
$ cd swagger_client-python
|
||||||
$ nosetests -v
|
$ nosetests -v
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -11,8 +11,8 @@ import os
|
|||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import SwaggerPetstore
|
import swagger_client
|
||||||
from SwaggerPetstore.rest import ApiException
|
from swagger_client.rest import ApiException
|
||||||
|
|
||||||
HOST = 'http://petstore.swagger.io/v2'
|
HOST = 'http://petstore.swagger.io/v2'
|
||||||
|
|
||||||
@ -20,8 +20,8 @@ HOST = 'http://petstore.swagger.io/v2'
|
|||||||
class PetApiTests(unittest.TestCase):
|
class PetApiTests(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.api_client = SwaggerPetstore.ApiClient(HOST)
|
self.api_client = swagger_client.ApiClient(HOST)
|
||||||
self.pet_api = SwaggerPetstore.PetApi(self.api_client)
|
self.pet_api = swagger_client.PetApi(self.api_client)
|
||||||
self.setUpModels()
|
self.setUpModels()
|
||||||
self.setUpFiles()
|
self.setUpFiles()
|
||||||
|
|
||||||
@ -30,13 +30,13 @@ class PetApiTests(unittest.TestCase):
|
|||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
def setUpModels(self):
|
def setUpModels(self):
|
||||||
self.category = SwaggerPetstore.Category()
|
self.category = swagger_client.Category()
|
||||||
self.category.id = int(time.time())
|
self.category.id = int(time.time())
|
||||||
self.category.name = "dog"
|
self.category.name = "dog"
|
||||||
self.tag = SwaggerPetstore.Tag()
|
self.tag = swagger_client.Tag()
|
||||||
self.tag.id = int(time.time())
|
self.tag.id = int(time.time())
|
||||||
self.tag.name = "blank"
|
self.tag.name = "blank"
|
||||||
self.pet = SwaggerPetstore.Pet()
|
self.pet = swagger_client.Pet()
|
||||||
self.pet.id = int(time.time())
|
self.pet.id = int(time.time())
|
||||||
self.pet.name = "hello kity"
|
self.pet.name = "hello kity"
|
||||||
self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"]
|
self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"]
|
||||||
@ -50,22 +50,22 @@ class PetApiTests(unittest.TestCase):
|
|||||||
self.foo = os.path.join(self.test_file_dir, "foo.png")
|
self.foo = os.path.join(self.test_file_dir, "foo.png")
|
||||||
|
|
||||||
def test_create_api_instance(self):
|
def test_create_api_instance(self):
|
||||||
pet_api = SwaggerPetstore.PetApi()
|
pet_api = swagger_client.PetApi()
|
||||||
pet_api2 = SwaggerPetstore.PetApi()
|
pet_api2 = swagger_client.PetApi()
|
||||||
api_client3 = SwaggerPetstore.ApiClient()
|
api_client3 = swagger_client.ApiClient()
|
||||||
api_client3.user_agent = 'api client 3'
|
api_client3.user_agent = 'api client 3'
|
||||||
api_client4 = SwaggerPetstore.ApiClient()
|
api_client4 = swagger_client.ApiClient()
|
||||||
api_client4.user_agent = 'api client 4'
|
api_client4.user_agent = 'api client 4'
|
||||||
pet_api3 = SwaggerPetstore.PetApi(api_client3)
|
pet_api3 = swagger_client.PetApi(api_client3)
|
||||||
|
|
||||||
# same default api client
|
# same default api client
|
||||||
self.assertEqual(pet_api.api_client, pet_api2.api_client)
|
self.assertEqual(pet_api.api_client, pet_api2.api_client)
|
||||||
# confirm using the default api client in the config module
|
# confirm using the default api client in the config module
|
||||||
self.assertEqual(pet_api.api_client, SwaggerPetstore.configuration.api_client)
|
self.assertEqual(pet_api.api_client, swagger_client.configuration.api_client)
|
||||||
# 2 different api clients are not the same
|
# 2 different api clients are not the same
|
||||||
self.assertNotEqual(api_client3, api_client4)
|
self.assertNotEqual(api_client3, api_client4)
|
||||||
# customized pet api not using the default api client
|
# customized pet api not using the default api client
|
||||||
self.assertNotEqual(pet_api3.api_client, SwaggerPetstore.configuration.api_client)
|
self.assertNotEqual(pet_api3.api_client, swagger_client.configuration.api_client)
|
||||||
# customized pet api not using the old pet api's api client
|
# customized pet api not using the old pet api's api client
|
||||||
self.assertNotEqual(pet_api3.api_client, pet_api2.api_client)
|
self.assertNotEqual(pet_api3.api_client, pet_api2.api_client)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user