Skip to content

Commit

Permalink
Merge pull request #846 from ynput/bugfix/fix_anatomy_format
Browse files Browse the repository at this point in the history
Validate opening and closing brackets on Anatomy keys.
  • Loading branch information
iLLiCiTiT authored Aug 19, 2024
2 parents 49b1d0d + 5442cac commit 874a0f1
Showing 1 changed file with 34 additions and 0 deletions.
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):
"""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

0 comments on commit 874a0f1

Please sign in to comment.