sharing/bday/template: improve placeholder replacement
This commit is contained in:
@@ -382,27 +382,37 @@ def verify(file: str, encoding: str):
|
|||||||
|
|
||||||
|
|
||||||
def replace_placeholders(text: str, placeholder_mapping: dict) -> str:
|
def replace_placeholders(text: str, placeholder_mapping: dict) -> str:
|
||||||
|
logger.trace("item/convert_vcf_to_ics: resolve placeholders: %r", text)
|
||||||
|
|
||||||
for placeholder in placeholder_mapping:
|
for placeholder in placeholder_mapping:
|
||||||
text = text.replace(placeholder, placeholder_mapping[placeholder])
|
text = text.replace(placeholder, placeholder_mapping[placeholder])
|
||||||
|
|
||||||
# resolve {..|..} recursive
|
logger.trace("item/convert_vcf_to_ics: resolve [..|..] in : %r", text)
|
||||||
pattern = re.compile('(.*)(\\[)([^|]+)\\|(.+)(\\])(.*)')
|
|
||||||
logger.trace("item/convert_vcf_to_ics: resolve [..|..] starting with: %r", text)
|
# resolve [..|..] recursive
|
||||||
|
pattern = re.compile('(.*)(\\[)([^|]+)\\|([^\\]]*)(\\])(.*)')
|
||||||
while True:
|
while True:
|
||||||
match = pattern.match(text)
|
match = pattern.match(text)
|
||||||
if not match:
|
if not match:
|
||||||
# nothing more todo
|
# nothing more todo
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
if match[3].startswith('!') and match[3].endswith('!'):
|
logger.trace("item/convert_vcf_to_ics: resolve match : %r", match[0])
|
||||||
|
# check for still unresolved placeholders
|
||||||
|
unresolved = False
|
||||||
|
for placeholder in VCF_TO_ICS_SUPPORTED_PLACEHOLDERS:
|
||||||
|
if "!" + placeholder + "!" in match[3]:
|
||||||
|
unresolved = True
|
||||||
|
break
|
||||||
|
if unresolved:
|
||||||
# not resolved variable
|
# not resolved variable
|
||||||
if '|' in match[4]:
|
if '|' in match[4]:
|
||||||
# further recursion required
|
# further recursion required
|
||||||
text = match[1] + match[2] + match[4] + match[5] + match[6]
|
text = match[1] + match[2] + match[4] + match[5] + match[6]
|
||||||
logger.trace("item/convert_vcf_to_ics: resolve [..|..] match/replace/continue result: %r", text)
|
logger.trace("item/convert_vcf_to_ics: resolve continue : %r", text)
|
||||||
else:
|
else:
|
||||||
text = match[1] + match[4] + match[6]
|
text = match[1] + match[4] + match[6]
|
||||||
logger.trace("item/convert_vcf_to_ics: resolve [..|..] match/replace/final result: %r", text)
|
logger.trace("item/convert_vcf_to_ics: resolve final result: %r", text)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# resolved variable
|
# resolved variable
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ def check_template(data: Any) -> str:
|
|||||||
placeholder_mapping["{" + placeholder + "}"] = '!' + placeholder + '!'
|
placeholder_mapping["{" + placeholder + "}"] = '!' + placeholder + '!'
|
||||||
|
|
||||||
result = item.replace_placeholders(data, placeholder_mapping)
|
result = item.replace_placeholders(data, placeholder_mapping)
|
||||||
logger.trace("replace placeholders: %r -> %r", data, result)
|
logger.trace("replace placeholders result: %r -> %r", data, result)
|
||||||
pattern = re.compile('.*{.*}.*')
|
pattern = re.compile('.*{.*}.*')
|
||||||
if pattern.search(result):
|
if pattern.search(result):
|
||||||
raise ValueError("template contains unsupported placeholder {..}: %r" % result)
|
raise ValueError("template contains unsupported placeholder {..}: %r" % result)
|
||||||
|
|||||||
Reference in New Issue
Block a user