Use oslo.utils

Modules `strutils`, `timeutils` and `network_utils` from common code are
graduated in `oslo.utils`, so we can:
 1. remove `novaclient.openstack.common.network_utils` and use
    `oslo.utils.netutils` instead.
 2. use `oslo.utils.encodeutils` and `oslo.utils.strutils` instead of
    `novaclient.openstack.common.strutils`.
 3. use `oslo.utils.timeutils` instead of
    `novaclient.openstack.common.timeutils`.

Additional information:
 - modules `importutils`, `strutils` and `timeutils` from
   `novaclient.openstack.common` cannot be removed, because:
   - importutils is used by apiclient and jsonutils;
   - strutils is used by apiclient, cliutils and jsonutils;
   - timeutils is used by jsonutils
 - additional check for `safe_encode` in Py3 is required, since
   If91a866d864a22d28a352152beff4c7406a27b7b was merged.

Change-Id: Ib8d79d9c85af4916e87a76a1a67a13488ddaa111
This commit is contained in:
Andrey Kurilin 2014-08-27 18:08:14 +03:00
parent aa30c13fc5
commit 392148c7ef
15 changed files with 44 additions and 140 deletions

View File

@ -31,6 +31,7 @@ import re
import time
from keystoneclient import adapter
from oslo.utils import netutils
import requests
from requests import adapters
@ -43,7 +44,6 @@ from six.moves.urllib import parse
from novaclient import exceptions
from novaclient.openstack.common.gettextutils import _
from novaclient.openstack.common import network_utils
from novaclient import service_catalog
from novaclient import utils
@ -558,7 +558,7 @@ class HTTPClient(object):
extract_token=False)
def authenticate(self):
magic_tuple = network_utils.urlsplit(self.auth_url)
magic_tuple = netutils.urlsplit(self.auth_url)
scheme, netloc, path, query, frag = magic_tuple
port = magic_tuple.port
if port is None:

View File

@ -1,108 +0,0 @@
# Copyright 2012 OpenStack Foundation.
# All Rights Reserved.
#
# 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.
"""
Network-related utilities and helper functions.
"""
# TODO(jd) Use six.moves once
# https://bitbucket.org/gutworth/six/pull-request/28
# is merged
try:
import urllib.parse
SplitResult = urllib.parse.SplitResult
except ImportError:
import urlparse
SplitResult = urlparse.SplitResult
from six.moves.urllib import parse
def parse_host_port(address, default_port=None):
"""Interpret a string as a host:port pair.
An IPv6 address MUST be escaped if accompanied by a port,
because otherwise ambiguity ensues: 2001:db8:85a3::8a2e:370:7334
means both [2001:db8:85a3::8a2e:370:7334] and
[2001:db8:85a3::8a2e:370]:7334.
>>> parse_host_port('server01:80')
('server01', 80)
>>> parse_host_port('server01')
('server01', None)
>>> parse_host_port('server01', default_port=1234)
('server01', 1234)
>>> parse_host_port('[::1]:80')
('::1', 80)
>>> parse_host_port('[::1]')
('::1', None)
>>> parse_host_port('[::1]', default_port=1234)
('::1', 1234)
>>> parse_host_port('2001:db8:85a3::8a2e:370:7334', default_port=1234)
('2001:db8:85a3::8a2e:370:7334', 1234)
"""
if address[0] == '[':
# Escaped ipv6
_host, _port = address[1:].split(']')
host = _host
if ':' in _port:
port = _port.split(':')[1]
else:
port = default_port
else:
if address.count(':') == 1:
host, port = address.split(':')
else:
# 0 means ipv4, >1 means ipv6.
# We prohibit unescaped ipv6 addresses with port.
host = address
port = default_port
return (host, None if port is None else int(port))
class ModifiedSplitResult(SplitResult):
"""Split results class for urlsplit."""
# NOTE(dims): The functions below are needed for Python 2.6.x.
# We can remove these when we drop support for 2.6.x.
@property
def hostname(self):
netloc = self.netloc.split('@', 1)[-1]
host, port = parse_host_port(netloc)
return host
@property
def port(self):
netloc = self.netloc.split('@', 1)[-1]
host, port = parse_host_port(netloc)
return port
def urlsplit(url, scheme='', allow_fragments=True):
"""Parse a URL using urlparse.urlsplit(), splitting query and fragments.
This function papers over Python issue9374 when needed.
The parameters are the same as urlparse.urlsplit.
"""
scheme, netloc, path, query, fragment = parse.urlsplit(
url, scheme, allow_fragments)
if allow_fragments and '#' in path:
path, fragment = path.split('#', 1)
if '?' in path:
path, query = path.split('?', 1)
return ModifiedSplitResult(scheme, netloc,
path, query, fragment)

View File

@ -29,6 +29,8 @@ import os
import pkgutil
import sys
from oslo.utils import encodeutils
from oslo.utils import strutils
import pkg_resources
import six
@ -47,7 +49,6 @@ from novaclient import exceptions as exc
import novaclient.extension
from novaclient.openstack.common import cliutils
from novaclient.openstack.common.gettextutils import _
from novaclient.openstack.common import strutils
from novaclient import utils
from novaclient.v1_1 import shell as shell_v1_1
from novaclient.v3 import shell as shell_v3
@ -795,13 +796,13 @@ class OpenStackHelpFormatter(argparse.HelpFormatter):
def main():
try:
argv = [strutils.safe_decode(a) for a in sys.argv[1:]]
argv = [encodeutils.safe_decode(a) for a in sys.argv[1:]]
OpenStackComputeShell().main(argv)
except Exception as e:
logger.debug(e, exc_info=1)
details = {'name': strutils.safe_encode(e.__class__.__name__),
'msg': strutils.safe_encode(six.text_type(e))}
details = {'name': encodeutils.safe_encode(e.__class__.__name__),
'msg': encodeutils.safe_encode(six.text_type(e))}
print("ERROR (%(name)s): %(msg)s" % details,
file=sys.stderr)
sys.exit(1)

View File

@ -16,12 +16,12 @@
import datetime
from oslo.utils import strutils
import six
from six.moves.urllib import parse
from novaclient import client as base_client
from novaclient import exceptions
from novaclient.openstack.common import strutils
from novaclient.tests import fakes
from novaclient.tests import utils
from novaclient.v1_1 import client

View File

