diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java index a14fa71f425..475a816aa5c 100755 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java @@ -1,5 +1,6 @@ package io.swagger.codegen.languages; +import io.swagger.codegen.CliOption; import io.swagger.codegen.CodegenConfig; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; @@ -13,25 +14,19 @@ import java.util.Arrays; import java.util.HashSet; public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig { - protected String module = "SwaggerPetstore"; - protected String invokerPackage; - protected String eggPackage; + protected String packageName = null; + protected String packageVersion = null; public PythonClientCodegen() { super(); - eggPackage = module + "-python"; - - invokerPackage = eggPackage + File.separatorChar + module; - + modelPackage = "models"; + apiPackage = "api"; outputFolder = "generated-code" + File.separatorChar + "python"; modelTemplateFiles.put("model.mustache", ".py"); apiTemplateFiles.put("api.mustache", ".py"); templateDir = "python"; - apiPackage = invokerPackage + File.separatorChar + "apis"; - modelPackage = invokerPackage + File.separatorChar + "models"; - languageSpecificPrimitives.clear(); languageSpecificPrimitives.add("int"); languageSpecificPrimitives.add("float"); @@ -60,14 +55,43 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig "print", "class", "exec", "in", "raise", "continue", "finally", "is", "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")); - supportingFiles.add(new SupportingFile("setup.mustache", eggPackage, "setup.py")); - supportingFiles.add(new SupportingFile("api_client.mustache", invokerPackage, "api_client.py")); - supportingFiles.add(new SupportingFile("rest.mustache", invokerPackage, "rest.py")); - supportingFiles.add(new SupportingFile("configuration.mustache", invokerPackage, "configuration.py")); - supportingFiles.add(new SupportingFile("__init__package.mustache", invokerPackage, "__init__.py")); + @Override + public void processOpts() { + super.processOpts(); + + if (additionalProperties.containsKey("packageName")) { + 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__api.mustache", apiPackage, "__init__.py")); } @@ -225,4 +249,23 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig 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]+", "")); + } } + + diff --git a/modules/swagger-codegen/src/main/resources/python/setup.mustache b/modules/swagger-codegen/src/main/resources/python/setup.mustache index f1ba52d2930..af4fa69baf3 100644 --- a/modules/swagger-codegen/src/main/resources/python/setup.mustache +++ b/modules/swagger-codegen/src/main/resources/python/setup.mustache @@ -1,6 +1,9 @@ import sys from setuptools import setup, find_packages +NAME = "{{packageName}}" +VERSION = "{{packageVersion}}" + {{#apiInfo}}{{#apis}}{{^hasMore}} # 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"] setup( - name="{{module}}", - version="{{version}}", + name=NAME, + version=VERSION, description="{{appName}}", author_email="{{infoEmail}}", url="{{infoUrl}}", diff --git a/samples/client/petstore/python/SwaggerPetstore-python/Makefile b/samples/client/petstore/python/Makefile similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/Makefile rename to samples/client/petstore/python/Makefile diff --git a/samples/client/petstore/python/SwaggerPetstore-python/README.md b/samples/client/petstore/python/README.md similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/README.md rename to samples/client/petstore/python/README.md diff --git a/samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt b/samples/client/petstore/python/dev-requirements.txt similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt rename to samples/client/petstore/python/dev-requirements.txt diff --git a/samples/client/petstore/python/SwaggerPetstore-python/pom.xml b/samples/client/petstore/python/pom.xml similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/pom.xml rename to samples/client/petstore/python/pom.xml diff --git a/samples/client/petstore/python/SwaggerPetstore-python/setup.cfg b/samples/client/petstore/python/setup.cfg similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/setup.cfg rename to samples/client/petstore/python/setup.cfg diff --git a/samples/client/petstore/python/SwaggerPetstore-python/setup.py b/samples/client/petstore/python/setup.py similarity index 92% rename from samples/client/petstore/python/SwaggerPetstore-python/setup.py rename to samples/client/petstore/python/setup.py index fbd738be680..4232c3992b9 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/setup.py +++ b/samples/client/petstore/python/setup.py @@ -1,6 +1,9 @@ import sys from setuptools import setup, find_packages +NAME = "swagger_client" +VERSION = "1.0.0" + # 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"] setup( - name="SwaggerPetstore", - version="1.0.0", + name=NAME, + version=VERSION, description="Swagger Petstore", author_email="apiteam@swagger.io", url="", diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py b/samples/client/petstore/python/swagger_client/__init__.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py rename to samples/client/petstore/python/swagger_client/__init__.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py b/samples/client/petstore/python/swagger_client/api_client.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py rename to samples/client/petstore/python/swagger_client/api_client.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/__init__.py b/samples/client/petstore/python/swagger_client/apis/__init__.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/__init__.py rename to samples/client/petstore/python/swagger_client/apis/__init__.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py b/samples/client/petstore/python/swagger_client/apis/pet_api.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py rename to samples/client/petstore/python/swagger_client/apis/pet_api.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py b/samples/client/petstore/python/swagger_client/apis/store_api.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py rename to samples/client/petstore/python/swagger_client/apis/store_api.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py b/samples/client/petstore/python/swagger_client/apis/user_api.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py rename to samples/client/petstore/python/swagger_client/apis/user_api.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py b/samples/client/petstore/python/swagger_client/configuration.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py rename to samples/client/petstore/python/swagger_client/configuration.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/__init__.py b/samples/client/petstore/python/swagger_client/models/__init__.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/__init__.py rename to samples/client/petstore/python/swagger_client/models/__init__.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/category.py rename to samples/client/petstore/python/swagger_client/models/category.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/order.py rename to samples/client/petstore/python/swagger_client/models/order.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/pet.py rename to samples/client/petstore/python/swagger_client/models/pet.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/tag.py rename to samples/client/petstore/python/swagger_client/models/tag.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/user.py rename to samples/client/petstore/python/swagger_client/models/user.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py b/samples/client/petstore/python/swagger_client/rest.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py rename to samples/client/petstore/python/swagger_client/rest.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/testfiles/foo.png b/samples/client/petstore/python/testfiles/foo.png similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/testfiles/foo.png rename to samples/client/petstore/python/testfiles/foo.png diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/__init__.py b/samples/client/petstore/python/tests/__init__.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/tests/__init__.py rename to samples/client/petstore/python/tests/__init__.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py b/samples/client/petstore/python/tests/test_api_client.py similarity index 84% rename from samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py rename to samples/client/petstore/python/tests/test_api_client.py index 42d838417fd..d932006c052 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py +++ b/samples/client/petstore/python/tests/test_api_client.py @@ -11,8 +11,8 @@ import os import time import unittest -import SwaggerPetstore -import SwaggerPetstore.configuration +import swagger_client +import swagger_client.configuration HOST = 'http://petstore.swagger.io/v2' @@ -20,20 +20,20 @@ HOST = 'http://petstore.swagger.io/v2' class ApiClientTests(unittest.TestCase): def setUp(self): - self.api_client = SwaggerPetstore.ApiClient(HOST) + self.api_client = swagger_client.ApiClient(HOST) def test_configuration(self): - SwaggerPetstore.configuration.api_key['api_key'] = '123456' - SwaggerPetstore.configuration.api_key_prefix['api_key'] = 'PREFIX' - SwaggerPetstore.configuration.username = 'test_username' - SwaggerPetstore.configuration.password = 'test_password' + swagger_client.configuration.api_key['api_key'] = '123456' + swagger_client.configuration.api_key_prefix['api_key'] = 'PREFIX' + swagger_client.configuration.username = 'test_username' + swagger_client.configuration.password = 'test_password' header_params = {'test1': 'value1'} query_params = {'test2': 'value2'} auth_settings = ['api_key', 'unknown'] # 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 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') # test basic auth - self.assertEqual('test_username', SwaggerPetstore.configuration.username) - self.assertEqual('test_password', SwaggerPetstore.configuration.password) + self.assertEqual('test_username', swagger_client.configuration.username) + self.assertEqual('test_password', swagger_client.configuration.password) def test_select_header_accept(self): accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] @@ -114,7 +114,7 @@ class ApiClientTests(unittest.TestCase): data = self.api_client.deserialize(json, 'dict(str, Pet)') self.assertTrue(isinstance(data, dict)) - self.assertTrue(isinstance(data['pet'], SwaggerPetstore.Pet)) + self.assertTrue(isinstance(data['pet'], swagger_client.Pet)) # dict(str, int) json = { @@ -128,6 +128,3 @@ class ApiClientTests(unittest.TestCase): def test_deserialize_to_object(self): data = self.api_client.deserialize("", "object") self.assertTrue(type(data) == object) - - - diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py b/samples/client/petstore/python/tests/test_api_exception.py similarity index 85% rename from samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py rename to samples/client/petstore/python/tests/test_api_exception.py index d1b786241e8..f3d00cb10dc 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py +++ b/samples/client/petstore/python/tests/test_api_exception.py @@ -3,7 +3,7 @@ """ Run the tests. $ pip install nose (optional) -$ cd SwaggerPetstore-python +$ cd swagger_client-python $ nosetests -v """ @@ -11,25 +11,25 @@ import os import time import unittest -import SwaggerPetstore -from SwaggerPetstore.rest import ApiException +import swagger_client +from swagger_client.rest import ApiException class ApiExceptionTests(unittest.TestCase): def setUp(self): - self.api_client = SwaggerPetstore.ApiClient() - self.pet_api = SwaggerPetstore.PetApi(self.api_client) + self.api_client = swagger_client.ApiClient() + self.pet_api = swagger_client.PetApi(self.api_client) self.setUpModels() def setUpModels(self): - self.category = SwaggerPetstore.Category() + self.category = swagger_client.Category() self.category.id = int(time.time()) self.category.name = "dog" - self.tag = SwaggerPetstore.Tag() + self.tag = swagger_client.Tag() self.tag.id = int(time.time()) self.tag.name = "blank" - self.pet = SwaggerPetstore.Pet() + self.pet = swagger_client.Pet() self.pet.id = int(time.time()) self.pet.name = "hello kity" self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"] diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py b/samples/client/petstore/python/tests/test_pet_api.py similarity index 84% rename from samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py rename to samples/client/petstore/python/tests/test_pet_api.py index 3bf18607729..5397c0b50ef 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py +++ b/samples/client/petstore/python/tests/test_pet_api.py @@ -3,7 +3,7 @@ """ Run the tests. $ pip install nose (optional) -$ cd SwaggerPetstore-python +$ cd swagger_client-python $ nosetests -v """ @@ -11,8 +11,8 @@ import os import time import unittest -import SwaggerPetstore -from SwaggerPetstore.rest import ApiException +import swagger_client +from swagger_client.rest import ApiException HOST = 'http://petstore.swagger.io/v2' @@ -20,8 +20,8 @@ HOST = 'http://petstore.swagger.io/v2' class PetApiTests(unittest.TestCase): def setUp(self): - self.api_client = SwaggerPetstore.ApiClient(HOST) - self.pet_api = SwaggerPetstore.PetApi(self.api_client) + self.api_client = swagger_client.ApiClient(HOST) + self.pet_api = swagger_client.PetApi(self.api_client) self.setUpModels() self.setUpFiles() @@ -30,13 +30,13 @@ class PetApiTests(unittest.TestCase): time.sleep(1) def setUpModels(self): - self.category = SwaggerPetstore.Category() + self.category = swagger_client.Category() self.category.id = int(time.time()) self.category.name = "dog" - self.tag = SwaggerPetstore.Tag() + self.tag = swagger_client.Tag() self.tag.id = int(time.time()) self.tag.name = "blank" - self.pet = SwaggerPetstore.Pet() + self.pet = swagger_client.Pet() self.pet.id = int(time.time()) self.pet.name = "hello kity" 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") def test_create_api_instance(self): - pet_api = SwaggerPetstore.PetApi() - pet_api2 = SwaggerPetstore.PetApi() - api_client3 = SwaggerPetstore.ApiClient() + pet_api = swagger_client.PetApi() + pet_api2 = swagger_client.PetApi() + api_client3 = swagger_client.ApiClient() api_client3.user_agent = 'api client 3' - api_client4 = SwaggerPetstore.ApiClient() + api_client4 = swagger_client.ApiClient() api_client4.user_agent = 'api client 4' - pet_api3 = SwaggerPetstore.PetApi(api_client3) + pet_api3 = swagger_client.PetApi(api_client3) # same default api client self.assertEqual(pet_api.api_client, pet_api2.api_client) # 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 self.assertNotEqual(api_client3, api_client4) # 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 self.assertNotEqual(pet_api3.api_client, pet_api2.api_client) diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tox.ini b/samples/client/petstore/python/tox.ini similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/tox.ini rename to samples/client/petstore/python/tox.ini