[BUGFIX] Fix usage of ~ in paths (#981)

Fixes path with tildes in them, i.e. `~/videos/youtube`
This commit is contained in:
Jesse Bannon 2024-05-10 19:57:48 -07:00 committed by GitHub
parent 64d3082a8a
commit 325d229061
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 4 deletions

View file

@ -716,7 +716,7 @@ to_native_filepath
:spec: ``to_native_filepath(filepath: String) -> String``
Convert any unix-based path separators ('/') with the OS's native
separator.
separator. In addition, expand ~ to absolute directories.
truncate_filepath_if_too_long
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -1,3 +1,5 @@
import os
import posixpath
from typing import Any
from typing import Dict
from typing import Optional
@ -147,7 +149,8 @@ class ConfigOptions(StrictDictValidator):
The directory to temporarily store downloaded files before moving them into their final
directory. Defaults to .ytdl-sub-working-directory
"""
return self._working_directory.value
# Expands tildas to actual paths, use native os sep
return os.path.expanduser(self._working_directory.value.replace(posixpath.sep, os.sep))
@property
def umask(self) -> Optional[str]:

View file

@ -36,9 +36,9 @@ class CustomFunctions:
def to_native_filepath(filepath: String) -> String:
"""
Convert any unix-based path separators ('/') with the OS's native
separator.
separator. In addition, expand ~ to absolute directories.
"""
return String(filepath.value.replace(posixpath.sep, os.sep))
return String(os.path.expanduser(filepath.value.replace(posixpath.sep, os.sep)))
@staticmethod
def truncate_filepath_if_too_long(filepath: String) -> String:

View file

@ -1,4 +1,6 @@
import os.path
import re
from pathlib import Path
from typing import Dict
from typing import Optional
@ -94,6 +96,18 @@ class TestConfigFilePartiallyValidatesPresets:
},
)
def test_config_file_working_dir_home_dir(self):
out = ConfigFile(
name="test_tilda",
value={
"configuration": {"working_directory": "~/working/dir"},
},
)
assert out.config_options.working_directory == str(
Path(os.path.expanduser("~")) / "working" / "dir"
)
@pytest.mark.parametrize(
"preset_dict",
[

View file

@ -1,4 +1,6 @@
import os.path
import re
from pathlib import Path
import pytest
@ -37,6 +39,20 @@ class TestPreset:
},
)
def test_preset_with_output_directory_tilda(self, config_file, output_options, youtube_video):
out = Preset(
config=config_file,
name="test",
value={
"download": youtube_video,
"output_options": {"output_directory": "~/output/dir", "file_name": "{dne_var}"},
"overrides": {"dne_var": "not dne"},
},
)
assert out.overrides.apply_formatter(out.output_options.output_directory) == str(
Path(os.path.expanduser("~")) / "output" / "dir"
)
def test_preset_parent(self, config_file, output_options, youtube_video):
preset = Preset(
config=config_file,