[BUGFIX] Do not perform nested scripting on file path outputs (#1123)

Fixes a bug where curly braces in file-names causes scripting to think its a variable that does not exist
This commit is contained in:
Jesse Bannon 2024-11-18 00:22:20 -08:00 committed by GitHub
parent 0f542ac4e5
commit d3295cef86
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 17 additions and 28 deletions

View file

@ -1,4 +1,5 @@
import os
import posixpath
from pathlib import Path
from typing import Tuple
@ -59,3 +60,8 @@ class FilePathTruncater:
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))

View file

@ -2,7 +2,7 @@ import os
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)
)

View file

@ -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)

View file

@ -54,19 +54,17 @@ class TestStringFormatterFilePathValidator:
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 C & G… Should You Buy C Instead"
base_file_name = "s2023.e031701 - 𝗪𝗔𝗥𝗡𝗜𝗡𝗚 {LG} Secretly Overhaul This OLED Feature on C & G… Should You Buy C 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 C & G… Should You Buy C Instead{ext}"
/ f"s2023.e031701 - 𝗪𝗔𝗥𝗡𝗜𝗡𝗚 {{LG}} Secretly Overhaul This OLED Feature on C & G… Should You Buy C Instead{ext}"
)
# Ensure it can actually open the file