@ -22,6 +22,7 @@ import os
import fixtures
import mock
from oslo.utils import timeutils
import six
from six.moves import builtins
@ -1232,9 +1233,9 @@ class ShellTest(utils.TestCase):
'end=2005-02-01T00:00:00&' +
'detailed=1')
@mock.patch('novaclient.openstack.common.timeutils.utcnow')
def test_usage_list_no_args(self, mock_utcnow):
mock_utcnow.return_value = datetime.datetime(2005, 2, 1, 0, 0)
def test_usage_list_no_args(self):
timeutils.set_time_override(datetime.datetime(2005, 2, 1, 0, 0))
self.addCleanup(timeutils.clear_time_override)
self.run_command('usage-list')
self.assert_called('GET',
'/os-simple-tenant-usage?' +

View File

@ -16,7 +16,8 @@
import datetime
from novaclient.openstack.common import strutils
from oslo.utils import strutils
from novaclient.tests import fakes
from novaclient.tests.v1_1 import fakes as fakes_v1_1
from novaclient.v3 import client

View File

@ -17,6 +17,7 @@ import sys
import textwrap
import uuid
from oslo.utils import encodeutils
import pkg_resources
import prettytable
import six
@ -25,7 +26,6 @@ from novaclient import exceptions
from novaclient.openstack.common import cliutils
from novaclient.openstack.common.gettextutils import _
from novaclient.openstack.common import jsonutils
from novaclient.openstack.common import strutils
arg = cliutils.arg
@ -127,9 +127,12 @@ def print_list(objs, fields, formatters={}, sortby_index=None):
pt.add_row(row)
if sortby is not None:
result = strutils.safe_encode(pt.get_string(sortby=sortby))
result = encodeutils.safe_encode(pt.get_string(sortby=sortby))
else:
result = strutils.safe_encode(pt.get_string())
result = encodeutils.safe_encode(pt.get_string())
if six.PY3:
result = result.decode()
print(result)
@ -197,7 +200,10 @@ def print_dict(d, dict_property="Property", dict_value="Value", wrap=0):
v = '-'
pt.add_row([k, v])
result = strutils.safe_encode(pt.get_string())
result = encodeutils.safe_encode(pt.get_string())
if six.PY3:
result = result.decode()
print(result)
@ -219,7 +225,11 @@ def find_resource(manager, name_or_id, **find_args):
# now try to get entity as uuid
try:
tmp_id = strutils.safe_encode(name_or_id)
tmp_id = encodeutils.safe_encode(name_or_id)
if six.PY3:
tmp_id = tmp_id.decode()
uuid.UUID(tmp_id)
return manager.get(tmp_id)
except (TypeError, ValueError, exceptions.NotFound):

View File

@ -16,12 +16,12 @@
Flavor interface.
"""
from oslo.utils import strutils
from six.moves.urllib import parse
from novaclient import base
from novaclient import exceptions
from novaclient.openstack.common.gettextutils import _
from novaclient.openstack.common import strutils
from novaclient import utils

View File

@ -21,13 +21,13 @@ Server interface.
import base64
from oslo.utils import encodeutils
import six
from six.moves.urllib import parse
from novaclient import base
from novaclient import crypto
from novaclient.openstack.common.gettextutils import _
from novaclient.openstack.common import strutils
from novaclient.v1_1 import security_groups
@ -454,7 +454,7 @@ class ServerManager(base.BootingManagerWithFind):
if six.PY3:
userdata = userdata.encode("utf-8")
else:
userdata = strutils.safe_encode(userdata)
userdata = encodeutils.safe_encode(userdata)
userdata_b64 = base64.b64encode(userdata).decode('utf-8')
body["server"]["user_data"] = userdata_b64

View File

@ -28,12 +28,13 @@ import os
import sys
import time
from oslo.utils import encodeutils
from oslo.utils import strutils
from oslo.utils import timeutils
import six
from novaclient import exceptions
from novaclient.openstack.common.gettextutils import _
from novaclient.openstack.common import strutils
from novaclient.openstack.common import timeutils
from novaclient.openstack.common import uuidutils
from novaclient import utils
from novaclient.v1_1 import availability_zones
@ -2301,7 +2302,7 @@ def _print_secgroups(secgroups):
def _get_secgroup(cs, secgroup):
# Check secgroup is an ID (nova-network) or UUID (neutron)
if (utils.is_integer_like(strutils.safe_encode(secgroup))
if (utils.is_integer_like(encodeutils.safe_encode(secgroup))
or uuidutils.is_uuid_like(secgroup)):
try:
return cs.security_groups.get(secgroup)

View File

@ -17,10 +17,11 @@
Image interface.
"""
from oslo.utils import encodeutils
from oslo.utils import strutils
from six.moves.urllib import parse
from novaclient import base
from novaclient.openstack.common import strutils
class Image(base.Resource):
@ -51,7 +52,7 @@ class ImageManager(base.ManagerWithFind):
def _image_meta_from_headers(self, headers):
meta = {'properties': {}}
safe_decode = strutils.safe_decode
safe_decode = encodeutils.safe_decode
for key, value in headers.items():
value = safe_decode(value, incoming='utf-8')
if key.startswith('x-image-meta-property-'):

View File

@ -21,13 +21,13 @@ Server interface.
import base64
from oslo.utils import encodeutils
import six
from six.moves.urllib import parse
from novaclient import base
from novaclient import crypto
from novaclient.openstack.common.gettextutils import _
from novaclient.openstack.common import strutils
REBOOT_SOFT, REBOOT_HARD = 'SOFT', 'HARD'
@ -402,7 +402,7 @@ class ServerManager(base.BootingManagerWithFind):
if six.PY3:
userdata = userdata.encode("utf-8")
else:
userdata = strutils.safe_encode(userdata)
userdata = encodeutils.safe_encode(userdata)
data = base64.b64encode(userdata).decode('utf-8')
body["server"]["os-user-data:user_data"] = data

View File

@ -27,12 +27,12 @@ import os
import sys
import time
from oslo.utils import strutils
from oslo.utils import timeutils
import six
from novaclient import exceptions
from novaclient.openstack.common.gettextutils import _
from novaclient.openstack.common import strutils
from novaclient.openstack.common import timeutils
from novaclient.openstack.common import uuidutils
from novaclient import utils
from novaclient.v3 import availability_zones

View File

@ -5,11 +5,7 @@ module=apiclient
module=cliutils
module=gettextutils
module=install_venv_common
module=importutils
module=jsonutils
module=network_utils
module=strutils
module=timeutils
module=uuidutils
# The base module to hold the copy of openstack.common

View File

@ -1,6 +1,7 @@
pbr>=0.6,!=0.7,<1.0
argparse
iso8601>=0.1.9
oslo.utils>=0.2.0 # Apache-2.0
PrettyTable>=0.7,<0.8
requests>=1.2.1
simplejson>=2.0.9