From bfc946ceb985a0e9c62ffaf2d39c8102168f9596 Mon Sep 17 00:00:00 2001 From: NextFire Date: Tue, 30 Jan 2024 00:07:03 -0500 Subject: [PATCH] [models] list[str] template --- sachi/config.py | 4 ++-- sachi/models.py | 22 +++++++++++++--------- sachi/resources/config.toml | 9 +++++++-- sachi/utils.py | 5 ----- 4 files changed, 22 insertions(+), 18 deletions(-) delete mode 100644 sachi/utils.py diff --git a/sachi/config.py b/sachi/config.py index 3212581..2fa882f 100644 --- a/sachi/config.py +++ b/sachi/config.py @@ -39,8 +39,8 @@ class GeneralConfig(BaseModel): class SeriesConfig(BaseModel): - template: str + template: list[str] class MovieConfig(BaseModel): - template: str + template: list[str] diff --git a/sachi/models.py b/sachi/models.py index 019b697..6ead7fa 100644 --- a/sachi/models.py +++ b/sachi/models.py @@ -1,4 +1,5 @@ import asyncio +import re from dataclasses import asdict, dataclass from pathlib import Path from typing import Any, Callable, Self, assert_never, cast @@ -15,7 +16,8 @@ SachiEpisodeModel, SachiParentModel, ) -from sachi.utils import FAKE_SLASH, FS_SPECIAL_CHARS + +FS_SPECIAL_CHARS = re.compile(r"[/\\:*\"?<>|]") @dataclass @@ -110,18 +112,20 @@ async def template_new_path(self): match self.match.parent.media_type: case MediaType.SERIES: - template_str = config_model.series.template + template_list = config_model.series.template case MediaType.MOVIE: - template_str = config_model.movie.template + template_list = config_model.movie.template case _: assert_never(self.match.parent.media_type) - # FIXME: replace FAKE_SLASH with a better solution - template = jinja2.Template(template_str.replace("/", FAKE_SLASH)) - new_segment = template.render(asdict(self.ctx)) - new_segment = FS_SPECIAL_CHARS.sub("", new_segment) - new_segment = new_segment.replace(FAKE_SLASH, "/") - new_path = (self.base_dir / new_segment).with_suffix(self.path.suffix) + new_path = self.base_dir + ctx_dict = asdict(self.ctx) + for part in template_list: + template = jinja2.Template(part) + segment = template.render(ctx_dict) + segment = FS_SPECIAL_CHARS.sub("", segment) + new_path /= segment + new_path = new_path.with_suffix(self.path.suffix) self.set_rename_cell(str(new_path.relative_to(self.base_dir))) self.new_path.set_result(new_path) diff --git a/sachi/resources/config.toml b/sachi/resources/config.toml index 2bf556b..b95a613 100644 --- a/sachi/resources/config.toml +++ b/sachi/resources/config.toml @@ -2,10 +2,15 @@ dark = true [series] -template = "TV Shows/{{n}}/Season {{'%02d' % s}}/{{n}} - {{s00e00}} - {{t}}" +template = [ + "TV Shows", + "{{n}}", + "Season {{'%02d' % s}}", + "{{n}} - {{s00e00}} - {{t}}", +] [movie] -template = "Movies/{{n}} ({{y}})/{{n}} ({{y}})" +template = ["Movies", "{{n}} ({{y}})", "{{n}} ({{y}})"] [tvdb] apiKey = "" diff --git a/sachi/utils.py b/sachi/utils.py deleted file mode 100644 index 27f20ee..0000000 --- a/sachi/utils.py +++ /dev/null @@ -1,5 +0,0 @@ -import re - -FAKE_SLASH = "/" - -FS_SPECIAL_CHARS = re.compile(r"[/\\:*\"?<>|]")