utils: replace bool_from_string by builtin distutils strtobool

Change-Id: I68777282cf008658ce8d343a63b904e7f7ba720b
This commit is contained in:
Julien Danjou 2017-04-18 15:47:24 +02:00
parent f8569ccab6
commit 4278189688
5 changed files with 31 additions and 24 deletions

View File

@ -16,7 +16,6 @@
import datetime import datetime
import numpy import numpy
from oslo_utils import strutils
from oslo_utils import timeutils from oslo_utils import timeutils
import pandas import pandas
import six import six
@ -75,7 +74,7 @@ class MovingAverage(aggregates.CustomAggregator):
""" """
if center: if center:
center = strutils.bool_from_string(center) center = utils.strtobool(center)
def moving_window(x): def moving_window(x):
msec = datetime.timedelta(milliseconds=1) msec = datetime.timedelta(milliseconds=1)

View File

@ -21,7 +21,6 @@ import uuid
from concurrent import futures from concurrent import futures
import jsonpatch import jsonpatch
from oslo_utils import dictutils from oslo_utils import dictutils
from oslo_utils import strutils
import pecan import pecan
from pecan import rest from pecan import rest
import pyparsing import pyparsing
@ -138,16 +137,8 @@ def Timespan(value):
def get_header_option(name, params): def get_header_option(name, params):
type, options = werkzeug.http.parse_options_header( type, options = werkzeug.http.parse_options_header(
pecan.request.headers.get('Accept')) pecan.request.headers.get('Accept'))
try: return strtobool('Accept header' if name in options else name,
return strutils.bool_from_string( options.get(name, params.pop(name, 'false')))
options.get(name, params.pop(name, 'false')),
strict=True)
except ValueError as e:
method = 'Accept' if name in options else 'query'
abort(
400,
"Unable to parse %s value in %s: %s"
% (name, method, six.text_type(e)))
def get_history(params): def get_history(params):
@ -158,6 +149,17 @@ def get_details(params):
return get_header_option('details', params) return get_header_option('details', params)
def strtobool(varname, v):
"""Convert a string to a boolean.
Default to false if unable to convert.
"""
try:
return utils.strtobool(v)
except ValueError as e:
abort(400, "Unable to parse `%s': %s" % (varname, six.text_type(e)))
RESOURCE_DEFAULT_PAGINATION = ['revision_start:asc', RESOURCE_DEFAULT_PAGINATION = ['revision_start:asc',
'started_at:asc'] 'started_at:asc']
@ -427,7 +429,7 @@ class MetricController(rest.RestController):
except ValueError as e: except ValueError as e:
abort(400, e) abort(400, e)
if strutils.bool_from_string(refresh): if strtobool("refresh", refresh):
pecan.request.storage.process_new_measures( pecan.request.storage.process_new_measures(
pecan.request.indexer, [six.text_type(self.metric.id)], True) pecan.request.indexer, [six.text_type(self.metric.id)], True)
@ -1366,7 +1368,7 @@ class ResourcesMetricsMeasuresBatchController(rest.RestController):
names=names, resource_id=resource_id) names=names, resource_id=resource_id)
known_names = [m.name for m in metrics] known_names = [m.name for m in metrics]
if strutils.bool_from_string(create_metrics): if strtobool("create_metrics", create_metrics):
already_exists_names = [] already_exists_names = []
for name in names: for name in names:
if name not in known_names: if name not in known_names:
@ -1603,7 +1605,7 @@ class AggregationController(rest.RestController):
abort(400, "fill must be a float or \'null\': %s" % e) abort(400, "fill must be a float or \'null\': %s" % e)
try: try:
if strutils.bool_from_string(refresh): if strtobool("refresh", refresh):
pecan.request.storage.process_new_measures( pecan.request.storage.process_new_measures(
pecan.request.indexer, pecan.request.indexer,
[six.text_type(m.id) for m in metrics], True) [six.text_type(m.id) for m in metrics], True)
@ -1667,7 +1669,7 @@ class StatusController(rest.RestController):
enforce("get status", {}) enforce("get status", {})
try: try:
report = pecan.request.storage.incoming.measures_report( report = pecan.request.storage.incoming.measures_report(
strutils.bool_from_string(details)) strtobool("details", details))
except incoming.ReportGenerationError: except incoming.ReportGenerationError:
abort(503, 'Unable to generate status. Please retry.') abort(503, 'Unable to generate status. Please retry.')
report_dict = {"storage": {"summary": report['summary']}} report_dict = {"storage": {"summary": report['summary']}}

View File

@ -18,8 +18,6 @@ from __future__ import absolute_import
from six.moves.urllib import parse from six.moves.urllib import parse
from oslo_utils import strutils
try: try:
import redis import redis
from redis import sentinel from redis import sentinel
@ -27,6 +25,8 @@ except ImportError:
redis = None redis = None
sentinel = None sentinel = None
from gnocchi import utils
SEP = ':' SEP = ':'
@ -96,7 +96,7 @@ def get_client(conf):
if a not in options: if a not in options:
continue continue
if a in CLIENT_BOOL_ARGS: if a in CLIENT_BOOL_ARGS:
v = strutils.bool_from_string(options[a][-1]) v = utils.strtobool(options[a][-1])
elif a in CLIENT_LIST_ARGS: elif a in CLIENT_LIST_ARGS:
v = options[a][-1] v = options[a][-1]
elif a in CLIENT_INT_ARGS: elif a in CLIENT_INT_ARGS:

View File

@ -1450,8 +1450,7 @@ class ResourceTest(RestTest):
result = self.app.get("/v1/resource/generic?details=awesome", result = self.app.get("/v1/resource/generic?details=awesome",
status=400) status=400)
self.assertIn( self.assertIn(
b"Unable to parse details value in query: " b"Unable to parse `details': invalid truth value",
b"Unrecognized value 'awesome', acceptable values are",
result.body) result.body)
def test_list_resources_with_bad_details_in_accept(self): def test_list_resources_with_bad_details_in_accept(self):
@ -1461,8 +1460,7 @@ class ResourceTest(RestTest):
}, },
status=400) status=400)
self.assertIn( self.assertIn(
b"Unable to parse details value in Accept: " b"Unable to parse `Accept header': invalid truth value",
b"Unrecognized value 'foo', acceptable values are",
result.body) result.body)
def _do_test_list_resources_with_detail(self, request): def _do_test_list_resources_with_detail(self, request):

View File

@ -1,5 +1,6 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
# #
# Copyright © 2015-2017 Red Hat, Inc.
# Copyright © 2015-2016 eNovance # Copyright © 2015-2016 eNovance
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -14,6 +15,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import datetime import datetime
import distutils.util
import errno import errno
import itertools import itertools
import multiprocessing import multiprocessing
@ -206,3 +208,9 @@ def ensure_paths(paths):
except OSError as e: except OSError as e:
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
raise raise
def strtobool(v):
if isinstance(v, bool):
return v
return bool(distutils.util.strtobool(v))