From 231e4c6cf8f57b96ca10d52d07b38d221eba5c21 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Wed, 4 Jun 2014 19:28:18 -0700 Subject: [PATCH] Remove misc.as_bool as oslo provides an equivalent There isn't a need to have a misc.as_bool function anymore now that we have imported the oslo incubator strutils module since that module provides a function that does *nearly* the same thing. Change-Id: I7afe141d5a37c50b0c926144743f9af71db95bbf --- .../persistence/backends/impl_sqlalchemy.py | 23 +++++++++++++++---- taskflow/utils/misc.py | 12 ---------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/taskflow/persistence/backends/impl_sqlalchemy.py b/taskflow/persistence/backends/impl_sqlalchemy.py index 81f053ea..26cc4d27 100644 --- a/taskflow/persistence/backends/impl_sqlalchemy.py +++ b/taskflow/persistence/backends/impl_sqlalchemy.py @@ -32,6 +32,7 @@ from sqlalchemy import orm as sa_orm from sqlalchemy import pool as sa_pool from taskflow import exceptions as exc +from taskflow.openstack.common import strutils from taskflow.persistence.backends import base from taskflow.persistence.backends.sqlalchemy import migration from taskflow.persistence.backends.sqlalchemy import models @@ -120,6 +121,18 @@ def _is_db_connection_error(reason): return _in_any(reason, list(MY_SQL_CONN_ERRORS + POSTGRES_CONN_ERRORS)) +def _as_bool(value): + if isinstance(value, bool): + return value + # This is different than strutils, but imho is an acceptable difference. + if value is None: + return False + # NOTE(harlowja): prefer strictness to avoid users getting accustomed + # to passing bad values in and this *just working* (which imho is a bad + # habit to encourage). + return strutils.bool_from_string(value, strict=True) + + def _thread_yield(dbapi_con, con_record): """Ensure other greenthreads get a chance to be executed. @@ -183,8 +196,8 @@ class SQLAlchemyBackend(base.Backend): # all the popping that will happen below. conf = copy.deepcopy(self._conf) engine_args = { - 'echo': misc.as_bool(conf.pop('echo', False)), - 'convert_unicode': misc.as_bool(conf.pop('convert_unicode', True)), + 'echo': _as_bool(conf.pop('echo', False)), + 'convert_unicode': _as_bool(conf.pop('convert_unicode', True)), 'pool_recycle': 3600, } if 'idle_timeout' in conf: @@ -229,13 +242,13 @@ class SQLAlchemyBackend(base.Backend): engine = sa.create_engine(sql_connection, **engine_args) checkin_yield = conf.pop('checkin_yield', eventlet_utils.EVENTLET_AVAILABLE) - if misc.as_bool(checkin_yield): + if _as_bool(checkin_yield): sa.event.listen(engine, 'checkin', _thread_yield) if 'mysql' in e_url.drivername: - if misc.as_bool(conf.pop('checkout_ping', True)): + if _as_bool(conf.pop('checkout_ping', True)): sa.event.listen(engine, 'checkout', _ping_listener) mode = None - if misc.as_bool(conf.pop('mysql_traditional_mode', True)): + if _as_bool(conf.pop('mysql_traditional_mode', True)): mode = 'TRADITIONAL' if 'mysql_sql_mode' in conf: mode = conf.pop('mysql_sql_mode') diff --git a/taskflow/utils/misc.py b/taskflow/utils/misc.py index d2360c9b..b69089f9 100644 --- a/taskflow/utils/misc.py +++ b/taskflow/utils/misc.py @@ -411,18 +411,6 @@ class ExponentialBackoff(object): return "ExponentialBackoff: %s" % ([str(v) for v in self]) -def as_bool(val): - """Converts an arbitrary value into a boolean.""" - if isinstance(val, bool): - return val - if isinstance(val, six.string_types): - if val.lower() in ('f', 'false', '0', 'n', 'no'): - return False - if val.lower() in ('t', 'true', '1', 'y', 'yes'): - return True - return bool(val) - - def as_int(obj, quiet=False): """Converts an arbitrary value into a integer.""" # Try "2" -> 2