Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate opening and closing brackets on Anatomy keys. #846

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions client/ayon_core/lib/path_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,34 @@ def validate_value_type(value):
return True
return False

@staticmethod
def validate_key_is_matched(key):
robin-ynput marked this conversation as resolved.
Show resolved Hide resolved
"""Validate that opening has closing at correct place.
Future-proof, only square brackets are currently used in keys.

Example:
>>> is_matched("[]()()(((([])))")
False
>>> is_matched("[](){{{[]}}}")
True

Returns:
bool: Openings and closing are valid.

"""
mapping = dict(zip("({[", ")}]"))
opening = set(mapping.keys())
closing = set(mapping.values())
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.

Expand All @@ -472,6 +500,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))
Expand Down