fix paths

This commit is contained in:
Jesse Bannon 2023-12-08 15:39:03 -08:00
parent 6d681611c4
commit f723ebca2c
5 changed files with 41 additions and 14 deletions

View file

@ -124,9 +124,11 @@ class Overrides(DictFormatterValidator, Scriptable):
script = entry.script
unresolvable = entry.unresolvable
return str(
script.resolve_once(
dict({"tmp_var": formatter.format_string}, **(function_overrides or {})),
unresolvable=unresolvable,
)["tmp_var"]
return formatter.post_process(
str(
script.resolve_once(
dict({"tmp_var": formatter.format_string}, **(function_overrides or {})),
unresolvable=unresolvable,
)["tmp_var"]
)
)

View file

@ -1,4 +1,5 @@
import os
import posixpath
from yt_dlp.utils import sanitize_filename
@ -28,7 +29,7 @@ class CustomFunctions:
@staticmethod
def to_native_filepath(filepath: String) -> String:
return String(str(os.path.realpath(filepath.value)))
return String(filepath.value.replace(posixpath.sep, os.sep))
@staticmethod
def truncate_filepath_if_too_long(filepath: String) -> String:

View file

@ -3,6 +3,7 @@ from pathlib import Path
from typing import Any
from ytdl_sub.script.parser import parse
from ytdl_sub.script.script import Script
from ytdl_sub.script.types.resolvable import String
from ytdl_sub.utils.scriptable import Scriptable
from ytdl_sub.validators.string_formatter_validators import OverridesStringFormatterValidator
@ -46,16 +47,30 @@ class StringFormatterFileNameValidator(StringFormatterValidator):
_expected_value_type_name = "filepath"
@property
def format_string(self) -> str:
return f"{{%to_native_filepath(%truncate_filepath_if_too_long({Scriptable.wrappable_format_string(super().format_string)}))}}"
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")
)
class OverridesStringFormatterFilePathValidator(OverridesStringFormatterValidator):
_expected_value_type_name = "static filepath"
@property
def format_string(self) -> str:
def post_process(self, resolved: str) -> str:
return (
f"{{%to_native_filepath({Scriptable.wrappable_format_string(super().format_string)})}}"
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")
)

View file

@ -88,6 +88,7 @@ class StringFormatterValidator(StringValidator):
raise self._validation_exception(exc) from exc
@property
@final
def format_string(self) -> str:
"""
Returns
@ -96,6 +97,14 @@ class StringFormatterValidator(StringValidator):
"""
return self._value
def post_process(self, resolved: str) -> str:
"""
Returns
-------
Apply any post processing to the resolved value
"""
return resolved
# pylint: disable=line-too-long
class OverridesStringFormatterValidator(StringFormatterValidator):

View file

@ -30,7 +30,7 @@ class TestStringFormatterFilePathValidator:
file_path = str(Path(temp_dir) / file_name)
formatter = StringFormatterFileNameValidator(name="test", value=str(file_path))
truncated_file_path = (
truncated_file_path = formatter.post_process(
Script({"file_name": formatter.format_string}).resolve().get_str("file_name")
)
@ -60,7 +60,7 @@ class TestStringFormatterFilePathValidator:
file_path = str(Path(temp_dir) / f"{base_file_name}{ext}")
formatter = StringFormatterFileNameValidator(name="test", value=str(file_path))
truncated_file_path = (
truncated_file_path = formatter.post_process(
Script({"file_name": formatter.format_string}).resolve().get_str("file_name")
)