Beispiel #1
0
def replaceAllPlaceholders(originalInput: str, placeholders: list,
                           finalValues: dict, path: str):
    for placeholder in placeholders:
        if placeholder[:4] == 'env(':
            envVariableName = placeholder[4:-1]

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

            originalInput = replacePlaceholder(originalInput, placeholder,
                                               os.environ[envVariableName],
                                               path)
        else:
            try:
                finalValue = reduce(operator.getitem,
                                    placeholderSplitter.split(placeholder),
                                    finalValues)
            except KeyError:
                raise Exception(
                    f'Parameter "{placeholder}" used in {path} not found')

            finalValueResolved = finalValue() if callable(
                finalValue) else finalValue
            originalInput = replacePlaceholder(originalInput, placeholder,
                                               finalValueResolved, path)

    return originalInput
    def test_noneConcat(self):
        with self.assertRaises(Exception) as error:
            replacePlaceholder('Instance type: %instanceType%', 'instanceType',
                               None, 'job.clusterInstance')

        self.assertEqual(
            'Merging None value with other variable types is not allowed in job.clusterInstance',
            str(error.exception))
    def test_listConcat(self):
        with self.assertRaises(Exception) as error:
            replacePlaceholder('Emails: %emails%', 'emails',
                               ['*****@*****.**', '*****@*****.**'],
                               'job.onErrorNotifications')

        self.assertEqual(
            'Merging list parameters with other variable types is not allowed in job.onErrorNotifications',
            str(error.exception))
    def test_string(self):
        result = replacePlaceholder('hello %name%', 'name', 'Jiri',
                                    'company.owner')

        self.assertEqual('hello Jiri', result)
    def test_noneSolo(self):
        result = replacePlaceholder('%instanceType%', 'instanceType', None,
                                    'job.clusterInstance')

        self.assertEqual(None, result)
    def test_listSolo(self):
        result = replacePlaceholder('%emails%', 'emails',
                                    ['*****@*****.**', '*****@*****.**'],
                                    'job.onErrorNotifications')

        self.assertEqual(['*****@*****.**', '*****@*****.**'], result)
    def test_intConcat(self):
        result = replacePlaceholder('House number: %houseNumber%',
                                    'houseNumber', 3901, 'company.address')

        self.assertEqual('House number: 3901', result)
    def test_intSolo(self):
        result = replacePlaceholder('%houseNumber%', 'houseNumber', 3901,
                                    'company.address')

        self.assertEqual(3901, result)