Merge branch 'develop' into feature/AY-5185_render-product-templates

This commit is contained in:
Ondřej Samohel 2024-08-20 10:33:21 +02:00 committed by GitHub
commit 95af0bd8f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -460,6 +460,34 @@ class FormattingPart:
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.
@ -472,6 +500,12 @@ class FormattingPart:
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))