From f2ecc271b5502c799060489f4b1dacf790a2a888 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Thu, 9 Apr 2015 09:21:21 +0800 Subject: [PATCH] Better import for python client. --- .../languages/PythonClientCodegen.java | 10 ++++---- ...init__.mustache => __init__model.mustache} | 0 .../resources/python/__init__package.mustache | 24 +++++++++++++++++++ .../client/petstore/python/client/__init__.py | 19 +++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) rename modules/swagger-codegen/src/main/resources/python/{__init__.mustache => __init__model.mustache} (100%) create mode 100644 modules/swagger-codegen/src/main/resources/python/__init__package.mustache diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java index bc47b4041daf..4f71c2bc219b 100755 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java @@ -27,10 +27,10 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig modelTemplateFiles.put("model.mustache", ".py"); apiTemplateFiles.put("api.mustache", ".py"); templateDir = "python"; - + apiPackage = module; modelPackage = module + ".models"; - + languageSpecificPrimitives.clear(); languageSpecificPrimitives.add("int"); languageSpecificPrimitives.add("float"); @@ -61,8 +61,8 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("swagger.mustache", module, "swagger.py")); - supportingFiles.add(new SupportingFile("__init__.mustache", module, "__init__.py")); - supportingFiles.add(new SupportingFile("__init__.mustache", modelPackage.replace('.', File.separatorChar), "__init__.py")); + supportingFiles.add(new SupportingFile("__init__package.mustache", module, "__init__.py")); + supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage.replace('.', File.separatorChar), "__init__.py")); } @Override @@ -113,5 +113,5 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig public String toDefaultValue(Property p) { // TODO: Support Python def value return "null"; - } + } } diff --git a/modules/swagger-codegen/src/main/resources/python/__init__.mustache b/modules/swagger-codegen/src/main/resources/python/__init__model.mustache similarity index 100% rename from modules/swagger-codegen/src/main/resources/python/__init__.mustache rename to modules/swagger-codegen/src/main/resources/python/__init__model.mustache diff --git a/modules/swagger-codegen/src/main/resources/python/__init__package.mustache b/modules/swagger-codegen/src/main/resources/python/__init__package.mustache new file mode 100644 index 000000000000..4b595ed56b8a --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/python/__init__package.mustache @@ -0,0 +1,24 @@ +#!/usr/bin/env python +"""Add all of the modules in the current directory to __all__""" +import os + +# import models into package +{{#models}} +{{#model}} +from .models.{{classname}} import {{classname}} +{{/model}} +{{/models}} + +# import apis into package +{{#apiInfo}}{{#apis}}{{#operations}} +from .{{classname}} import {{classname}} +{{/operations}}{{/apis}}{{/apiInfo}} + +# import ApiClient +from .swagger import ApiClient + +__all__ = [] + +for module in os.listdir(os.path.dirname(__file__)): + if module != '__init__.py' and module[-3:] == '.py': + __all__.append(module[:-3]) diff --git a/samples/client/petstore/python/client/__init__.py b/samples/client/petstore/python/client/__init__.py index 728aacbb9ab6..a8240eaf541d 100644 --- a/samples/client/petstore/python/client/__init__.py +++ b/samples/client/petstore/python/client/__init__.py @@ -2,6 +2,25 @@ """Add all of the modules in the current directory to __all__""" import os +# import models into package +from .models.User import User +from .models.Category import Category +from .models.Pet import Pet +from .models.Tag import Tag +from .models.Order import Order + +# import apis into package + +from .UserApi import UserApi + +from .PetApi import PetApi + +from .StoreApi import StoreApi + + +# import ApiClient +from .swagger import ApiClient + __all__ = [] for module in os.listdir(os.path.dirname(__file__)):