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 script = entry.script
unresolvable = entry.unresolvable unresolvable = entry.unresolvable
return str( return formatter.post_process(
script.resolve_once( str(
dict({"tmp_var": formatter.format_string}, **(function_overrides or {})), script.resolve_once(
unresolvable=unresolvable, dict({"tmp_var": formatter.format_string}, **(function_overrides or {})),
)["tmp_var"] unresolvable=unresolvable,
)["tmp_var"]
)
) )

View file

@ -1,4 +1,5 @@
import os import os
import posixpath
from yt_dlp.utils import sanitize_filename from yt_dlp.utils import sanitize_filename
@ -28,7 +29,7 @@ class CustomFunctions:
@staticmethod @staticmethod
def to_native_filepath(filepath: String) -> String: 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 @staticmethod
def truncate_filepath_if_too_long(filepath: String) -> String: def truncate_filepath_if_too_long(filepath: String) -> String:

View file

@ -3,6 +3,7 @@ from pathlib import Path
from typing import Any from typing import Any
from ytdl_sub.script.parser import parse 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.script.types.resolvable import String
from ytdl_sub.utils.scriptable import Scriptable from ytdl_sub.utils.scriptable import Scriptable
from ytdl_sub.validators.string_formatter_validators import OverridesStringFormatterValidator from ytdl_sub.validators.string_formatter_validators import OverridesStringFormatterValidator
@ -46,16 +47,30 @@ class StringFormatterFileNameValidator(StringFormatterValidator):
_expected_value_type_name = "filepath" _expected_value_type_name = "filepath"
@property def post_process(self, resolved: str) -> str:
def format_string(self) -> str: return (
return f"{{%to_native_filepath(%truncate_filepath_if_too_long({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")
)
class OverridesStringFormatterFilePathValidator(OverridesStringFormatterValidator): class OverridesStringFormatterFilePathValidator(OverridesStringFormatterValidator):
_expected_value_type_name = "static filepath" _expected_value_type_name = "static filepath"
@property def post_process(self, resolved: str) -> str:
def format_string(self) -> str:
return ( 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 raise self._validation_exception(exc) from exc
@property @property
@final
def format_string(self) -> str: def format_string(self) -> str:
""" """
Returns Returns
@ -96,6 +97,14 @@ class StringFormatterValidator(StringValidator):
""" """
return self._value 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 # pylint: disable=line-too-long
class OverridesStringFormatterValidator(StringFormatterValidator): class OverridesStringFormatterValidator(StringFormatterValidator):

View file

@ -30,7 +30,7 @@ class TestStringFormatterFilePathValidator:
file_path = str(Path(temp_dir) / file_name) file_path = str(Path(temp_dir) / file_name)
formatter = StringFormatterFileNameValidator(name="test", value=str(file_path)) 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") 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}") file_path = str(Path(temp_dir) / f"{base_file_name}{ext}")
formatter = StringFormatterFileNameValidator(name="test", value=str(file_path)) 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") Script({"file_name": formatter.format_string}).resolve().get_str("file_name")
) )