This commit is contained in:
Jesse Bannon 2023-02-28 13:57:54 -08:00
parent ad0b56002c
commit c2fab59b3e
6 changed files with 55 additions and 38 deletions

View file

@ -439,7 +439,7 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT], ABC):
if path.endswith(".info.json") if path.endswith(".info.json")
] ]
for info_json_file in info_json_files: for info_json_file in info_json_files:
os.remove(info_json_file) FileHandler.delete(info_json_file)
def _extract_entry_info_with_retry(self, entry: Entry) -> Entry: def _extract_entry_info_with_retry(self, entry: Entry) -> Entry:
download_entry_dict = self.extract_info_with_retry( download_entry_dict = self.extract_info_with_retry(

View file

@ -140,6 +140,7 @@ def set_ffmpeg_metadata_chapters(
metadata_file.write("\n".join(lines)) metadata_file.write("\n".join(lines))
metadata_file.flush() metadata_file.flush()
try:
FFMPEG.run( FFMPEG.run(
[ [
"-i", "-i",
@ -157,7 +158,8 @@ def set_ffmpeg_metadata_chapters(
] ]
) )
FileHandler.move(tmp_file_path, file_path) FileHandler.move(tmp_file_path, file_path)
os.remove(metadata_file.name) finally:
FileHandler.delete(metadata_file.name)
def add_ffmpeg_metadata_key_values(file_path: str, key_values: Dict[str, str]) -> None: def add_ffmpeg_metadata_key_values(file_path: str, key_values: Dict[str, str]) -> None:

View file

@ -67,6 +67,7 @@ def convert_url_thumbnail(thumbnail_url: str, output_thumbnail_path: str) -> Opt
with tempfile.NamedTemporaryFile(delete=False) as thumbnail: with tempfile.NamedTemporaryFile(delete=False) as thumbnail:
thumbnail.write(file.read()) thumbnail.write(file.read())
try:
os.makedirs(os.path.dirname(output_thumbnail_path), exist_ok=True) os.makedirs(os.path.dirname(output_thumbnail_path), exist_ok=True)
tmp_output_path = FFMPEG.tmp_file_path( tmp_output_path = FFMPEG.tmp_file_path(
@ -76,8 +77,8 @@ def convert_url_thumbnail(thumbnail_url: str, output_thumbnail_path: str) -> Opt
# Have FileHandler handle the move to a potential cross-device # Have FileHandler handle the move to a potential cross-device
FileHandler.move(tmp_output_path, output_thumbnail_path) FileHandler.move(tmp_output_path, output_thumbnail_path)
finally:
FileHandler.delete(tmp_output_path) FileHandler.delete(tmp_output_path)
FileHandler.delete(thumbnail.name)
os.remove(thumbnail.name)
return True return True

View file

@ -1,6 +1,7 @@
import contextlib import contextlib
import json import json
import logging import logging
import os
import tempfile import tempfile
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
@ -11,6 +12,7 @@ from unittest.mock import patch
import pytest import pytest
from ytdl_sub.utils.file_handler import FileHandler
from ytdl_sub.utils.logger import Logger from ytdl_sub.utils.logger import Logger
@ -84,9 +86,12 @@ def preset_dict_to_subscription_yaml_generator() -> Callable:
@contextlib.contextmanager @contextlib.contextmanager
def _preset_dict_to_subscription_yaml_generator(subscription_name: str, preset_dict: Dict): def _preset_dict_to_subscription_yaml_generator(subscription_name: str, preset_dict: Dict):
subscription_dict = {subscription_name: preset_dict} subscription_dict = {subscription_name: preset_dict}
with tempfile.NamedTemporaryFile(suffix=".yaml") as tmp_file: with tempfile.NamedTemporaryFile(suffix=".yaml", delete=False) as tmp_file:
tmp_file.write(json.dumps(subscription_dict).encode("utf-8")) tmp_file.write(json.dumps(subscription_dict).encode("utf-8"))
tmp_file.flush()
try:
yield tmp_file.name yield tmp_file.name
finally:
FileHandler.delete(tmp_file.name)
return _preset_dict_to_subscription_yaml_generator return _preset_dict_to_subscription_yaml_generator

View file

@ -15,7 +15,7 @@ from ytdl_sub.cli.main import main
from ytdl_sub.config.config_file import ConfigFile from ytdl_sub.config.config_file import ConfigFile
from ytdl_sub.subscriptions.subscription import Subscription from ytdl_sub.subscriptions.subscription import Subscription
from ytdl_sub.subscriptions.subscription_download import SubscriptionDownload from ytdl_sub.subscriptions.subscription_download import SubscriptionDownload
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog from ytdl_sub.utils.file_handler import FileHandlerTransactionLog, FileHandler
from ytdl_sub.utils.logger import Logger from ytdl_sub.utils.logger import Logger
from ytdl_sub.utils.yaml import load_yaml from ytdl_sub.utils.yaml import load_yaml
@ -67,10 +67,13 @@ def music_video_config(music_video_config_path, working_directory) -> ConfigFile
@pytest.fixture() @pytest.fixture()
def music_video_config_for_cli(music_video_config) -> str: def music_video_config_for_cli(music_video_config) -> str:
with tempfile.NamedTemporaryFile(suffix=".yaml") as tmp_file: with tempfile.NamedTemporaryFile(suffix=".yaml", delete=False) as tmp_file:
tmp_file.write(json.dumps(music_video_config._value).encode("utf-8")) tmp_file.write(json.dumps(music_video_config._value).encode("utf-8"))
tmp_file.flush()
try:
yield tmp_file.name yield tmp_file.name
finally:
FileHandler.delete(tmp_file.name)
@pytest.fixture() @pytest.fixture()
@ -98,10 +101,13 @@ def timestamps_file_path():
"00:01:01 Part 5\n", "00:01:01 Part 5\n",
] ]
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", suffix=".txt") as tmp: with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", suffix=".txt", delete=False) as tmp:
tmp.writelines(timestamps) tmp.writelines(timestamps)
tmp.seek(0)
try:
yield tmp.name yield tmp.name
finally:
FileHandler.delete(tmp.name)
def mock_run_from_cli(args: str) -> List[Tuple[Subscription, FileHandlerTransactionLog]]: def mock_run_from_cli(args: str) -> List[Tuple[Subscription, FileHandlerTransactionLog]]:

View file

@ -6,6 +6,7 @@ import pytest
from ytdl_sub.utils.exceptions import FileNotFoundException from ytdl_sub.utils.exceptions import FileNotFoundException
from ytdl_sub.utils.exceptions import InvalidYamlException from ytdl_sub.utils.exceptions import InvalidYamlException
from ytdl_sub.utils.file_handler import FileHandler
from ytdl_sub.utils.yaml import load_yaml from ytdl_sub.utils.yaml import load_yaml
@ -28,8 +29,10 @@ def bad_yaml_file_path(bad_yaml) -> str:
tmp_file.write(bad_yaml.encode("utf-8")) tmp_file.write(bad_yaml.encode("utf-8"))
tmp_file.flush() tmp_file.flush()
try:
yield tmp_file.name yield tmp_file.name
os.remove(tmp_file.name) finally:
FileHandler.delete(tmp_file.name)
def test_load_yaml_file_not_found(): def test_load_yaml_file_not_found():