Skip to content

Commit

Permalink
[models] list[str] template
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Jan 30, 2024
1 parent a0abab9 commit bfc946c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions sachi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class GeneralConfig(BaseModel):


class SeriesConfig(BaseModel):
template: str
template: list[str]


class MovieConfig(BaseModel):
template: str
template: list[str]
22 changes: 13 additions & 9 deletions sachi/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,7 +16,8 @@
SachiEpisodeModel,
SachiParentModel,
)
from sachi.utils import FAKE_SLASH, FS_SPECIAL_CHARS

FS_SPECIAL_CHARS = re.compile(r"[/\\:*\"?<>|]")


@dataclass
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 7 additions & 2 deletions sachi/resources/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
5 changes: 0 additions & 5 deletions sachi/utils.py

This file was deleted.

0 comments on commit bfc946c

Please sign in to comment.