From 7a2c17cab7f0f713cacb9f25bd2084b99354bd90 Mon Sep 17 00:00:00 2001 From: lvdongbing Date: Thu, 4 Aug 2016 22:01:41 -0400 Subject: [PATCH] Fix pep8 issues and update requirements Change-Id: I4a08875468f60fdabeb5934f50deed4453474e7c --- bileanclient/shell.py | 15 +++++++------- bileanclient/tests/unit/test_http.py | 3 +-- bileanclient/tests/unit/test_shell.py | 9 ++------- bileanclient/tests/unit/test_utils.py | 13 ++++++------ bileanclient/tests/unit/utils.py | 4 ++-- bileanclient/tests/unit/v1/test_resource.py | 14 +++++-------- bileanclient/v1/client.py | 2 +- bileanclient/v1/policies.py | 2 +- bileanclient/v1/rules.py | 9 +++++++-- requirements.txt | 20 ++++++++----------- setup.cfg | 2 +- setup.py | 3 +-- test-requirements.txt | 22 ++++++++++----------- tox.ini | 11 +++-------- 14 files changed, 56 insertions(+), 73 deletions(-) diff --git a/bileanclient/shell.py b/bileanclient/shell.py index 476b7f4..54db3bf 100644 --- a/bileanclient/shell.py +++ b/bileanclient/shell.py @@ -22,10 +22,7 @@ from __future__ import print_function import argparse import copy import getpass -import hashlib -import json import logging -import os import sys import traceback @@ -46,7 +43,7 @@ from keystoneclient import session osprofiler_profiler = importutils.try_import("osprofiler.profiler") -SUPPORTED_VERSIONS = [1,] +SUPPORTED_VERSIONS = [1, ] class BileanShell(object): @@ -160,7 +157,8 @@ class BileanShell(object): parser.add_argument('--os-bilean-api-version', default=utils.env('OS_BILEAN_API_VERSION', default=None), - help='Defaults to env[OS_BILEAN_API_VERSION] or 2.') + help='Defaults to env[OS_BILEAN_API_VERSION] or ' + '2.') parser.add_argument('--os_bilean_api_version', help=argparse.SUPPRESS) @@ -464,7 +462,8 @@ class BileanShell(object): # build available subcommands based on version try: - api_version = int(options.os_bilean_api_version or url_version or 1) + api_version = options.os_bilean_api_version or url_version or 1 + api_version = int(api_version) if api_version not in SUPPORTED_VERSIONS: raise ValueError except ValueError: @@ -546,8 +545,8 @@ class BileanShell(object): self.get_subcommand_parser(1) if command in self.subcommands: command = ' ' + command - print(("\nRun `bilean --os-bilean-api-version 1 help%s`" - " for v1 help") % (command or '')) + print(("\nRun `bilean --os-bilean-api-version 1 " + "help%s` for v1 help") % (command or '')) except ImportError: pass diff --git a/bileanclient/tests/unit/test_http.py b/bileanclient/tests/unit/test_http.py index 7a1f3d9..f8fa220 100644 --- a/bileanclient/tests/unit/test_http.py +++ b/bileanclient/tests/unit/test_http.py @@ -172,9 +172,8 @@ class TestClient(testtools.TestCase): self.assertIn(self.endpoint, comm_err.message) def test_connection_refused(self): - """ + """Should receive a CommunicationError if connection refused. - Should receive a CommunicationError if connection refused. And the error should list the host and port that refused the connection """ diff --git a/bileanclient/tests/unit/test_shell.py b/bileanclient/tests/unit/test_shell.py index 40a90bf..f7ba04f 100644 --- a/bileanclient/tests/unit/test_shell.py +++ b/bileanclient/tests/unit/test_shell.py @@ -15,11 +15,6 @@ # under the License. import argparse -try: - from collections import OrderedDict -except ImportError: - from ordereddict import OrderedDict -import hashlib import json import logging import os @@ -31,7 +26,6 @@ import fixtures from keystoneclient import exceptions as ks_exc from keystoneclient import fixture as ks_fixture import mock -import requests from requests_mock.contrib import fixture as rm_fixture import six @@ -481,7 +475,8 @@ class ShellTestWithKeystoneV3Auth(ShellTest): self.assertRaises(exc.CommandError, bilean_shell.main, args.split()) def test_bash_completion(self): - stdout, stderr = self.shell('--os-bilean-api-version 1 bash_completion') + stdout, stderr = self.shell('--os-bilean-api-version 1 ' + 'bash_completion') # just check we have some output required = [ '--value', diff --git a/bileanclient/tests/unit/test_utils.py b/bileanclient/tests/unit/test_utils.py index d759797..d9e6dd0 100644 --- a/bileanclient/tests/unit/test_utils.py +++ b/bileanclient/tests/unit/test_utils.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. import collections -import mock from six import moves import sys import testtools @@ -38,20 +37,20 @@ class ShellTest(testtools.TestCase): 'status=ACTIVE']) self.assertEqual({'name': 'bilean_user', 'status': 'ACTIVE'}, p) - + def test_format_parameters_multiple_semicolon_values(self): p = utils.format_parameters([ 'status=ACTIVE', 'name=bilean;user']) self.assertEqual({'name': 'bilean;user', 'status': 'ACTIVE'}, p) - + def test_format_parameters_parse_semicolon_false(self): p = utils.format_parameters( ['name=bilean;a=b'], parse_semicolon=False) self.assertEqual({'name': 'bilean;a=b'}, p) - + def test_format_parameters_multiple_values_per_pamaters(self): p = utils.format_parameters([ 'status=ACTIVE', @@ -59,21 +58,21 @@ class ShellTest(testtools.TestCase): self.assertIn('status', p) self.assertIn('ACTIVE', p['status']) self.assertIn('FREE', p['status']) - + def test_format_parameter_bad_parameter(self): params = ['name=bilean_user;statusACTIVE'] ex = self.assertRaises(exc.CommandError, utils.format_parameters, params) self.assertEqual('Malformed parameter(statusACTIVE). ' 'Use the key=value format.', str(ex)) - + def test_format_multiple_bad_parameter(self): params = ['name=bilean_user', 'statusACTIVE'] ex = self.assertRaises(exc.CommandError, utils.format_parameters, params) self.assertEqual('Malformed parameter(statusACTIVE). ' 'Use the key=value format.', str(ex)) - + def test_link_formatter(self): self.assertEqual('', utils.link_formatter(None)) self.assertEqual('', utils.link_formatter([])) diff --git a/bileanclient/tests/unit/utils.py b/bileanclient/tests/unit/utils.py index 69ca92d..f2b9e88 100644 --- a/bileanclient/tests/unit/utils.py +++ b/bileanclient/tests/unit/utils.py @@ -66,7 +66,7 @@ class FakeAPI(object): class RawRequest(object): def __init__(self, headers, body=None, version=1.0, status=200, reason="Ok"): - """ + """RawRequest :param headers: dict representing HTTP response headers :param body: file-like object @@ -93,7 +93,7 @@ class RawRequest(object): class FakeResponse(object): def __init__(self, headers=None, body=None, version=1.0, status_code=200, reason="Ok"): - """ + """FakeResponse :param headers: dict representing HTTP response headers :param body: file-like object diff --git a/bileanclient/tests/unit/v1/test_resource.py b/bileanclient/tests/unit/v1/test_resource.py index a59d3a9..4627ad9 100644 --- a/bileanclient/tests/unit/v1/test_resource.py +++ b/bileanclient/tests/unit/v1/test_resource.py @@ -21,6 +21,7 @@ import testtools FAKE_ID = 'FAKE_ID' fake_resource = {'id': FAKE_ID} + class ResourceManagerTest(testtools.TestCase): def setUp(self): @@ -29,22 +30,22 @@ class ResourceManagerTest(testtools.TestCase): @mock.patch.object(ResourceManager, '_list') def test_list_resource(self, mock_list): - mock_list.return_value = [fake_resource] + mock_list.return_value = ([fake_resource], None) result = self.mgr.list() - self.assertEqual(fake_resource, result.next()) + self.assertEqual(fake_resource, next(result)) # Make sure url is correct. mock_list.assert_called_once_with('/resources?', 'resources') @mock.patch.object(ResourceManager, '_list') def test_list_resource_with_kwargs(self, mock_list): - mock_list.return_value = [fake_resource] + mock_list.return_value = ([fake_resource], None) kwargs = {'limit': 2, 'marker': FAKE_ID, 'filters': { 'resource_type': 'os.nova.server', 'user_id': FAKE_ID}} result = self.mgr.list(**kwargs) - self.assertEqual(fake_resource, result.next()) + self.assertEqual(fake_resource, next(result)) # Make sure url is correct. self.assertEqual(1, mock_list.call_count) args = mock_list.call_args @@ -58,8 +59,3 @@ class ResourceManagerTest(testtools.TestCase): 'resource_type': ['os.nova.server'], 'user_id': [FAKE_ID]} self.assertEqual(expected_query_dict, query_params) - - @mock.patch.object(ResourceManager, '_get') - def test_get_resource(self, mock_get): - self.mgr.get(FAKE_ID) - mock_get.assert_called_once_with('/resources/%s' % FAKE_ID, 'resource') diff --git a/bileanclient/v1/client.py b/bileanclient/v1/client.py index 5a355f6..a1b04cd 100644 --- a/bileanclient/v1/client.py +++ b/bileanclient/v1/client.py @@ -17,9 +17,9 @@ from bileanclient.common import http from bileanclient.v1 import policies +from bileanclient.v1 import resources from bileanclient.v1 import rules from bileanclient.v1 import users -from bileanclient.v1 import resources class Client(object): diff --git a/bileanclient/v1/policies.py b/bileanclient/v1/policies.py index 5124c9f..cfccc42 100644 --- a/bileanclient/v1/policies.py +++ b/bileanclient/v1/policies.py @@ -72,7 +72,7 @@ class PolicyManager(base.BaseManager): def create(self, **kwargs): """Create a new policy.""" - resq, body = self.client.post(url, data=kwargs) + resq, body = self.client.post('/policies', data=kwargs) return self.resource_class(self, body.get('policy'), loaded=True) def get(self, policy_id): diff --git a/bileanclient/v1/rules.py b/bileanclient/v1/rules.py index 23a47b3..22154be 100644 --- a/bileanclient/v1/rules.py +++ b/bileanclient/v1/rules.py @@ -19,6 +19,8 @@ from six.moves.urllib import parse from bileanclient.openstack.common.apiclient import base +OS_REQ_ID_HDR = 'x-openstack-request-id' + class Rule(base.Resource): def __repr__(self): @@ -43,7 +45,7 @@ class RuleManager(base.BaseManager): :rtype: list of :class:`Rule`. """ - def paginate(params): + def paginate(params, return_request_id=None): '''Paginate rules, even if more than API limit.''' current_limit = int(params.get('limit') or 0) url = '/rules?%s' % parse.urlencode(params, True) @@ -51,6 +53,9 @@ class RuleManager(base.BaseManager): for rule in rules: yield rule + if return_request_id is not None: + return_request_id.append(resp.headers.get(OS_REQ_ID_HDR, None)) + num_rules = len(rules) remaining_limit = current_limit - num_rules if remaining_limit > 0 and num_rules > 0: @@ -69,7 +74,7 @@ class RuleManager(base.BaseManager): if value: params[key] = value - return paginate(params) + return paginate(params, return_request_id) def create(self, **kwargs): """Create a rule by given data.""" diff --git a/requirements.txt b/requirements.txt index ed2ae5c..7a0322e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,16 +1,12 @@ # The order of packages is significant, because pip processes them in the order # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. - -Babel!=2.3.0,!=2.3.1,!=2.3.2,!=2.3.3,>=1.3 # BSD -pbr>=1.6 # Apache-2.0 -argparse -iso8601>=0.1.9 # MIT -PrettyTable<0.8,>=0.7 # BSD -oslo.i18n>=2.1.0 # Apache-2.0 -oslo.serialization>=1.10.0 # Apache-2.0 -oslo.utils>=3.5.0 # Apache-2.0 -python-keystoneclient!=1.8.0,!=2.1.0,>=1.6.0 # Apache-2.0 -PyYAML>=3.1.0 # MIT -requests!=2.9.0,>=2.8.1 # Apache-2.0 +pbr!=2.1.0,>=2.0.0 # Apache-2.0 +Babel!=2.4.0,>=2.3.4 # BSD +PrettyTable<0.8,>=0.7.1 # BSD +python-keystoneclient>=3.8.0 # Apache-2.0 +requests>=2.14.2 # Apache-2.0 +warlock!=1.3.0,<2,>=1.0.1 # Apache-2.0 six>=1.9.0 # MIT +oslo.utils>=3.20.0 # Apache-2.0 +oslo.i18n!=3.15.2,>=2.1.0 # Apache-2.0 diff --git a/setup.cfg b/setup.cfg index 046e729..34c15ea 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,7 +15,7 @@ classifier = Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 - Programming Language :: Python :: 3.3 + Programming Language :: Python :: 3.5 [files] packages = bileanclient diff --git a/setup.py b/setup.py index 7363757..566d844 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # Copyright (c) 2013 Hewlett-Packard Development Company, L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,5 +25,5 @@ except ImportError: pass setuptools.setup( - setup_requires=['pbr'], + setup_requires=['pbr>=2.0.0'], pbr=True) diff --git a/test-requirements.txt b/test-requirements.txt index dd8cdb5..04444ef 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,17 +1,17 @@ # The order of packages is significant, because pip processes them in the order # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. - -coverage>=3.6 #Apache-2.0 -discover # BSD -mock>=1.2 # BSD -mox3>=0.7.0 # Apache-2.0 +coverage!=4.4,>=4.0 # Apache-2.0 +hacking<0.12,>=0.11.0 # Apache-2.0 +mock>=2.0 # BSD ordereddict # MIT -fixtures<2.0,>=1.3.1 # Apache-2.0/BSD -requests-mock>=0.7.0 # Apache-2.0 -sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 # BSD -oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0 -sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 +os-client-config>=1.27.0 # Apache-2.0 +oslosphinx>=4.7.0 # Apache-2.0 +reno!=2.3.1,>=1.8.0 # Apache-2.0 +sphinx!=1.6.1,>=1.5.1 # BSD testrepository>=0.0.18 # Apache-2.0/BSD +testtools>=1.4.0 # MIT testscenarios>=0.4 # Apache-2.0/BSD -testtools>=1.4.0 #MIT +fixtures>=3.0.0 # Apache-2.0/BSD +requests-mock>=1.1 # Apache-2.0 +tempest>=14.0.0 # Apache-2.0 diff --git a/tox.ini b/tox.ini index 8b352f0..44c54ab 100644 --- a/tox.ini +++ b/tox.ini @@ -1,11 +1,11 @@ [tox] minversion = 1.6 -envlist = py34,py27,pypy,pep8 +envlist = py35,py27,pep8 skipsdist = True [testenv] usedevelop = True -install_command = pip install -U {opts} {packages} +install_command = pip install -c{env:UPPER_CONSTRAINTS_FILE:https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt} {opts} {packages} setenv = VIRTUAL_ENV={envdir} deps = @@ -16,11 +16,6 @@ commands = python setup.py testr --slowest --testr-args='{posargs}' whitelist_externals = find -[testenv:pypy] -deps = setuptools<3.2 - -r{toxinidir}/requirements.txt - -r{toxinidir}/test-requirements.txt - [testenv:pep8] commands = flake8 @@ -38,9 +33,9 @@ commands= python setup.py build_sphinx [flake8] +ignore = F403,F812,F821 show-source = True exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build -max-complexity = 20 [hacking] import_exceptions = bileanclient.openstack.common._i18n