diff --git a/cloudslang-runtime/src/main/resources/scripts/cs_extract_number.py b/cloudslang-runtime/src/main/resources/scripts/cs_extract_number.py index 637a36526d..42c1204dce 100644 --- a/cloudslang-runtime/src/main/resources/scripts/cs_extract_number.py +++ b/cloudslang-runtime/src/main/resources/scripts/cs_extract_number.py @@ -1,12 +1,73 @@ import re -def cs_extract_number(string, count = 1): - if type(string).__name__ != 'str' and type(string).__name__ != 'unicode': - raise Exception("Expected a string for parameter 'string', got " + str(string)) - result = None - numbers = re.findall("[0-9]*\.?[0-9]+", string) +def cs_extract_number(string, count=1): + if type(string).__name__ != "str" and type(string).__name__ != "unicode": + raise Exception("Expected a string for parameter 'string', got " + str(string)) - if len(numbers) >= count & count >= 1: - result = numbers[count - 1] + result = None + numbers = re.findall("[0-9]*\.?[0-9]+", string) - return result \ No newline at end of file + count_int_value = validate_input(count, "count", 1) + + if len(numbers) >= count_int_value & count_int_value >= 1: + result = numbers[count_int_value - 1] + + return result + + +def validate_input(input_value, parameter_name, default_value): + if type(input_value).__name__ == "NoneType": + return default_value + elif type(input_value).__name__ == "int": + return input_value + elif type(input_value).__name__ == "float" or type(input_value).__name__ == "bool": + return int(input_value) + elif type(input_value).__name__ == "str": + return convert_string_to_int(input_value, parameter_name, default_value) + elif type(input_value).__name__ == "bytes": + return convert_string_to_int(input_value.decode(), parameter_name, default_value) + else: + raise Exception("Type of parameter '" + parameter_name + "' is not valid, got " + str(input_value) + + " of type " + str(type(input_value).__name__)) + + +def convert_string_to_int(input_value, parameter_name, default_value): + if is_string_of_type_int(input_value): + return int(input_value) + elif is_string_of_type_float(input_value): + return int(float(input_value)) + elif is_string_of_type_none(input_value): + return default_value + else: + return check_bool_in_string(input_value, parameter_name) + + +def is_string_of_type_int(input_value): + try: + int(input_value) + return True + except ValueError: + return False + + +def is_string_of_type_float(input_value): + try: + float(input_value) + return True + except ValueError: + return False + +def is_string_of_type_none(input_value): + if input_value.lower() == 'none': + return True + else: + return False + + +def check_bool_in_string(input_value, parameter_name): + if input_value.lower() == 'true': + return int(True) + elif input_value.lower() == 'false': + return int(False) + else: + raise Exception("Value of the parameter '" + parameter_name + "' is not valid, got " + str(input_value)) \ No newline at end of file diff --git a/cloudslang-runtime/src/main/resources/scripts/cs_replace.py b/cloudslang-runtime/src/main/resources/scripts/cs_replace.py index eba3c69ca6..314d130484 100644 --- a/cloudslang-runtime/src/main/resources/scripts/cs_replace.py +++ b/cloudslang-runtime/src/main/resources/scripts/cs_replace.py @@ -1,12 +1,76 @@ -def cs_replace(string, old_val, new_val, count = -1): +def cs_replace(string, old_val, new_val, count=-1): - if type(string).__name__ != 'str' and type(string).__name__ != 'unicode': - raise Exception("Expected a string for parameter 'string', got " + str(string)) + if type(string).__name__ != "str" and type(string).__name__ != "unicode": + raise Exception("Expected a string for parameter 'string', got " + str(string)) - if type(old_val).__name__ != 'str' and type(old_val).__name__ != 'unicode': - raise Exception("Expected a string for parameter 'old_val', got " + str(old_val)) + if type(old_val).__name__ != "str" and type(old_val).__name__ != "unicode": + raise Exception("Expected a string for parameter 'old_val', got " + str(old_val)) - if type(new_val).__name__ != 'str' and type(new_val).__name__ != 'unicode': - raise Exception("Expected a string for parameter 'new_val', got " + str(new_val)) + if type(new_val).__name__ != "str" and type(new_val).__name__ != "unicode": + raise Exception("Expected a string for parameter 'new_val', got " + str(new_val)) - return string.replace(old_val, new_val) if count < 0 else string.replace(old_val, new_val, count) + count_int_value = validate_input(count, "count", -1) + + return ( + string.replace(old_val, new_val) + if count_int_value < 0 + else string.replace(old_val, new_val, count_int_value) + ) + + +def validate_input(input_value, parameter_name, default_value): + if type(input_value).__name__ == "NoneType": + return default_value + elif type(input_value).__name__ == "int": + return input_value + elif type(input_value).__name__ == "float" or type(input_value).__name__ == "bool": + return int(input_value) + elif type(input_value).__name__ == "str": + return convert_string_to_int(input_value, parameter_name, default_value) + elif type(input_value).__name__ == "bytes": + return convert_string_to_int(input_value.decode(), parameter_name, default_value) + else: + raise Exception("Type of parameter '" + parameter_name + "' is not valid, got " + str(input_value) + + " of type " + str(type(input_value).__name__)) + + +def convert_string_to_int(input_value, parameter_name, default_value): + if is_string_of_type_int(input_value): + return int(input_value) + elif is_string_of_type_float(input_value): + return int(float(input_value)) + elif is_string_of_type_none(input_value): + return default_value + else: + return check_bool_in_string(input_value, parameter_name) + + +def is_string_of_type_int(input_value): + try: + int(input_value) + return True + except ValueError: + return False + + +def is_string_of_type_float(input_value): + try: + float(input_value) + return True + except ValueError: + return False + +def is_string_of_type_none(input_value): + if input_value.lower() == 'none': + return True + else: + return False + + +def check_bool_in_string(input_value, parameter_name): + if input_value.lower() == 'true': + return int(True) + elif input_value.lower() == 'false': + return int(False) + else: + raise Exception("Value of the parameter '" + parameter_name + "' is not valid, got " + str(input_value)) \ No newline at end of file diff --git a/cloudslang-runtime/src/main/resources/scripts/cs_substring.py b/cloudslang-runtime/src/main/resources/scripts/cs_substring.py index 4eb06cc24f..f98730d383 100644 --- a/cloudslang-runtime/src/main/resources/scripts/cs_substring.py +++ b/cloudslang-runtime/src/main/resources/scripts/cs_substring.py @@ -1,5 +1,73 @@ -def cs_substring(string, start, end = None): - if type(string).__name__ != 'str' and type(string).__name__ != 'unicode': - raise Exception("Expected a string for parameter 'string', got " + str(string)) +def cs_substring(string, start, end=None): + if type(string).__name__ != "str" and type(string).__name__ != "unicode": + raise Exception("Expected a string for parameter 'string', got " + str(string)) - return string[start: end] + start_int_value = validate_input(start, "start", None) + end_int_value = validate_input(end, "end", None) + + if (start_int_value is None) and (end_int_value is None): + return string + elif end_int_value is None: + return string[start_int_value:] + elif start_int_value is None: + return string[:end_int_value] + else: + return string[start_int_value:end_int_value] + + +def validate_input(input_value, parameter_name, default_value): + if type(input_value).__name__ == "NoneType": + return default_value + elif type(input_value).__name__ == "int": + return input_value + elif type(input_value).__name__ == "float" or type(input_value).__name__ == "bool": + return int(input_value) + elif type(input_value).__name__ == "str": + return convert_string_to_int(input_value, parameter_name, default_value) + elif type(input_value).__name__ == "bytes": + return convert_string_to_int(input_value.decode(), parameter_name, default_value) + else: + raise Exception("Type of parameter '" + parameter_name + "' is not valid, got " + str(input_value) + + " of type " + str(type(input_value).__name__)) + + +def convert_string_to_int(input_value, parameter_name, default_value): + if is_string_of_type_int(input_value): + return int(input_value) + elif is_string_of_type_float(input_value): + return int(float(input_value)) + elif is_string_of_type_none(input_value): + return default_value + else: + return check_bool_in_string(input_value, parameter_name) + + +def is_string_of_type_int(input_value): + try: + int(input_value) + return True + except ValueError: + return False + + +def is_string_of_type_float(input_value): + try: + float(input_value) + return True + except ValueError: + return False + +def is_string_of_type_none(input_value): + if input_value.lower() == 'none': + return True + else: + return False + + +def check_bool_in_string(input_value, parameter_name): + if input_value.lower() == 'true': + return int(True) + elif input_value.lower() == 'false': + return int(False) + else: + raise Exception("Value of the parameter '" + parameter_name + "' is not valid, got " + str(input_value)) \ No newline at end of file