diff --git a/src/ytdl_sub/utils/file_path.py b/src/ytdl_sub/utils/file_path.py index af8246f1d..ff50c9916 100644 --- a/src/ytdl_sub/utils/file_path.py +++ b/src/ytdl_sub/utils/file_path.py @@ -1,4 +1,5 @@ import os +import posixpath from pathlib import Path from typing import Tuple @@ -59,3 +60,8 @@ def maybe_truncate_file_path(cls, file_path: str) -> str: return str(Path(file_directory) / cls._truncate_file_name(file_name)) return str(file_path) + + @classmethod + def to_native_filepath(cls, file_path: str) -> str: + """Ensures file paths use the correct separator""" + return os.path.expanduser(file_path.replace(posixpath.sep, os.sep)) diff --git a/src/ytdl_sub/validators/file_path_validators.py b/src/ytdl_sub/validators/file_path_validators.py index c255ed1a0..f5a69f615 100644 --- a/src/ytdl_sub/validators/file_path_validators.py +++ b/src/ytdl_sub/validators/file_path_validators.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import Any -from ytdl_sub.script.script import Script +from ytdl_sub.utils.file_path import FilePathTruncater from ytdl_sub.validators.string_formatter_validators import OverridesStringFormatterValidator from ytdl_sub.validators.string_formatter_validators import StringFormatterValidator from ytdl_sub.validators.validators import StringValidator @@ -45,15 +45,8 @@ class StringFormatterFileNameValidator(StringFormatterValidator): _expected_value_type_name = "filepath" def post_process(self, resolved: str) -> str: - return ( - Script( - { - "tmp_var_1": resolved, - "tmp_var_2": "{%to_native_filepath(%truncate_filepath_if_too_long(tmp_var_1))}", - } - ) - .resolve() - .get_str("tmp_var_2") + return FilePathTruncater.to_native_filepath( + FilePathTruncater.maybe_truncate_file_path(resolved) ) @@ -61,13 +54,6 @@ class OverridesStringFormatterFilePathValidator(OverridesStringFormatterValidato _expected_value_type_name = "static filepath" def post_process(self, resolved: str) -> str: - return ( - Script( - { - "tmp_var_1": resolved, - "tmp_var_2": "{%to_native_filepath(%truncate_filepath_if_too_long(tmp_var_1))}", - } - ) - .resolve() - .get_str("tmp_var_2") + return FilePathTruncater.to_native_filepath( + FilePathTruncater.maybe_truncate_file_path(resolved) ) diff --git a/tests/e2e/test_debug_repro.py b/tests/e2e/test_debug_repro.py index b96f530b2..8587dd5cb 100644 --- a/tests/e2e/test_debug_repro.py +++ b/tests/e2e/test_debug_repro.py @@ -1,4 +1,3 @@ -import json from typing import Dict import pytest @@ -44,13 +43,13 @@ class TestReproduce: def test_debug_log_repro( self, default_config, - repro_preset_dict, + debug_log_rerpo, output_directory, ): sub = Subscription.from_dict( config=default_config, preset_name="repro", - preset_dict=repro_preset_dict, + preset_dict=debug_log_rerpo, ) transaction_log = sub.download(dry_run=False) diff --git a/tests/unit/validators/test_file_path_validators.py b/tests/unit/validators/test_file_path_validators.py index 7ff621431..467f7b802 100644 --- a/tests/unit/validators/test_file_path_validators.py +++ b/tests/unit/validators/test_file_path_validators.py @@ -54,19 +54,17 @@ def test_truncates_file_names_successfully(self, ext: str): if "thumb" not in ext: # do not put . in front of -thumb ext = f".{ext}" # pytest args with . in the beginning act weird - base_file_name = "s2023.e031701 - 𝗪𝗔𝗥𝗡𝗜𝗡𝗚: LG Secretly Overhaul This OLED Feature on C3 & G3… Should You Buy C2 Instead?" + base_file_name = "s2023.e031701 - 𝗪𝗔𝗥𝗡𝗜𝗡𝗚: {LG} Secretly Overhaul This OLED Feature on C3 & G3… Should You Buy C2 Instead?" with tempfile.TemporaryDirectory() as temp_dir: file_path = str(Path(temp_dir) / f"{base_file_name}{ext}") - formatter = StringFormatterFileNameValidator(name="test", value=str(file_path)) - truncated_file_path = formatter.post_process( - Script({"file_name": formatter.format_string}).resolve().get_str("file_name") - ) + formatter = StringFormatterFileNameValidator(name="test", value="") + truncated_file_path = formatter.post_process(file_path) assert truncated_file_path == str( Path(temp_dir) - / f"s2023.e031701 - 𝗪𝗔𝗥𝗡𝗜𝗡𝗚: LG Secretly Overhaul This OLED Feature on C3 & G3… Should You Buy C2 Instead?{ext}" + / f"s2023.e031701 - 𝗪𝗔𝗥𝗡𝗜𝗡𝗚: {{LG}} Secretly Overhaul This OLED Feature on C3 & G3… Should You Buy C2 Instead?{ext}" ) # Ensure it can actually open the file