From a32335dfbcb09bce83aeb4b214b2159e8e8724de Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 12 Jun 2015 17:51:05 +0800 Subject: [PATCH 1/6] Enable cli config options for python generator --- .../languages/PythonClientCodegen.java | 79 ++++++++++++++---- .../src/main/resources/python/setup.mustache | 7 +- .../Makefile | 0 .../README.md | 0 .../dev-requirements.txt | 0 .../pom.xml | 0 .../setup.cfg | 0 .../setup.py | 7 +- .../swagger_client}/__init__.py | 0 .../swagger_client}/api_client.py | 0 .../swagger_client}/apis/__init__.py | 0 .../swagger_client}/apis/pet_api.py | 0 .../swagger_client}/apis/store_api.py | 0 .../swagger_client}/apis/user_api.py | 0 .../swagger_client}/configuration.py | 0 .../swagger_client}/models/__init__.py | 0 .../swagger_client}/models/category.py | 0 .../swagger_client}/models/order.py | 0 .../swagger_client}/models/pet.py | 0 .../swagger_client}/models/tag.py | 0 .../swagger_client}/models/user.py | 0 .../swagger_client}/rest.py | 0 .../testfiles/foo.png | Bin .../tests/__init__.py | 0 .../tests/test_api_client.py | 20 ++--- .../tests/test_api_exception.py | 16 ++-- .../tests/test_pet_api.py | 30 +++---- .../tox.ini | 0 28 files changed, 105 insertions(+), 54 deletions(-) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/Makefile (100%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/README.md (100%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/dev-requirements.txt (100%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/pom.xml (100%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/setup.cfg (100%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/setup.py (92%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/__init__.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/api_client.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/apis/__init__.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/apis/pet_api.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/apis/store_api.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/apis/user_api.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/configuration.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/models/__init__.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/models/category.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/models/order.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/models/pet.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/models/tag.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/models/user.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python/SwaggerPetstore => swagger_client_python/swagger_client}/rest.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/testfiles/foo.png (100%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/tests/__init__.py (100%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/tests/test_api_client.py (85%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/tests/test_api_exception.py (85%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/tests/test_pet_api.py (84%) rename samples/client/petstore/python/{SwaggerPetstore-python => swagger_client_python}/tox.ini (100%) 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..e5a3b0810b2 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,45 @@ 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, default: SwaggerClient")); + 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("SwaggerClient"); + } + setPackageName(generatePackageName(packageName)); + + if (additionalProperties.containsKey("packageVersion")) { + setPackageVersion((String) additionalProperties.get("packageVersion")); + } + else { + setPackageVersion("1.0.0"); + } + + additionalProperties.put("packageName", packageName); + additionalProperties.put("packageVersion", packageVersion); + + String baseFolder = packageName + "_python"; + String swaggerFoler = baseFolder + File.separatorChar + packageName; + + modelPackage = swaggerFoler + File.separatorChar + "models"; + apiPackage = swaggerFoler + File.separatorChar + "apis"; + + supportingFiles.add(new SupportingFile("README.mustache", baseFolder, "README.md")); + supportingFiles.add(new SupportingFile("setup.mustache", baseFolder, "setup.py")); + supportingFiles.add(new SupportingFile("api_client.mustache", swaggerFoler, "api_client.py")); + supportingFiles.add(new SupportingFile("rest.mustache", swaggerFoler, "rest.py")); + supportingFiles.add(new SupportingFile("configuration.mustache", swaggerFoler, "configuration.py")); + supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFoler, "__init__.py")); supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py")); supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py")); } @@ -225,4 +251,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/swagger_client_python/Makefile similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/Makefile rename to samples/client/petstore/python/swagger_client_python/Makefile diff --git a/samples/client/petstore/python/SwaggerPetstore-python/README.md b/samples/client/petstore/python/swagger_client_python/README.md similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/README.md rename to samples/client/petstore/python/swagger_client_python/README.md diff --git a/samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt b/samples/client/petstore/python/swagger_client_python/dev-requirements.txt similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt rename to samples/client/petstore/python/swagger_client_python/dev-requirements.txt diff --git a/samples/client/petstore/python/SwaggerPetstore-python/pom.xml b/samples/client/petstore/python/swagger_client_python/pom.xml similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/pom.xml rename to samples/client/petstore/python/swagger_client_python/pom.xml diff --git a/samples/client/petstore/python/SwaggerPetstore-python/setup.cfg b/samples/client/petstore/python/swagger_client_python/setup.cfg similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/setup.cfg rename to samples/client/petstore/python/swagger_client_python/setup.cfg diff --git a/samples/client/petstore/python/SwaggerPetstore-python/setup.py b/samples/client/petstore/python/swagger_client_python/setup.py similarity index 92% rename from samples/client/petstore/python/SwaggerPetstore-python/setup.py rename to samples/client/petstore/python/swagger_client_python/setup.py index fbd738be680..4232c3992b9 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/setup.py +++ b/samples/client/petstore/python/swagger_client_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_python/swagger_client/rest.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/testfiles/foo.png b/samples/client/petstore/python/swagger_client_python/testfiles/foo.png similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/testfiles/foo.png rename to samples/client/petstore/python/swagger_client_python/testfiles/foo.png diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/__init__.py b/samples/client/petstore/python/swagger_client_python/tests/__init__.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/tests/__init__.py rename to samples/client/petstore/python/swagger_client_python/tests/__init__.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py b/samples/client/petstore/python/swagger_client_python/tests/test_api_client.py similarity index 85% rename from samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py rename to samples/client/petstore/python/swagger_client_python/tests/test_api_client.py index 42d838417fd..d18008f5575 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py +++ b/samples/client/petstore/python/swagger_client_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'] diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py b/samples/client/petstore/python/swagger_client_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/swagger_client_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/swagger_client_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/swagger_client_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/swagger_client_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/swagger_client_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/swagger_client_python/tox.ini similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/tox.ini rename to samples/client/petstore/python/swagger_client_python/tox.ini From 5d19ef6146ff926cd4fe94078ceb5bbfd771fa46 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 12 Jun 2015 18:22:24 +0800 Subject: [PATCH 2/6] Generate the python into `python/` folder directly --- .../codegen/languages/PythonClientCodegen.java | 7 +++---- .../python/{swagger_client_python => }/Makefile | 0 .../python/{swagger_client_python => }/README.md | 0 .../dev-requirements.txt | 0 .../python/{swagger_client_python => }/pom.xml | 0 .../python/{swagger_client_python => }/setup.cfg | 0 .../python/{swagger_client_python => }/setup.py | 0 .../swagger_client/__init__.py | 0 .../swagger_client/api_client.py | 0 .../swagger_client/apis/__init__.py | 0 .../swagger_client/apis/pet_api.py | 0 .../swagger_client/apis/store_api.py | 0 .../swagger_client/apis/user_api.py | 0 .../swagger_client/configuration.py | 0 .../swagger_client/models/__init__.py | 0 .../swagger_client/models/category.py | 0 .../swagger_client/models/order.py | 0 .../swagger_client/models/pet.py | 0 .../swagger_client/models/tag.py | 0 .../swagger_client/models/user.py | 0 .../swagger_client/rest.py | 0 .../{swagger_client_python => }/testfiles/foo.png | Bin .../{swagger_client_python => }/tests/__init__.py | 0 .../tests/test_api_client.py | 0 .../tests/test_api_exception.py | 0 .../tests/test_pet_api.py | 0 .../python/{swagger_client_python => }/tox.ini | 0 27 files changed, 3 insertions(+), 4 deletions(-) rename samples/client/petstore/python/{swagger_client_python => }/Makefile (100%) rename samples/client/petstore/python/{swagger_client_python => }/README.md (100%) rename samples/client/petstore/python/{swagger_client_python => }/dev-requirements.txt (100%) rename samples/client/petstore/python/{swagger_client_python => }/pom.xml (100%) rename samples/client/petstore/python/{swagger_client_python => }/setup.cfg (100%) rename samples/client/petstore/python/{swagger_client_python => }/setup.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/__init__.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/api_client.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/apis/__init__.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/apis/pet_api.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/apis/store_api.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/apis/user_api.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/configuration.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/models/__init__.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/models/category.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/models/order.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/models/pet.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/models/tag.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/models/user.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/swagger_client/rest.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/testfiles/foo.png (100%) rename samples/client/petstore/python/{swagger_client_python => }/tests/__init__.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/tests/test_api_client.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/tests/test_api_exception.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/tests/test_pet_api.py (100%) rename samples/client/petstore/python/{swagger_client_python => }/tox.ini (100%) 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 e5a3b0810b2..030e19c1a31 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 @@ -82,14 +82,13 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig additionalProperties.put("packageName", packageName); additionalProperties.put("packageVersion", packageVersion); - String baseFolder = packageName + "_python"; - String swaggerFoler = baseFolder + File.separatorChar + packageName; + String swaggerFoler = packageName; modelPackage = swaggerFoler + File.separatorChar + "models"; apiPackage = swaggerFoler + File.separatorChar + "apis"; - supportingFiles.add(new SupportingFile("README.mustache", baseFolder, "README.md")); - supportingFiles.add(new SupportingFile("setup.mustache", baseFolder, "setup.py")); + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); + supportingFiles.add(new SupportingFile("setup.mustache", "", "setup.py")); supportingFiles.add(new SupportingFile("api_client.mustache", swaggerFoler, "api_client.py")); supportingFiles.add(new SupportingFile("rest.mustache", swaggerFoler, "rest.py")); supportingFiles.add(new SupportingFile("configuration.mustache", swaggerFoler, "configuration.py")); diff --git a/samples/client/petstore/python/swagger_client_python/Makefile b/samples/client/petstore/python/Makefile similarity index 100% rename from samples/client/petstore/python/swagger_client_python/Makefile rename to samples/client/petstore/python/Makefile diff --git a/samples/client/petstore/python/swagger_client_python/README.md b/samples/client/petstore/python/README.md similarity index 100% rename from samples/client/petstore/python/swagger_client_python/README.md rename to samples/client/petstore/python/README.md diff --git a/samples/client/petstore/python/swagger_client_python/dev-requirements.txt b/samples/client/petstore/python/dev-requirements.txt similarity index 100% rename from samples/client/petstore/python/swagger_client_python/dev-requirements.txt rename to samples/client/petstore/python/dev-requirements.txt diff --git a/samples/client/petstore/python/swagger_client_python/pom.xml b/samples/client/petstore/python/pom.xml similarity index 100% rename from samples/client/petstore/python/swagger_client_python/pom.xml rename to samples/client/petstore/python/pom.xml diff --git a/samples/client/petstore/python/swagger_client_python/setup.cfg b/samples/client/petstore/python/setup.cfg similarity index 100% rename from samples/client/petstore/python/swagger_client_python/setup.cfg rename to samples/client/petstore/python/setup.cfg diff --git a/samples/client/petstore/python/swagger_client_python/setup.py b/samples/client/petstore/python/setup.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/setup.py rename to samples/client/petstore/python/setup.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/__init__.py b/samples/client/petstore/python/swagger_client/__init__.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/__init__.py rename to samples/client/petstore/python/swagger_client/__init__.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/api_client.py b/samples/client/petstore/python/swagger_client/api_client.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/api_client.py rename to samples/client/petstore/python/swagger_client/api_client.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/apis/__init__.py b/samples/client/petstore/python/swagger_client/apis/__init__.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/apis/__init__.py rename to samples/client/petstore/python/swagger_client/apis/__init__.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/apis/pet_api.py b/samples/client/petstore/python/swagger_client/apis/pet_api.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/apis/pet_api.py rename to samples/client/petstore/python/swagger_client/apis/pet_api.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/apis/store_api.py b/samples/client/petstore/python/swagger_client/apis/store_api.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/apis/store_api.py rename to samples/client/petstore/python/swagger_client/apis/store_api.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/apis/user_api.py b/samples/client/petstore/python/swagger_client/apis/user_api.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/apis/user_api.py rename to samples/client/petstore/python/swagger_client/apis/user_api.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/configuration.py b/samples/client/petstore/python/swagger_client/configuration.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/configuration.py rename to samples/client/petstore/python/swagger_client/configuration.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/models/__init__.py b/samples/client/petstore/python/swagger_client/models/__init__.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/models/__init__.py rename to samples/client/petstore/python/swagger_client/models/__init__.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/models/category.py rename to samples/client/petstore/python/swagger_client/models/category.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/models/order.py rename to samples/client/petstore/python/swagger_client/models/order.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/models/pet.py rename to samples/client/petstore/python/swagger_client/models/pet.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/models/tag.py rename to samples/client/petstore/python/swagger_client/models/tag.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/models/user.py rename to samples/client/petstore/python/swagger_client/models/user.py diff --git a/samples/client/petstore/python/swagger_client_python/swagger_client/rest.py b/samples/client/petstore/python/swagger_client/rest.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/swagger_client/rest.py rename to samples/client/petstore/python/swagger_client/rest.py diff --git a/samples/client/petstore/python/swagger_client_python/testfiles/foo.png b/samples/client/petstore/python/testfiles/foo.png similarity index 100% rename from samples/client/petstore/python/swagger_client_python/testfiles/foo.png rename to samples/client/petstore/python/testfiles/foo.png diff --git a/samples/client/petstore/python/swagger_client_python/tests/__init__.py b/samples/client/petstore/python/tests/__init__.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/tests/__init__.py rename to samples/client/petstore/python/tests/__init__.py diff --git a/samples/client/petstore/python/swagger_client_python/tests/test_api_client.py b/samples/client/petstore/python/tests/test_api_client.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/tests/test_api_client.py rename to samples/client/petstore/python/tests/test_api_client.py diff --git a/samples/client/petstore/python/swagger_client_python/tests/test_api_exception.py b/samples/client/petstore/python/tests/test_api_exception.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/tests/test_api_exception.py rename to samples/client/petstore/python/tests/test_api_exception.py diff --git a/samples/client/petstore/python/swagger_client_python/tests/test_pet_api.py b/samples/client/petstore/python/tests/test_pet_api.py similarity index 100% rename from samples/client/petstore/python/swagger_client_python/tests/test_pet_api.py rename to samples/client/petstore/python/tests/test_pet_api.py diff --git a/samples/client/petstore/python/swagger_client_python/tox.ini b/samples/client/petstore/python/tox.ini similarity index 100% rename from samples/client/petstore/python/swagger_client_python/tox.ini rename to samples/client/petstore/python/tox.ini From 62f4c252f2eb01c58350f27d70c35aaeaf17ec8c Mon Sep 17 00:00:00 2001 From: geekerzp Date: Tue, 16 Jun 2015 09:44:18 +0800 Subject: [PATCH 3/6] Update python codegen. * Update config-help message. * Do not sanitize packageName to underscore format. --- .../io/swagger/codegen/languages/PythonClientCodegen.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 030e19c1a31..c901462ca48 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 @@ -56,7 +56,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig "return", "def", "for", "lambda", "try")); cliOptions.clear(); - cliOptions.add(new CliOption("packageName", "python package name, default: SwaggerClient")); + cliOptions.add(new CliOption("packageName", "python package name, default: swagger_client")); cliOptions.add(new CliOption("packageVersion", "python package version, default: 1.0.0")); } @@ -68,9 +68,8 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig setPackageName((String) additionalProperties.get("packageName")); } else { - setPackageName("SwaggerClient"); + setPackageName("swagger_client"); } - setPackageName(generatePackageName(packageName)); if (additionalProperties.containsKey("packageVersion")) { setPackageVersion((String) additionalProperties.get("packageVersion")); From 194c8ff71deb1382c1e5547b877f6f99f0191e07 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Tue, 16 Jun 2015 15:29:10 +0800 Subject: [PATCH 4/6] Update config-help message of python client --- .../java/io/swagger/codegen/languages/PythonClientCodegen.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c901462ca48..cdafa337c21 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 @@ -56,7 +56,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig "return", "def", "for", "lambda", "try")); cliOptions.clear(); - cliOptions.add(new CliOption("packageName", "python package name, default: swagger_client")); + 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")); } From 8d18aee2f2222e17f4bd7d4dbba1e4918b5b2e3b Mon Sep 17 00:00:00 2001 From: geekerzp Date: Thu, 18 Jun 2015 22:02:54 +0800 Subject: [PATCH 5/6] Update test case `test_deserialize_to_dict` of python client --- samples/client/petstore/python/tests/test_api_client.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/samples/client/petstore/python/tests/test_api_client.py b/samples/client/petstore/python/tests/test_api_client.py index d18008f5575..d932006c052 100644 --- a/samples/client/petstore/python/tests/test_api_client.py +++ b/samples/client/petstore/python/tests/test_api_client.py @@ -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) - - - From acf09da5787728fdfc56defd29f0912f5bb5743c Mon Sep 17 00:00:00 2001 From: geekerzp Date: Mon, 22 Jun 2015 16:35:15 +0800 Subject: [PATCH 6/6] Fix typo in python codegen --- .../codegen/languages/PythonClientCodegen.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 cdafa337c21..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 @@ -81,17 +81,17 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig additionalProperties.put("packageName", packageName); additionalProperties.put("packageVersion", packageVersion); - String swaggerFoler = packageName; + String swaggerFolder = packageName; - modelPackage = swaggerFoler + File.separatorChar + "models"; - apiPackage = swaggerFoler + File.separatorChar + "apis"; + 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", swaggerFoler, "api_client.py")); - supportingFiles.add(new SupportingFile("rest.mustache", swaggerFoler, "rest.py")); - supportingFiles.add(new SupportingFile("configuration.mustache", swaggerFoler, "configuration.py")); - supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFoler, "__init__.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")); }