Exemplo n.º 1
0
def replace_all_placeholders(original_input: str, placeholders: list,
                             final_values: dict, path: str):
    for placeholder in placeholders:
        if placeholder[:4] == "env(":
            env_variable_name = placeholder[4:-1]

            if env_variable_name not in os.environ:
                raise Exception(
                    f'Undefined environment variable "{env_variable_name}" used in {path}'
                )

            original_input = replace_placeholder(original_input, placeholder,
                                                 os.environ[env_variable_name],
                                                 path)
        else:
            try:
                final_value = reduce(operator.getitem,
                                     placeholder_splitter.split(placeholder),
                                     final_values)
            except KeyError:
                raise Exception(
                    f'Parameter "{placeholder}" used in {path} not found')

            final_value_resolved = final_value() if callable(
                final_value) else final_value
            original_input = replace_placeholder(original_input, placeholder,
                                                 final_value_resolved, path)

    return original_input
Exemplo n.º 2
0
    def test_none_concat(self):
        with self.assertRaises(Exception) as error:
            replace_placeholder("Instance type: %instance_type%",
                                "instance_type", None, "job.cluster_instance")

        self.assertEqual(
            "Merging None value with other variable types is not allowed in job.cluster_instance",
            str(error.exception))
Exemplo n.º 3
0
    def test_list_concat(self):
        with self.assertRaises(Exception) as error:
            replace_placeholder("Emails: %emails%", "emails",
                                ["*****@*****.**", "*****@*****.**"],
                                "job.on_error_notifications")

        self.assertEqual(
            "Merging list parameters with other variable types is not allowed in job.on_error_notifications",
            str(error.exception))
Exemplo n.º 4
0
    def test_string(self):
        result = replace_placeholder("hello %name%", "name", "Jiri",
                                     "company.owner")

        self.assertEqual("hello Jiri", result)
Exemplo n.º 5
0
    def test_none_solo(self):
        result = replace_placeholder("%instance_type%", "instance_type", None,
                                     "job.cluster_instance")

        self.assertEqual(None, result)
Exemplo n.º 6
0
    def test_list_solo(self):
        result = replace_placeholder("%emails%", "emails",
                                     ["*****@*****.**", "*****@*****.**"],
                                     "job.on_error_notifications")

        self.assertEqual(["*****@*****.**", "*****@*****.**"], result)
Exemplo n.º 7
0
    def test_int_concat(self):
        result = replace_placeholder("House number: %house_number%",
                                     "house_number", 3901, "company.address")

        self.assertEqual("House number: 3901", result)
Exemplo n.º 8
0
    def test_int_solo(self):
        result = replace_placeholder("%house_number%", "house_number", 3901,
                                     "company.address")

        self.assertEqual(3901, result)