diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index 4b10f96a73..62048f4b29 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -25,6 +25,7 @@ from .rest import ApiException import os import re +import sys import urllib import json import mimetypes diff --git a/modules/swagger-codegen/src/main/resources/python/configuration.mustache b/modules/swagger-codegen/src/main/resources/python/configuration.mustache index 07625c67e4..f2691c0528 100644 --- a/modules/swagger-codegen/src/main/resources/python/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/python/configuration.mustache @@ -15,7 +15,7 @@ Copyright 2015 SmartBear Software See the License for the specific language governing permissions and limitations under the License. - ref: https://github.com/swagger-api/swagger-codegen + ref: https://github.com/swagger-api/swagger-codegen """ from __future__ import absolute_import @@ -27,10 +27,11 @@ try: except ImportError: # for python3 import http.client as httplib - + import sys import logging +from six import iteritems def singleton(cls, *args, **kw): instances = {} @@ -72,36 +73,23 @@ class Configuration(object): self.password = "" # Logging Settings + self.logger = {} + self.logger["package_logger"] = logging.getLogger("{{packageName}}") + self.logger["urllib3_logger"] = logging.getLogger("urllib3") + # Log format self.logging_format = '%(asctime)s %(levelname)s %(message)s' # Debug file location - self.__logging_file = None + self.logging_stream_handler = None + self.logging_file_handler = None + self.logging_file = None # Debug switch - self.__debug = False - self.init_logger() + self.debug = False # SSL/TLS verification # Set this to false to skip verifying SSL certificate when calling API from https server. self.verify_ssl = True # Set this to customize the certificate file to verify the peer. self.ssl_ca_cert = None - - def init_logger(self): - """ - Initializes logger settings. - """ - self.logger = logging.getLogger() - formatter = logging.Formatter(self.logging_format) - stream_handler = logging.StreamHandler() - stream_handler.setFormatter(formatter) - self.logger.addHandler(stream_handler) - if self.__debug: - self.logger.setLevel(logging.DEBUG) - else: - self.logger.setLevel(logging.WARNING) - if self.__logging_file: - file_handler = logging.FileHandler(self.__logging_file) - file_handler.setFormatter(formatter) - self.logger.addFilter(file_handler) @property def logging_file(self): @@ -111,10 +99,23 @@ class Configuration(object): def logging_file(self, value): self.__logging_file = value if self.__logging_file: - formater = logging.Formatter(self.logging_format) - file_handler = logging.FileHandler(self.__logging_file) - file_handler.setFormatter(formater) - self.logger.addHandler(file_handler) + # If set logging file, + # then add file handler and remove stream handler. + self.logging_file_handler = logging.FileHandler(self.__logging_file) + self.logging_file_handler.setFormatter(self.logging_formatter) + for _, logger in iteritems(self.logger): + logger.addHandler(self.logging_file_handler) + if self.logging_stream_handler: + logger.removeHandler(self.logging_stream_handler) + else: + # If not set logging file, + # then add stream handler and remove file handler. + self.logging_stream_handler = logging.StreamHandler() + self.logging_stream_handler.setFormatter(self.logging_formatter) + for _, logger in iteritems(self.logger): + logger.addHandler(self.logging_stream_handler) + if self.logging_file_handler: + logger.removeHandler(self.logging_file_handler) @property def debug(self): @@ -125,13 +126,26 @@ class Configuration(object): self.__debug = value if self.__debug: # if debug status is True, turn on debug logging - self.logger.setLevel(logging.DEBUG) + for _, logger in iteritems(self.logger): + logger.setLevel(logging.DEBUG) # turn on httplib debug httplib.HTTPConnection.debuglevel = 1 else: # if debug status is False, turn off debug logging, # setting log level to default `logging.WARNING` - self.logger.setLevel(logging.WARNING) + for _, logger in iteritems(self.logger): + logger.setLevel(logging.WARNING) + # turn off httplib debug + httplib.HTTPConnection.debuglevel = 0 + + @property + def logging_format(self): + return self.__logging_format + + @logging_format.setter + def logging_format(self, value): + self.__logging_format = value + self.logging_formatter = logging.Formatter(self.__logging_format) def get_api_key_with_prefix(self, identifier): """ diff --git a/samples/client/petstore/python/swagger_client/api_client.py b/samples/client/petstore/python/swagger_client/api_client.py index b44c4a321a..86c4a16111 100644 --- a/samples/client/petstore/python/swagger_client/api_client.py +++ b/samples/client/petstore/python/swagger_client/api_client.py @@ -25,6 +25,7 @@ from .rest import ApiException import os import re +import sys import urllib import json import mimetypes @@ -180,16 +181,19 @@ class ApiClient(object): If obj is str, int, float, bool, return directly. If obj is datetime.datetime, datetime.date convert to string in iso8601 format. - If obj is list, santize each element in the list. + If obj is list, sanitize each element in the list. If obj is dict, return the dict. If obj is swagger model, return the properties dict. :param obj: The data to serialize. :return: The serialized form of data. """ + types = (str, int, float, bool, tuple) + if sys.version_info < (3,0): + types = types + (unicode,) if isinstance(obj, type(None)): return None - elif isinstance(obj, (str, int, float, bool, tuple)): + elif isinstance(obj, types): return obj elif isinstance(obj, list): return [self.sanitize_for_serialization(sub_obj) diff --git a/samples/client/petstore/python/swagger_client/configuration.py b/samples/client/petstore/python/swagger_client/configuration.py index 4ca9342c05..4b5ed06f27 100644 --- a/samples/client/petstore/python/swagger_client/configuration.py +++ b/samples/client/petstore/python/swagger_client/configuration.py @@ -15,7 +15,7 @@ Copyright 2015 SmartBear Software See the License for the specific language governing permissions and limitations under the License. - ref: https://github.com/swagger-api/swagger-codegen + ref: https://github.com/swagger-api/swagger-codegen """ from __future__ import absolute_import @@ -27,10 +27,11 @@ try: except ImportError: # for python3 import http.client as httplib - + import sys import logging +from six import iteritems def singleton(cls, *args, **kw): instances = {} @@ -72,36 +73,23 @@ class Configuration(object): self.password = "" # Logging Settings + self.logger = {} + self.logger["package_logger"] = logging.getLogger("swagger_client") + self.logger["urllib3_logger"] = logging.getLogger("urllib3") + # Log format self.logging_format = '%(asctime)s %(levelname)s %(message)s' # Debug file location - self.__logging_file = None + self.logging_stream_handler = None + self.logging_file_handler = None + self.logging_file = None # Debug switch - self.__debug = False - self.init_logger() + self.debug = False # SSL/TLS verification # Set this to false to skip verifying SSL certificate when calling API from https server. self.verify_ssl = True # Set this to customize the certificate file to verify the peer. self.ssl_ca_cert = None - - def init_logger(self): - """ - Initializes logger settings. - """ - self.logger = logging.getLogger() - formatter = logging.Formatter(self.logging_format) - stream_handler = logging.StreamHandler() - stream_handler.setFormatter(formatter) - self.logger.addHandler(stream_handler) - if self.__debug: - self.logger.setLevel(logging.DEBUG) - else: - self.logger.setLevel(logging.WARNING) - if self.__logging_file: - file_handler = logging.FileHandler(self.__logging_file) - file_handler.setFormatter(formatter) - self.logger.addFilter(file_handler) @property def logging_file(self): @@ -111,10 +99,23 @@ class Configuration(object): def logging_file(self, value): self.__logging_file = value if self.__logging_file: - formater = logging.Formatter(self.logging_format) - file_handler = logging.FileHandler(self.__logging_file) - file_handler.setFormatter(formater) - self.logger.addHandler(file_handler) + # If set logging file, + # then add file handler and remove stream handler. + self.logging_file_handler = logging.FileHandler(self.__logging_file) + self.logging_file_handler.setFormatter(self.logging_formatter) + for _, logger in iteritems(self.logger): + logger.addHandler(self.logging_file_handler) + if self.logging_stream_handler: + logger.removeHandler(self.logging_stream_handler) + else: + # If not set logging file, + # then add stream handler and remove file handler. + self.logging_stream_handler = logging.StreamHandler() + self.logging_stream_handler.setFormatter(self.logging_formatter) + for _, logger in iteritems(self.logger): + logger.addHandler(self.logging_stream_handler) + if self.logging_file_handler: + logger.removeHandler(self.logging_file_handler) @property def debug(self): @@ -125,13 +126,26 @@ class Configuration(object): self.__debug = value if self.__debug: # if debug status is True, turn on debug logging - self.logger.setLevel(logging.DEBUG) + for _, logger in iteritems(self.logger): + logger.setLevel(logging.DEBUG) # turn on httplib debug httplib.HTTPConnection.debuglevel = 1 else: # if debug status is False, turn off debug logging, # setting log level to default `logging.WARNING` - self.logger.setLevel(logging.WARNING) + for _, logger in iteritems(self.logger): + logger.setLevel(logging.WARNING) + # turn off httplib debug + httplib.HTTPConnection.debuglevel = 0 + + @property + def logging_format(self): + return self.__logging_format + + @logging_format.setter + def logging_format(self, value): + self.__logging_format = value + self.logging_formatter = logging.Formatter(self.__logging_format) def get_api_key_with_prefix(self, identifier): """