in gcpdiag/runbook/__init__.py [0:0]
def parse_parameters(self, parameter_def: Dict,
caller_args: models.Parameter):
"""Set to defaults parameters and convert datatypes"""
def is_builtin_type(target_type):
"""Check if the object's type is a built-in type."""
return target_type in vars(builtins).values()
def cast_to_type(param_val, target_type):
"""Attempt to cast the object to the target built-in type if possible.
Args:
obj: The object to cast.
target_type: The target built-in type to cast the object to.
Returns:
The object cast to the target type if the original object's type and the target type are
built-in types and the cast is possible. Otherwise, returns the original object.
"""
if is_builtin_type(target_type) and target_type != bool:
try:
return target_type(param_val)
except ValueError:
print(f'Cannot cast {param_val} to type {target_type}.')
elif target_type == bool:
try:
return constants.BOOL_VALUES[str(param_val).lower()]
except KeyError:
print(f'Cannot cast {param_val} to type {target_type}.')
else:
if target_type == datetime:
if isinstance(param_val, target_type):
return param_val
parsed_time = util.parse_time_input(param_val.upper())
if parsed_time.tzinfo is None:
# Define the timezone (for example, UTC) if not present
tz = timezone.utc
# Attach the timezone information
parsed_time = parsed_time.replace(tzinfo=tz)
return parsed_time
try:
return target_type(param_val)
except ValueError:
print(f'Cannot cast {param_val} to type {target_type}.')
self._set_default_parameters(parameter_def)
# convert data types and set defaults for non exist parameters
for k, _ in parameter_def.items():
# Set default if not provided by user
dt_param = parameter_def.get(k)
user_provided_param = caller_args.get(k)
if k not in caller_args and dt_param and dt_param.get('default'):
caller_args[k] = dt_param['default']
continue
if isinstance(user_provided_param, str):
if dt_param and dt_param.get('ignorecase') is True:
caller_args[k] = user_provided_param
else:
caller_args[k] = user_provided_param.lower()
if dt_param and dt_param.get('type') == datetime:
if k == flags.END_TIME:
end_time = caller_args.get(flags.END_TIME, datetime.now(timezone.utc))
caller_args[flags.END_TIME] = cast_to_type(end_time, dt_param['type'])
if k == flags.START_TIME:
end_time = caller_args.get(flags.END_TIME, datetime.now(timezone.utc))
caller_args[flags.END_TIME] = cast_to_type(end_time, dt_param['type'])
parsed_end_time = caller_args[flags.END_TIME]
start_time = caller_args.get(flags.START_TIME,
parsed_end_time - timedelta(hours=8))
caller_args[flags.START_TIME] = cast_to_type(start_time,
dt_param['type'])
if k != flags.START_TIME or k == flags.END_TIME:
date_string = caller_args.get(k)
if date_string:
caller_args[k] = cast_to_type(date_string, dt_param['type'])
# DT specified a type for the param and it's not a string.
# cast the parameter to the type specified by the runbook.
if (dt_param and dt_param.get('type') and dt_param['type'] != str and
user_provided_param):
caller_args[k] = cast_to_type(user_provided_param, dt_param['type'])