Revert "move convert_value to util lib"

This reverts commit 61f5d2bd46623a9fd9c74ae67f81a7d43da7821f.
This commit is contained in:
Mike McLean 2023-04-20 18:20:33 -04:00 committed by Tomas Kopecek
parent 1f54dbdf8f
commit 4b5596b9bd
3 changed files with 33 additions and 35 deletions

View file

@ -93,7 +93,6 @@ from .db import ( # noqa: F401
nextval,
currval,
)
from .util import convert_value
logger = logging.getLogger('koji.hub')
@ -111,6 +110,36 @@ def xform_user_krb(entry):
return entry
def convert_value(value, cast=None, message=None,
exc_type=koji.ParameterError, none_allowed=False, check_only=False):
"""Cast to another type with tailored exception
:param any value: tested object
:param type cast: To which type value should be cast
:param type exc_type: Raise this exception
:param bool none_allowed: Is None valid value?
:param check_only: Don't convert but raise an exception if type(value) != cast
:returns any value: returns converted value
"""
if value is None:
if not none_allowed:
raise exc_type(message or f"Invalid type, expected type {cast}")
else:
return value
if check_only:
if not isinstance(value, cast):
raise exc_type(message or f"Invalid type for value '{value}': {type(value)}, "
f"expected type {cast}")
else:
try:
value = cast(value)
except (ValueError, TypeError):
raise exc_type(message or f"Invalid type for value '{value}': {type(value)}, "
f"expected type {cast}")
return value
class Task(object):
"""A task for the build hosts"""

View file

@ -5,10 +5,12 @@ import time
import koji
from . import kojihub
from .db import QueryProcessor, InsertProcessor, UpdateProcessor, db_lock
from .util import convert_value
from koji.context import context
convert_value = kojihub.convert_value
logger = logging.getLogger('koji.scheduler')

View file

@ -1,33 +0,0 @@
import koji
def convert_value(value, cast=None, message=None,
exc_type=koji.ParameterError, none_allowed=False, check_only=False):
"""Cast to another type with tailored exception
:param any value: tested object
:param type cast: To which type value should be cast
:param type exc_type: Raise this exception
:param bool none_allowed: Is None valid value?
:param check_only: Don't convert but raise an exception if type(value) != cast
:returns any value: returns converted value
"""
if value is None:
if not none_allowed:
raise exc_type(message or f"Invalid type, expected type {cast}")
else:
return value
if check_only:
if not isinstance(value, cast):
raise exc_type(message or f"Invalid type for value '{value}': {type(value)}, "
f"expected type {cast}")
else:
try:
value = cast(value)
except (ValueError, TypeError):
raise exc_type(message or f"Invalid type for value '{value}': {type(value)}, "
f"expected type {cast}")
return value