Skip to content

Commit

Permalink
Validate opening and closing brackets on Anatomy keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-ynput committed Aug 14, 2024
1 parent 9f2d454 commit eaa1b50
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 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,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.
Expand All @@ -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))
Expand Down

0 comments on commit eaa1b50

Please sign in to comment.