add generic templates

This commit is contained in:
ChristopheSeux
2024-04-16 14:28:02 +02:00
parent 2960bab63d
commit 0055e0f39e
11 changed files with 343 additions and 116 deletions
+13 -8
View File
@@ -164,19 +164,24 @@ def parse(template, string):
if result := re.match(reg, string):
return result.groupdict()
def find_last(template, **kargs):
#print(find_last, template, kargs)
pattern = Path(template).as_posix()
pattern = expandvars(pattern)
def expand(template, **kargs):
template = Path(template).as_posix()
template = expandvars(template)
for key, value in kargs.items():
pattern = re.sub(r'\{%s\}'%key, value, pattern)
template = re.sub(r'\{%s\}'%key, value, template)
return template
def find_last(template, **kargs):
pattern = expand(template, **kargs)
pattern = re.sub(r'{\w+:(\d{2})d}', lambda x : '?' * int(x.groups()[0]), pattern)
pattern = re.sub(r'{.*?}', '*', pattern)
print(pattern)
filepaths = glob.glob(pattern)
if filepaths:
return Path(max(filepaths))
return Path(max(filepaths))
else:
print(f'No preview found for template {pattern}')