summaryrefslogtreecommitdiffstats
path: root/result_collection_api/tornado_swagger_ui/tornado_swagger
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2016-05-26 14:30:24 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2016-05-26 15:22:48 +0800
commitc8be4b557e51316a3cd0cc54d962c86619e4cbaa (patch)
tree5bc535103f89edb99a01f7b5b0b7975d0e341e74 /result_collection_api/tornado_swagger_ui/tornado_swagger
parentb140bd0f8055af4f1624a2c9a6291f7923003122 (diff)
eliminate flake8 violation in testAPI except dashboard dir
JIRA: FUNCTEST-262 Change-Id: Ib77bb080111392b8e3248c5e6152f55c12b8c67c Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'result_collection_api/tornado_swagger_ui/tornado_swagger')
-rw-r--r--result_collection_api/tornado_swagger_ui/tornado_swagger/__init__.py1
-rw-r--r--result_collection_api/tornado_swagger_ui/tornado_swagger/handlers.py31
-rw-r--r--result_collection_api/tornado_swagger_ui/tornado_swagger/settings.py9
-rw-r--r--result_collection_api/tornado_swagger_ui/tornado_swagger/swagger.py24
-rw-r--r--result_collection_api/tornado_swagger_ui/tornado_swagger/views.py42
5 files changed, 75 insertions, 32 deletions
diff --git a/result_collection_api/tornado_swagger_ui/tornado_swagger/__init__.py b/result_collection_api/tornado_swagger_ui/tornado_swagger/__init__.py
index 49b1b74..031a4a2 100644
--- a/result_collection_api/tornado_swagger_ui/tornado_swagger/__init__.py
+++ b/result_collection_api/tornado_swagger_ui/tornado_swagger/__init__.py
@@ -2,4 +2,3 @@
# -*- coding: utf-8 -*-
__author__ = 'serena'
-
diff --git a/result_collection_api/tornado_swagger_ui/tornado_swagger/handlers.py b/result_collection_api/tornado_swagger_ui/tornado_swagger/handlers.py
index 33c4b53..8bcb966 100644
--- a/result_collection_api/tornado_swagger_ui/tornado_swagger/handlers.py
+++ b/result_collection_api/tornado_swagger_ui/tornado_swagger/handlers.py
@@ -2,8 +2,9 @@
# -*- coding: utf-8 -*-
from tornado.web import URLSpec, StaticFileHandler
-from settings import *
-from views import *
+from settings import default_settings, \
+ SWAGGER_API_DOCS, SWAGGER_API_LIST, SWAGGER_API_SPEC
+from views import SwaggerUIHandler, SwaggerResourcesHandler, SwaggerApiHandler
__author__ = 'serena'
@@ -12,9 +13,27 @@ def swagger_handlers():
prefix = default_settings.get('swagger_prefix', '/swagger')
if prefix[-1] != '/':
prefix += '/'
+
+ def _path(suffix):
+ return prefix + suffix
return [
- URLSpec(prefix + r'spec.html$', SwaggerUIHandler, default_settings, name=URL_SWAGGER_API_DOCS),
- URLSpec(prefix + r'spec.json$', SwaggerResourcesHandler, default_settings, name=URL_SWAGGER_API_LIST),
- URLSpec(prefix + r'spec$', SwaggerApiHandler, default_settings, name=URL_SWAGGER_API_SPEC),
- (prefix + r'(.*\.(css|png|gif|js))', StaticFileHandler, {'path': default_settings.get('static_path')}),
+ URLSpec(
+ _path(r'spec.html$'),
+ SwaggerUIHandler,
+ default_settings,
+ name=SWAGGER_API_DOCS),
+ URLSpec(
+ _path(r'spec.json$'),
+ SwaggerResourcesHandler,
+ default_settings,
+ name=SWAGGER_API_LIST),
+ URLSpec(
+ _path(r'spec$'),
+ SwaggerApiHandler,
+ default_settings,
+ name=SWAGGER_API_SPEC),
+ (
+ _path(r'(.*\.(css|png|gif|js))'),
+ StaticFileHandler,
+ {'path': default_settings.get('static_path')}),
]
diff --git a/result_collection_api/tornado_swagger_ui/tornado_swagger/settings.py b/result_collection_api/tornado_swagger_ui/tornado_swagger/settings.py
index bd70c17..8f43c4a 100644
--- a/result_collection_api/tornado_swagger_ui/tornado_swagger/settings.py
+++ b/result_collection_api/tornado_swagger_ui/tornado_swagger/settings.py
@@ -6,11 +6,12 @@ __author__ = 'serena'
SWAGGER_VERSION = '1.2'
-URL_SWAGGER_API_DOCS = 'swagger-api-docs'
-URL_SWAGGER_API_LIST = 'swagger-api-list'
-URL_SWAGGER_API_SPEC = 'swagger-api-spec'
+SWAGGER_API_DOCS = 'swagger-api-docs'
+SWAGGER_API_LIST = 'swagger-api-list'
+SWAGGER_API_SPEC = 'swagger-api-spec'
-STATIC_PATH = os.path.join(os.path.dirname(os.path.normpath(__file__)), 'static')
+STATIC_PATH = os.path.join(os.path.dirname(os.path.normpath(__file__)),
+ 'static')
default_settings = {
'base_url': '/',
diff --git a/result_collection_api/tornado_swagger_ui/tornado_swagger/swagger.py b/result_collection_api/tornado_swagger_ui/tornado_swagger/swagger.py
index 50b2cfe..b290e05 100644
--- a/result_collection_api/tornado_swagger_ui/tornado_swagger/swagger.py
+++ b/result_collection_api/tornado_swagger_ui/tornado_swagger/swagger.py
@@ -128,7 +128,7 @@ class DocParser(object):
if code is None:
self.properties.setdefault(arg, {}).update({
'type': link
- })
+ })
elif code == 'list':
self.properties.setdefault(arg, {}).update({
'type': 'array',
@@ -184,7 +184,7 @@ class model(DocParser):
self.required = []
self.cls = None
- def __call__(self, *args, **kwargs):
+ def __call__(self, *args):
if self.cls:
return self.cls
@@ -206,17 +206,21 @@ class model(DocParser):
argspec.args.remove("self")
defaults = {}
if argspec.defaults:
- defaults = list(zip(argspec.args[-len(argspec.defaults):], argspec.defaults))
+ defaults = list(zip(argspec.args[-len(argspec.defaults):],
+ argspec.defaults))
required_args_count = len(argspec.args) - len(defaults)
for arg in argspec.args[:required_args_count]:
self.required.append(arg)
self.properties.setdefault(arg, {'type': 'string'})
for arg, default in defaults:
- self.properties.setdefault(arg, {'type': 'string', "default": default})
+ self.properties.setdefault(arg, {
+ 'type': 'string',
+ "default": default
+ })
class operation(DocParser):
- def __init__(self, nickname=None, **kwds):
+ def __init__(self, nickname='apis', **kwds):
super(operation, self).__init__()
self.nickname = nickname
self.func = None
@@ -271,5 +275,11 @@ def docs(**opts):
class Application(tornado.web.Application):
- def __init__(self, handlers=None, default_host="", transforms=None, **settings):
- super(Application, self).__init__(swagger_handlers() + handlers, default_host, transforms, **settings)
+ def __init__(self, handlers=None,
+ default_host="",
+ transforms=None,
+ **settings):
+ super(Application, self).__init__(swagger_handlers() + handlers,
+ default_host,
+ transforms,
+ **settings)
diff --git a/result_collection_api/tornado_swagger_ui/tornado_swagger/views.py b/result_collection_api/tornado_swagger_ui/tornado_swagger/views.py
index 1882f00..7624023 100644
--- a/result_collection_api/tornado_swagger_ui/tornado_swagger/views.py
+++ b/result_collection_api/tornado_swagger_ui/tornado_swagger/views.py
@@ -5,13 +5,17 @@ import json
import inspect
import tornado.web
import tornado.template
-from settings import SWAGGER_VERSION, URL_SWAGGER_API_LIST, URL_SWAGGER_API_SPEC, models
+from settings import SWAGGER_VERSION, \
+ SWAGGER_API_LIST, \
+ SWAGGER_API_SPEC
+from settings import models
__author__ = 'serena'
def json_dumps(obj, pretty=False):
- return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': ')) if pretty else json.dumps(obj)
+ return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': ')) \
+ if pretty else json.dumps(obj)
class SwaggerUIHandler(tornado.web.RequestHandler):
@@ -22,7 +26,9 @@ class SwaggerUIHandler(tornado.web.RequestHandler):
return self.static_path
def get(self):
- discovery_url = urlparse.urljoin(self.request.full_url(), self.reverse_url(URL_SWAGGER_API_LIST))
+ discovery_url = \
+ urlparse.urljoin(self.request.full_url(),
+ self.reverse_url(SWAGGER_API_LIST))
self.render('index.html', discovery_url=discovery_url)
@@ -41,7 +47,7 @@ class SwaggerResourcesHandler(tornado.web.RequestHandler):
'produces': ["application/json"],
'description': 'Test Api Spec',
'apis': [{
- 'path': self.reverse_url(URL_SWAGGER_API_SPEC),
+ 'path': self.reverse_url(SWAGGER_API_SPEC),
'description': 'Test Api Spec'
}]
}
@@ -60,11 +66,14 @@ class SwaggerApiHandler(tornado.web.RequestHandler):
if apis is None:
raise tornado.web.HTTPError(404)
+ base_path = urlparse.urljoin(self.request.full_url(),
+ self.base_url)[:-1]
specs = {
'apiVersion': self.api_version,
'swaggerVersion': SWAGGER_VERSION,
- 'basePath': urlparse.urljoin(self.request.full_url(), self.base_url)[:-1],
- 'apis': [self.__get_api_spec__(path, spec, operations) for path, spec, operations in apis],
+ 'basePath': base_path,
+ 'apis': [self.__get_api_spec__(path, spec, operations)
+ for path, spec, operations in apis],
'models': self.__get_models_spec(models)
}
self.finish(json_dumps(specs, self.get_arguments('pretty')))
@@ -103,14 +112,19 @@ class SwaggerApiHandler(tornado.web.RequestHandler):
@staticmethod
def find_api(host_handlers):
+ def get_path(url, args):
+ return url % tuple(['{%s}' % arg for arg in args])
+
+ def get_operations(cls):
+ return [member.rest_api
+ for (_, member) in inspect.getmembers(cls)
+ if hasattr(member, 'rest_api')]
+
for host, handlers in host_handlers:
for spec in handlers:
- for (name, member) in inspect.getmembers(spec.handler_class):
- if inspect.ismethod(member) and hasattr(member, 'rest_api'):
- spec_path = spec._path % tuple(['{%s}' % arg for arg in member.rest_api.func_args])
- operations = [member.rest_api for (name, member) in inspect.getmembers(spec.handler_class)
- if hasattr(member, 'rest_api')]
- yield spec_path, spec, operations
+ for (_, mbr) in inspect.getmembers(spec.handler_class):
+ if inspect.ismethod(mbr) and hasattr(mbr, 'rest_api'):
+ path = get_path(spec._path, mbr.rest_api.func_args)
+ operations = get_operations(spec.handler_class)
+ yield path, spec, operations
break
-
-