![Sun Austin](/assets/img/avatar_default.png)
below issures are removed from ignore cases: E114 indentation is not a multiple of four (comment) E116 unexpected indentation (comment) E121 continuation line under-indented for hanging indent E122 continuation line missing indentation or outdented E123 closing bracket does not match indentation of opening bracket's line E124 closing bracket does not match visual indentation E125 continuation line with same indent as next logical line E126 continuation line over-indented for hanging indent E127 continuation line over-indented for visual indent E128 continuation line under-indented for visual indent E129 visually indented line with same indent as next logical line E131 continuation line unaligned for hanging indent E201 whitespace after '(' E228 missing whitespace around modulo operator E231 missing whitespace after ',' E241 multiple spaces after ':' E251 unexpected spaces around keyword / parameter equals E265 block comment should start with '#' E271 multiple spaces after keyword E302 expected 2 blank lines, found 1 E303 too many blank lines E305 expected 2 blank lines after class or function definition, found 1 E704 multiple statements on one line (def) E713 test for membership should be 'not in' E714 test for object identity should be 'is not' E722 do not use bare except' E731 do not assign a lambda expression, use a def E999 SyntaxError: invalid syntax (this is likely python3) F401 <foo> imported but unused F841 local variable 'foo' is assigned to but never used H201: no 'except:' H233: Python 3.x incompatible use of print operator B001 Do not use bare `except:` B004 Using `hasattr(x, '__call__')` to test if `x` is callable is unreliable. B305 `.next()` is not a thing on Python 3. Use the `next()` builtin. B306 `BaseException.message` has been deprecated as of Python 2.6 and is removed in Python 3. B007 Loop control variable 'key' not used within the loop body. remain below issues in ignores: E402 module level import not at top of file ./service-mgmt-api/sm-api/sm_api/cmd/__init__.py:25 Hxxx since which are related with document format F811 redefinition of unused '<foo>' from line <x> ./service-mgmt-tools/sm-tools/sm_tools/sm_configure.py:18 F821 undefined name 'e' ./service-mgmt-api/sm-api/sm_api/common/utils.py:448 B006 Do not use mutable data structures for argument defaults. ./service-mgmt-api/sm-api/sm_api/common/service.py:59 B008 Do not perform calls in argument defaults. ./service-mgmt-api/sm-api/sm_api/openstack/common/timeutils.py:117 Test have been done:Build,Deploy,some smc command,such as smc service-list, smc service-show, sm-dump, etc Story: 2003430 Task: 26524 Change-Id: I3e2a4a31f87e3ff66cfce86f54285e830ee1c3dc Signed-off-by: Sun Austin <austin.sun@intel.com>
98 lines
3.7 KiB
Python
98 lines
3.7 KiB
Python
# -*- encoding: utf-8 -*-
|
|
#
|
|
# Copyright © 2012 New Dream Network, LLC (DreamHost)
|
|
#
|
|
# Author: Doug Hellmann <doug.hellmann@dreamhost.com>
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
#
|
|
# Copyright (c) 2013-2014 Wind River Systems, Inc.
|
|
#
|
|
|
|
"""
|
|
Middleware to replace the plain text message body of an error
|
|
response with one formatted so the client can parse it.
|
|
|
|
Based on pecan.middleware.errordocument
|
|
"""
|
|
|
|
import json
|
|
import webob
|
|
from xml import etree as et
|
|
|
|
from sm_api.openstack.common import log
|
|
|
|
LOG = log.getLogger(__name__)
|
|
|
|
|
|
class ParsableErrorMiddleware(object):
|
|
"""Replace error body with something the client can parse.
|
|
"""
|
|
|
|
def __init__(self, app):
|
|
self.app = app
|
|
|
|
def __call__(self, environ, start_response):
|
|
# Request for this state, modified by replace_start_response()
|
|
# and used when an error is being reported.
|
|
state = {}
|
|
|
|
def replacement_start_response(status, headers, exc_info=None):
|
|
"""Overrides the default response to make errors parsable.
|
|
"""
|
|
try:
|
|
status_code = int(status.split(' ')[0])
|
|
state['status_code'] = status_code
|
|
except (ValueError, TypeError): # pragma: nocover
|
|
raise Exception((
|
|
'ErrorDocumentMiddleware received an invalid '
|
|
'status %s' % status
|
|
))
|
|
else:
|
|
if (state['status_code'] / 100) not in (2, 3):
|
|
# Remove some headers so we can replace them later
|
|
# when we have the full error message and can
|
|
# compute the length.
|
|
headers = [(h, v)
|
|
for (h, v) in headers
|
|
if h not in ('Content-Length', 'Content-Type')
|
|
]
|
|
# Save the headers in case we need to modify them.
|
|
state['headers'] = headers
|
|
return start_response(status, headers, exc_info)
|
|
|
|
app_iter = self.app(environ, replacement_start_response)
|
|
if (state['status_code'] / 100) not in (2, 3):
|
|
req = webob.Request(environ)
|
|
if (req.accept.best_match(
|
|
['application/json', 'application/xml']) ==
|
|
'application/xml'):
|
|
try:
|
|
# simple check xml is valid
|
|
body = [et.ElementTree.tostring(
|
|
et.ElementTree.fromstring('<error_message>' +
|
|
'\n'.join(app_iter) +
|
|
'</error_message>'))]
|
|
except et.ElementTree.ParseError as err:
|
|
LOG.error('Error parsing HTTP response: %s' % err)
|
|
body = ['<error_message>%s' % state['status_code'] +
|
|
'</error_message>']
|
|
state['headers'].append(('Content-Type', 'application/xml'))
|
|
else:
|
|
body = [json.dumps({'error_message': '\n'.join(app_iter)})]
|
|
state['headers'].append(('Content-Type', 'application/json'))
|
|
state['headers'].append(('Content-Length', len(body[0])))
|
|
else:
|
|
body = app_iter
|
|
return body
|