OP-4470 - better handle missing keys

Message might contain {placeholder} which are not collected. Previously it would fail without sending message. Now missing keys are double escaped {{}}.
This commit is contained in:
Petr Kalis 2022-12-23 14:09:03 +01:00
parent e377b03e78
commit c16a5289e4

View file

@ -1,4 +1,5 @@
import os
import re
import six
import pyblish.api
import copy
@ -132,14 +133,14 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin):
fill_key = "task[{}]".format(key)
fill_pairs.append((fill_key, value))
self.log.debug("fill_pairs ::{}".format(fill_pairs))
multiple_case_variants = prepare_template_data(fill_pairs)
fill_data.update(multiple_case_variants)
message = None
message = ''
try:
message = message_templ.format(**fill_data)
message = self._escape_missing_keys(message_templ, fill_data).\
format(**fill_data)
except Exception:
# shouldn't happen
self.log.warning(
"Some keys are missing in {}".format(message_templ),
exc_info=True)
@ -263,3 +264,22 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin):
msg = " - application must added to channel '{}'.".format(channel)
error_str += msg + " Ask Slack admin."
return error_str
def _escape_missing_keys(self, message, fill_data):
"""Double escapes placeholder which are missing in 'fill_data'"""
placeholder_keys = re.findall("\{([^}]+)\}", message)
fill_keys = []
for key, value in fill_data.items():
fill_keys.append(key)
if isinstance(value, dict):
for child_key in value.keys():
fill_keys.append("{}[{}]".format(key, child_key))
not_matched = set(placeholder_keys) - set(fill_keys)
for not_matched_item in not_matched:
message = message.replace("{}".format(not_matched_item),
"{{{}}}".format(not_matched_item))
return message