From a6ee520fe0aaccc2d5fdfaaea2903b97cd7f4298 Mon Sep 17 00:00:00 2001 From: seolmin Date: Tue, 5 Nov 2024 18:18:24 +0900 Subject: [PATCH] feat: modify remove_jinja_braces method in DataTable Manager Signed-off-by: seolmin --- .../manager/data_table_manager/__init__.py | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/spaceone/dashboard/manager/data_table_manager/__init__.py b/src/spaceone/dashboard/manager/data_table_manager/__init__.py index bcd4c00..12d52cf 100644 --- a/src/spaceone/dashboard/manager/data_table_manager/__init__.py +++ b/src/spaceone/dashboard/manager/data_table_manager/__init__.py @@ -361,26 +361,26 @@ def change_global_variables(self, expression: str, vars: dict): @staticmethod def remove_jinja_braces(expression: str) -> Union[str, float, list]: - if re.match(r"{{\s*(\w+)\s*}}", expression): - return re.sub(r"{{\s*(\w+)\s*}}", r"\1", expression) - elif re.match(r"{{\s*(\d+(\.\d+)?)\s*}}", expression): - result = re.sub(r"{{\s*(\d+(\.\d+)?)\s*}}", r"\1", expression) - try: - return float(result) - except ValueError: - return eval(result) - else: - expression = expression.replace("{{", "").replace("}}", "").strip() - - pattern = r"\[\d+\]$" - if re.search(pattern, expression): - dict_expression, _ = re.subn(pattern, "", expression) - index_str = expression.replace(dict_expression, "").strip() - index_str = index_str.replace("[", "").replace("]", "") - index = int(index_str) - return ast.literal_eval(dict_expression)[index] - - return ast.literal_eval(expression) + while "{{" in expression and "}}" in expression: + if re.match(r"{{\s*(\w+)\s*}}", expression): + expression = re.sub(r"{{\s*(\w+)\s*}}", r"\1", expression) + elif re.match(r"{{\s*(\d+(\.\d+)?)\s*}}", expression): + result = re.sub(r"{{\s*(\d+(\.\d+)?)\s*}}", r"\1", expression) + try: + expression = float(result) + except ValueError: + expression = eval(result) + else: + expression = expression.replace("{{", "").replace("}}", "").strip() + + pattern = r"\[\d+\]$" + if re.search(pattern, expression): + dict_expression, _ = re.subn(pattern, "", expression) + index_str = expression.replace(dict_expression, "").strip() + index_str = index_str.replace("[", "").replace("]", "") + index = int(index_str) + expression = ast.literal_eval(dict_expression)[index] + return expression @staticmethod def change_expression_data_type(expression: str, gv_type_map: dict) -> str: