file handler, transaction log, metadata

This commit is contained in:
jbannon 2022-06-30 06:30:20 +00:00
parent 182ec5fc38
commit c8f3a9748f
3 changed files with 69 additions and 14 deletions

View file

@ -17,8 +17,12 @@ def _download_subscriptions_from_yaml_files(config: ConfigFile, args: argparse.N
""" """
Downloads all subscriptions from one or many subscription yaml files. Downloads all subscriptions from one or many subscription yaml files.
:param config: Configuration file Parameters
:param args: Arguments from argparse ----------
config
Configuration file
args
Arguments from argparse
""" """
preset_paths: List[str] = args.subscription_paths preset_paths: List[str] = args.subscription_paths
presets: List[Preset] = [] presets: List[Preset] = []
@ -30,15 +34,23 @@ def _download_subscriptions_from_yaml_files(config: ConfigFile, args: argparse.N
subscription = Subscription.from_preset(preset=preset, config=config) subscription = Subscription.from_preset(preset=preset, config=config)
logger.info("Beginning subscription download for %s", subscription.name) logger.info("Beginning subscription download for %s", subscription.name)
subscription.download() subscription.download(dry_run=args.dry_run)
def _download_subscription_from_cli(config: ConfigFile, extra_args: List[str]) -> None: def _download_subscription_from_cli(
config: ConfigFile, args: argparse.Namespace, extra_args: List[str]
) -> None:
""" """
Downloads a one-off subscription using the CLI Downloads a one-off subscription using the CLI
:param config: Configuration file Parameters
:param extra_args: Extra arguments from argparse that contain dynamic subscription options ----------
config
Configuration file
args
Arguments from argparse
extra_args
Extra arguments from argparse that contain dynamic subscription options
""" """
dl_args_parser = DownloadArgsParser( dl_args_parser = DownloadArgsParser(
extra_arguments=extra_args, config_options=config.config_options extra_arguments=extra_args, config_options=config.config_options
@ -57,7 +69,7 @@ def _download_subscription_from_cli(config: ConfigFile, extra_args: List[str]) -
config=config, config=config,
) )
subscription.download() subscription.download(dry_run=args.dry_run)
def _main(): def _main():
@ -80,7 +92,7 @@ def _main():
# One-off download # One-off download
if args.subparser == "dl": if args.subparser == "dl":
_download_subscription_from_cli(config=config, extra_args=extra_args) _download_subscription_from_cli(config=config, args=args, extra_args=extra_args)
logger.info("Download complete!") logger.info("Download complete!")
# Ran successfully, so we can delete the debug file # Ran successfully, so we can delete the debug file

View file

@ -7,6 +7,7 @@ from typing import final
from ytdl_sub.config.preset_options import Overrides from ytdl_sub.config.preset_options import Overrides
from ytdl_sub.entries.entry import Entry from ytdl_sub.entries.entry import Entry
from ytdl_sub.utils.file_handler import FileMetadata
from ytdl_sub.utils.logger import Logger from ytdl_sub.utils.logger import Logger
from ytdl_sub.validators.strict_dict_validator import StrictDictValidator from ytdl_sub.validators.strict_dict_validator import StrictDictValidator
from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive
@ -60,7 +61,7 @@ class Plugin(Generic[PluginOptionsT], ABC):
file_name=file_name, output_file_name=file_name, entry=entry file_name=file_name, output_file_name=file_name, entry=entry
) )
def post_process_entry(self, entry: Entry): def post_process_entry(self, entry: Entry) -> Optional[FileMetadata]:
""" """
For each file downloaded, apply post processing to it. For each file downloaded, apply post processing to it.
@ -68,6 +69,10 @@ class Plugin(Generic[PluginOptionsT], ABC):
---------- ----------
entry: entry:
Entry to post process Entry to post process
Returns
-------
Optional file metadata for the entry media file.
""" """
def post_process_subscription(self): def post_process_subscription(self):

View file

@ -1,10 +1,50 @@
import os import os
from pathlib import Path from pathlib import Path
from shutil import copyfile from shutil import copyfile
from typing import Dict
from typing import List
from typing import Optional
from typing import Set from typing import Set
from typing import Union from typing import Union
class FileMetadata:
def __init__(self, metadata: Optional[List[str]] = None):
self.metadata: List[str] = metadata if metadata else []
def append(self, other: "FileMetadata") -> "FileMetadata":
self.metadata.extend(other.metadata)
return self
class FileHandlerTransactionLog:
"""
Tracks file 'transactions' performed by a FileHandler
"""
def __init__(self):
self.files_created: Dict[str, FileMetadata] = {}
self.files_removed: Set[str] = set()
def log_created_file(
self, file_name: str, file_metadata: Optional[FileMetadata] = None
) -> "FileHandlerTransactionLog":
if not file_metadata:
file_metadata = FileMetadata()
if file_name in self.files_created:
raise ValueError(
"Adding a file to the file handler transaction log that already exists"
)
self.files_created[file_name] = file_metadata
return self
def log_removed_file(self, file_name: str) -> "FileHandlerTransactionLog":
self.files_removed.add(file_name)
return self
class FileHandler: class FileHandler:
""" """
Performs and tracks all file moving/copying/deleting Performs and tracks all file moving/copying/deleting
@ -14,9 +54,7 @@ class FileHandler:
self.dry_run = dry_run self.dry_run = dry_run
self.working_directory = working_directory self.working_directory = working_directory
self.output_directory = output_directory self.output_directory = output_directory
self._file_handler_transaction_log = FileHandlerTransactionLog()
self.files_created: Set[str] = set()
self.files_deleted: Set[str] = set()
@classmethod @classmethod
def copy(cls, src_file_path: Union[str, Path], dst_file_path: Union[str, Path]): def copy(cls, src_file_path: Union[str, Path], dst_file_path: Union[str, Path]):
@ -28,7 +66,7 @@ class FileHandler:
os.remove(file_path) os.remove(file_path)
def copy_file_to_output_directory(self, file_name: str, output_file_name: str): def copy_file_to_output_directory(self, file_name: str, output_file_name: str):
self.files_created.add(output_file_name) self._file_handler_transaction_log.log_created_file(output_file_name)
if not self.dry_run: if not self.dry_run:
output_file_path = Path(self.output_directory) / output_file_name output_file_path = Path(self.output_directory) / output_file_name
@ -43,6 +81,6 @@ class FileHandler:
exists = os.path.isfile(file_path) exists = os.path.isfile(file_path)
if exists: if exists:
self.files_deleted.add(file_name) self._file_handler_transaction_log.log_removed_file(file_name)
if not self.dry_run: if not self.dry_run:
self.delete(file_path=file_path) self.delete(file_path=file_path)