From eaa1b503b7e3e956ed753d83644dad8023765d24 Mon Sep 17 00:00:00 2001 From: robin Date: Wed, 14 Aug 2024 17:52:29 -0400 Subject: [PATCH 1/5] Validate opening and closing brackets on Anatomy keys. --- client/ayon_core/lib/path_templates.py | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/client/ayon_core/lib/path_templates.py b/client/ayon_core/lib/path_templates.py index 01a6985a25..0a39d05e1d 100644 --- a/client/ayon_core/lib/path_templates.py +++ b/client/ayon_core/lib/path_templates.py @@ -460,6 +460,30 @@ def validate_value_type(value): return True return False + @staticmethod + def validate_key_is_matched(key): + """ + Finds out how balanced an expression is. + With a string containing only brackets. + + >>> is_matched('[]()()(((([])))') + False + >>> is_matched('[](){{{[]}}}') + True + """ + opening = tuple('({[') + closing = tuple(')}]') + mapping = dict(zip(opening, closing)) + queue = [] + + for letter in key: + if letter in opening: + queue.append(mapping[letter]) + elif letter in closing: + if not queue or letter != queue.pop(): + return False + return not queue + def format(self, data, result): """Format the formattings string. @@ -472,6 +496,12 @@ def format(self, data, result): result.add_output(result.realy_used_values[key]) return result + # ensure key is properly formed [({})] properly closed. + if not self.validate_key_is_matched(key): + result.add_missing_key(key) + result.add_output(self.template) + return result + # check if key expects subdictionary keys (e.g. project[name]) existence_check = key key_padding = list(KEY_PADDING_PATTERN.findall(existence_check)) From 44c417fdb0e960c6f8af2bae56309efd70724dc6 Mon Sep 17 00:00:00 2001 From: __robin__ Date: Mon, 19 Aug 2024 08:08:33 -0400 Subject: [PATCH 2/5] Update client/ayon_core/lib/path_templates.py Co-authored-by: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> --- client/ayon_core/lib/path_templates.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/client/ayon_core/lib/path_templates.py b/client/ayon_core/lib/path_templates.py index 0a39d05e1d..46dd997da2 100644 --- a/client/ayon_core/lib/path_templates.py +++ b/client/ayon_core/lib/path_templates.py @@ -462,14 +462,17 @@ def validate_value_type(value): @staticmethod def validate_key_is_matched(key): - """ - Finds out how balanced an expression is. - With a string containing only brackets. + """Validate that opening has closing at correct place. + + Example: + >>> is_matched("[]()()(((([])))") + False + >>> is_matched("[](){{{[]}}}") + True + + Returns: + bool: Openings and closinga are valid. - >>> is_matched('[]()()(((([])))') - False - >>> is_matched('[](){{{[]}}}') - True """ opening = tuple('({[') closing = tuple(')}]') From 4d884db269de1f8de4b5824bfa9fb5a7b728220f Mon Sep 17 00:00:00 2001 From: __robin__ Date: Mon, 19 Aug 2024 08:08:50 -0400 Subject: [PATCH 3/5] Update client/ayon_core/lib/path_templates.py Co-authored-by: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> --- client/ayon_core/lib/path_templates.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/ayon_core/lib/path_templates.py b/client/ayon_core/lib/path_templates.py index 46dd997da2..edc7478cef 100644 --- a/client/ayon_core/lib/path_templates.py +++ b/client/ayon_core/lib/path_templates.py @@ -474,8 +474,8 @@ def validate_key_is_matched(key): bool: Openings and closinga are valid. """ - opening = tuple('({[') - closing = tuple(')}]') + opening = tuple("({[") + closing = tuple(")}]") mapping = dict(zip(opening, closing)) queue = [] From b0490fd15dbd24534c15e1f80ccf116b5ef2003f Mon Sep 17 00:00:00 2001 From: robin Date: Mon, 19 Aug 2024 08:13:55 -0400 Subject: [PATCH 4/5] Adjust docstring from PR feedback. --- client/ayon_core/lib/path_templates.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/ayon_core/lib/path_templates.py b/client/ayon_core/lib/path_templates.py index edc7478cef..3e3bdd8f78 100644 --- a/client/ayon_core/lib/path_templates.py +++ b/client/ayon_core/lib/path_templates.py @@ -463,6 +463,7 @@ def validate_value_type(value): @staticmethod def validate_key_is_matched(key): """Validate that opening has closing at correct place. + Future-proof, only square brackets are currently used in keys. Example: >>> is_matched("[]()()(((([])))") @@ -471,7 +472,7 @@ def validate_key_is_matched(key): True Returns: - bool: Openings and closinga are valid. + bool: Openings and closing are valid. """ opening = tuple("({[") From 5442cacdc346b7ef1c34c5493f116ea82300d531 Mon Sep 17 00:00:00 2001 From: Robin De Lillo Date: Mon, 19 Aug 2024 11:08:44 -0400 Subject: [PATCH 5/5] Update client/ayon_core/lib/path_templates.py Co-authored-by: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> --- client/ayon_core/lib/path_templates.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/ayon_core/lib/path_templates.py b/client/ayon_core/lib/path_templates.py index 3e3bdd8f78..33af503dd5 100644 --- a/client/ayon_core/lib/path_templates.py +++ b/client/ayon_core/lib/path_templates.py @@ -475,9 +475,9 @@ def validate_key_is_matched(key): bool: Openings and closing are valid. """ - opening = tuple("({[") - closing = tuple(")}]") - mapping = dict(zip(opening, closing)) + mapping = dict(zip("({[", ")}]")) + opening = set(mapping.keys()) + closing = set(mapping.values()) queue = [] for letter in key: