Skip to content

Commit

Permalink
Factored typecasting out (#5925)
Browse files Browse the repository at this point in the history
* factored typecasting out

* fixed nameing conflict
  • Loading branch information
matmair authored Nov 15, 2023
1 parent df0da18 commit acb3192
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions InvenTree/InvenTree/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,36 @@ def load_config_data(set_cache: bool = False) -> map:
return data


def do_typecast(value, type, var_name=None):
"""Attempt to typecast a value.
Arguments:
value: Value to typecast
type: Function to use for typecasting the value e.g. int, float, str, list, dict
var_name: Name that should be logged e.g. 'INVENTREE_STATIC_ROOT'. Set if logging is required.
Returns:
Typecasted value or original value if typecasting failed.
"""
# Force 'list' of strings
if type is list:
value = to_list(value)

# Valid JSON string is required
elif type is dict:
value = to_dict(value)

elif type is not None:
# Try to typecast the value
try:
val = type(value)
return val
except Exception as error:
if var_name:
logger.exception("Failed to typecast '%s' with value '%s' to type '%s' with error %s", var_name, value, type, error)
return value


def get_setting(env_var=None, config_key=None, default_value=None, typecast=None):
"""Helper function for retrieving a configuration setting value.
Expand All @@ -138,29 +168,8 @@ def get_setting(env_var=None, config_key=None, default_value=None, typecast=None
env_var: Name of the environment variable e.g. 'INVENTREE_STATIC_ROOT'
config_key: Key to lookup in the configuration file
default_value: Value to return if first two options are not provided
typecast: Function to use for typecasting the value
typecast: Function to use for typecasting the value e.g. int, float, str, list, dict
"""
def try_typecasting(value, source: str):
"""Attempt to typecast the value"""
# Force 'list' of strings
if typecast is list:
value = to_list(value)

# Valid JSON string is required
elif typecast is dict:
value = to_dict(value)

elif typecast is not None:
# Try to typecast the value
try:
val = typecast(value)
set_metadata(source)
return val
except Exception as error:
logger.exception("Failed to typecast '%s' with value '%s' to type '%s' with error %s", env_var, value, typecast, error)

set_metadata(source)
return value

def set_metadata(source: str):
"""Set lookup metadata for the setting."""
Expand All @@ -172,7 +181,8 @@ def set_metadata(source: str):
val = os.getenv(env_var, None)

if val is not None:
return try_typecasting(val, 'env')
set_metadata('env')
return do_typecast(val, typecast, var_name=env_var)

# Next, try to load from configuration file
if config_key is not None:
Expand All @@ -191,10 +201,12 @@ def set_metadata(source: str):
cfg_data = cfg_data[key]

if result is not None:
return try_typecasting(result, 'yaml')
set_metadata('yaml')
return do_typecast(result, typecast, var_name=env_var)

# Finally, return the default value
return try_typecasting(default_value, 'default')
set_metadata('default')
return do_typecast(default_value, typecast, var_name=env_var)


def get_boolean_setting(env_var=None, config_key=None, default_value=False):
Expand Down

0 comments on commit acb3192

Please sign in to comment.