roop-unleashed / roop /template_parser.py
basemnassar07's picture
Upload folder using huggingface_hub
9efad20 verified
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