neutron-lib: complete usage of helpers

Add support for:
    - get_random_string
    - camelize
    - round_val
    - safe_decode_utf8

Change-Id: I3b5ccbbcfa18aa488b1772f4ae9a591eb494de59
This commit is contained in:
Gary Kotton 2016-10-10 02:35:58 -07:00
parent a36dfabd25
commit 7c0b219e66
6 changed files with 25 additions and 23 deletions

View File

@ -17,6 +17,7 @@ import signal
import eventlet
import eventlet.event
import eventlet.queue
from neutron_lib.utils import helpers
from oslo_log import log as logging
from neutron._i18n import _, _LE
@ -225,7 +226,7 @@ class AsyncProcess(object):
def _read(self, stream, queue):
data = stream.readline()
if data:
data = common_utils.safe_decode_utf8(data.strip())
data = helpers.safe_decode_utf8(data.strip())
queue.put(data)
return data

View File

@ -28,6 +28,7 @@ import eventlet
from eventlet.green import subprocess
from eventlet import greenthread
from neutron_lib import constants
from neutron_lib.utils import helpers
from oslo_config import cfg
from oslo_log import log as logging
from oslo_rootwrap import client
@ -126,8 +127,8 @@ def execute(cmd, process_input=None, addl_env=None,
_stdout, _stderr = obj.communicate(_process_input)
returncode = obj.returncode
obj.stdin.close()
_stdout = utils.safe_decode_utf8(_stdout)
_stderr = utils.safe_decode_utf8(_stderr)
_stdout = helpers.safe_decode_utf8(_stdout)
_stderr = helpers.safe_decode_utf8(_stderr)
extra_ok_codes = extra_ok_codes or []
if returncode and returncode not in extra_ok_codes:

View File

@ -17,6 +17,7 @@ import os
from eventlet.green import subprocess
from eventlet import greenthread
from neutron_lib.utils import helpers
from oslo_log import log as logging
from oslo_utils import encodeutils
@ -57,8 +58,8 @@ def execute(cmd, process_input=None, addl_env=None,
obj, cmd = create_process(cmd, addl_env=addl_env)
_stdout, _stderr = obj.communicate(_process_input)
obj.stdin.close()
_stdout = utils.safe_decode_utf8(_stdout)
_stderr = utils.safe_decode_utf8(_stderr)
_stdout = helpers.safe_decode_utf8(_stdout)
_stderr = helpers.safe_decode_utf8(_stderr)
m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdin: %(stdin)s\n"
"Stdout: %(stdout)s\nStderr: %(stderr)s") % \

View File

@ -18,7 +18,6 @@
"""Utilities and helper functions."""
import decimal
import functools
import importlib
import os
@ -167,11 +166,10 @@ def get_random_mac(base_mac):
return ':'.join(["%02x" % x for x in mac])
@removals.remove(
message="Use get_random_string from neutron_lib.utils.helpers")
def get_random_string(length):
"""Get a random hex string of the specified length.
"""
return "{0:0{1}x}".format(random.getrandbits(length * 4), length)
return helpers.get_random_string(length)
def get_dhcp_agent_device_id(network_id, host):
@ -329,15 +327,16 @@ class DelayedStringRenderer(object):
return str(self.function(*self.args, **self.kwargs))
@removals.remove(
message="Use camelize from neutron_lib.utils.helpers")
def camelize(s):
return ''.join(s.replace('_', ' ').title().split())
return helpers.camelize(s)
@removals.remove(
message="Use round_val from neutron_lib.utils.helpers")
def round_val(val):
# we rely on decimal module since it behaves consistently across Python
# versions (2.x vs. 3.x)
return int(decimal.Decimal(val).quantize(decimal.Decimal('1'),
rounding=decimal.ROUND_HALF_UP))
return helpers.round_val(val)
@removals.remove(
@ -375,10 +374,10 @@ def load_class_by_alias_or_classname(namespace, name):
return class_to_load
@removals.remove(
message="Use safe_decode_utf8 from neutron_lib.utils.helpers")
def safe_decode_utf8(s):
if six.PY3 and isinstance(s, bytes):
return s.decode('utf-8', 'surrogateescape')
return s
return helpers.safe_decode_utf8(s)
def _hex_format(port, mask=0):
@ -857,7 +856,7 @@ def get_related_rand_names(prefixes, max_length=None):
_("'max_length' must be longer than all prefixes"))
else:
length = 8
rndchrs = get_random_string(length)
rndchrs = helpers.get_random_string(length)
return [p + rndchrs for p in prefixes]

View File

@ -17,13 +17,13 @@ import abc
import sys
from neutron_lib import constants
from neutron_lib.utils import helpers
from oslo_utils import versionutils
from oslo_versionedobjects import base as obj_base
from oslo_versionedobjects import exception
from oslo_versionedobjects import fields as obj_fields
import six
from neutron.common import utils
from neutron.db import api as db_api
from neutron.db.qos import models as qos_db_model
from neutron.objects import base
@ -37,7 +37,7 @@ def get_rules(context, qos_policy_id):
all_rules = []
with db_api.autonested_transaction(context.session):
for rule_type in qos_consts.VALID_RULE_TYPES:
rule_cls_name = 'Qos%sRule' % utils.camelize(rule_type)
rule_cls_name = 'Qos%sRule' % helpers.camelize(rule_type)
rule_cls = getattr(sys.modules[__name__], rule_cls_name)
rules = rule_cls.get_objects(context, qos_policy_id=qos_policy_id)

View File

@ -16,12 +16,12 @@
import os
import re
from neutron_lib.utils import helpers
from oslo_log import log as logging
import six
from neutron._i18n import _, _LE, _LW
from neutron.agent.linux import ip_link_support
from neutron.common import utils
from neutron.plugins.ml2.drivers.mech_sriov.agent.common \
import exceptions as exc
from neutron.plugins.ml2.drivers.mech_sriov.agent import pci_lib
@ -183,7 +183,7 @@ class EmbSwitch(object):
if rate_kbps > 0 and rate_kbps < 1000:
rate_mbps = 1
else:
rate_mbps = utils.round_val(rate_kbps / 1000.0)
rate_mbps = helpers.round_val(rate_kbps / 1000.0)
log_dict = {
'rate_mbps': rate_mbps,