B2BMGMT_roop / roop /template_parser.py
shiveshnavin's picture
init
9d2fc55
raw
history blame contribute delete
658 Bytes
import re
from datetime import datetime
template_functions = {
"timestamp": lambda data: str(int(datetime.now().timestamp())),
"i": lambda data: data.get("index", False),
"file": lambda data: data.get("file", False),
"date": lambda data: datetime.now().strftime("%Y-%m-%d"),
"time": lambda data: datetime.now().strftime("%H-%M-%S"),
}
def parse(text: str, data: dict):
pattern = r"\{([^}]+)\}"
matches = re.findall(pattern, text)
for match in matches:
replacement = template_functions[match](data)
if replacement is not False:
text = text.replace(f"{{{match}}}", replacement)
return text