Create logger class, use it in favor of prints (#16)

This commit is contained in:
Jesse Bannon 2022-04-29 22:39:06 -07:00 committed by GitHub
parent fa19e0d0c0
commit fd5c48b897
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 80 additions and 18 deletions

View file

@ -69,6 +69,15 @@ class SubscriptionValidator(StrictDictValidator):
preset_options=self.preset, preset_options=self.preset,
) )
@property
def name(self) -> str:
"""
Returns
-------
Name of the subscription
"""
return self._name
@classmethod @classmethod
def from_dict( def from_dict(
cls, config: ConfigFile, subscription_name: str, subscription_dict: Dict cls, config: ConfigFile, subscription_name: str, subscription_dict: Dict

View file

@ -17,10 +17,13 @@ from ytdl_sub.config.preset_options import Overrides
from ytdl_sub.downloaders.downloader import Downloader from ytdl_sub.downloaders.downloader import Downloader
from ytdl_sub.downloaders.downloader import DownloaderValidator from ytdl_sub.downloaders.downloader import DownloaderValidator
from ytdl_sub.entries.youtube import YoutubeVideo from ytdl_sub.entries.youtube import YoutubeVideo
from ytdl_sub.utils.logger import Logger
from ytdl_sub.validators.date_range_validator import DateRangeValidator from ytdl_sub.validators.date_range_validator import DateRangeValidator
from ytdl_sub.validators.string_formatter_validators import OverridesStringFormatterValidator from ytdl_sub.validators.string_formatter_validators import OverridesStringFormatterValidator
from ytdl_sub.validators.validators import StringValidator from ytdl_sub.validators.validators import StringValidator
logger = Logger.get()
############################################################################### ###############################################################################
# Abstract Youtube downloader + options # Abstract Youtube downloader + options
@ -223,7 +226,7 @@ class YoutubeChannelDownloader(YoutubeDownloader[YoutubeChannelDownloaderOptions
break break
if not thumbnail_url: if not thumbnail_url:
# TODO: add logger with warn here logger.warning("Could not find a thumbnail for %s", entry_dict.get("id"))
return return
with urlopen(thumbnail_url) as file: with urlopen(thumbnail_url) as file:

View file

@ -1,6 +1,5 @@
import argparse import argparse
import sys import sys
import traceback
from typing import List from typing import List
from ytdl_sub.cli.download_args_parser import DownloadArgsParser from ytdl_sub.cli.download_args_parser import DownloadArgsParser
@ -8,8 +7,9 @@ from ytdl_sub.cli.main_args_parser import parser
from ytdl_sub.config.config_file import ConfigFile from ytdl_sub.config.config_file import ConfigFile
from ytdl_sub.config.subscription import SubscriptionValidator from ytdl_sub.config.subscription import SubscriptionValidator
from ytdl_sub.utils.exceptions import ValidationException from ytdl_sub.utils.exceptions import ValidationException
from ytdl_sub.utils.logger import Logger
DEBUGGER_MODE = True logger = Logger.get()
def _download_subscriptions_from_yaml_files(config: ConfigFile, args: argparse.Namespace) -> None: def _download_subscriptions_from_yaml_files(config: ConfigFile, args: argparse.Namespace) -> None:
@ -28,6 +28,7 @@ def _download_subscriptions_from_yaml_files(config: ConfigFile, args: argparse.N
) )
for subscription in subscriptions: for subscription in subscriptions:
logger.info("Beginning subscription download for %s", subscription.name)
subscription.to_subscription().download() subscription.to_subscription().download()
@ -56,27 +57,22 @@ def main():
config: ConfigFile = ConfigFile.from_file_path(args.config) config: ConfigFile = ConfigFile.from_file_path(args.config)
if args.subparser == "sub": if args.subparser == "sub":
_download_subscriptions_from_yaml_files(config=config, args=args) _download_subscriptions_from_yaml_files(config=config, args=args)
print("Subscription download complete!") logger.info("Subscription download complete!")
# 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, extra_args=extra_args)
print("Download complete!") logger.info("Download complete!")
if __name__ == "__main__": if __name__ == "__main__":
try: try:
main() main()
except ValidationException as validation_exception: except ValidationException as validation_exception:
if DEBUGGER_MODE: logger.error(validation_exception)
raise
print(validation_exception)
sys.exit(1) sys.exit(1)
except Exception as exc: # pylint: disable=broad-except except Exception as exc: # pylint: disable=broad-except
if DEBUGGER_MODE: logger.exception(
raise
print(traceback.format_exc())
print(
"A fatal error occurred. Please copy and paste the stacktrace above and make a Github " "A fatal error occurred. Please copy and paste the stacktrace above and make a Github "
"issue at https://github.com/jmbannon/ytdl-subscribe/issues with your config and " "issue at https://github.com/jmbannon/ytdl-subscribe/issues with your config and "
"command/subscription yaml file to reproduce. Thanks for trying ytdl-subscribe!" "command/subscription yaml file to reproduce. Thanks for trying ytdl-subscribe!"

View file

@ -31,10 +31,11 @@ class MusicTagsPlugin(Plugin[MusicTagsOptions]):
audio_file = mediafile.MediaFile(entry.get_download_file_path()) audio_file = mediafile.MediaFile(entry.get_download_file_path())
for tag, tag_formatter in self.plugin_options.tags.dict.items(): for tag, tag_formatter in self.plugin_options.tags.dict.items():
if tag not in audio_file.fields(): if tag not in audio_file.fields():
# TODO: Add proper logger and warn here self._logger.warning(
print( "tag '%s' is not supported for %s files. Supported tags: %s",
f"[ytld-sub: WARN] tag {tag} is not supported for {entry.ext} files. Supported " tag,
f"tags: {', '.join(audio_file.sorted_fields())}" entry.ext,
", ".join(audio_file.sorted_fields()),
) )
tag_value = self.overrides.apply_formatter(formatter=tag_formatter, entry=entry) tag_value = self.overrides.apply_formatter(formatter=tag_formatter, entry=entry)

View file

@ -6,6 +6,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.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
@ -38,6 +39,8 @@ class Plugin(Generic[PluginOptionsT], ABC):
self.output_directory = output_directory self.output_directory = output_directory
self.overrides = overrides self.overrides = overrides
self.__enhanced_download_archive = enhanced_download_archive self.__enhanced_download_archive = enhanced_download_archive
# TODO pass yaml snake case name in the class somewhere, and use it for the logger
self._logger = Logger.get(self.__class__.__name__)
@final @final
def archive_entry_file_name(self, entry: Entry, relative_file_path: str) -> None: def archive_entry_file_name(self, entry: Entry, relative_file_path: str) -> None:

View file

@ -0,0 +1,47 @@
import logging
import sys
from typing import Optional
class Logger:
@classmethod
def _get_formatter(cls) -> logging.Formatter:
"""
Returns
-------
Formatter for all ytdl-sub loggers
"""
return logging.Formatter("[%(name)s] %(message)s")
@classmethod
def _get_handler(cls) -> logging.StreamHandler:
"""
Returns
-------
Logger handler
"""
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
handler.setFormatter(cls._get_formatter())
return handler
@classmethod
def get(cls, name: Optional[str] = None) -> logging.Logger:
"""
Parameters
----------
name
Optional. Name of the logger which is included in the prefix like [ytdl-sub:<name>].
If None, the prefix is just [ytdl-sub]
Returns
-------
A configured logger
"""
logger_name = "ytdl-sub"
if name:
logger_name += f":{name}"
logger = logging.Logger(name=logger_name)
logger.addHandler(cls._get_handler())
return logger

View file

@ -13,6 +13,7 @@ from typing import Set
from yt_dlp import DateRange from yt_dlp import DateRange
from ytdl_sub.entries.entry import Entry from ytdl_sub.entries.entry import Entry
from ytdl_sub.utils.logger import Logger
@dataclass @dataclass
@ -336,6 +337,8 @@ class EnhancedDownloadArchive:
self._download_archive: Optional[DownloadArchive] = None self._download_archive: Optional[DownloadArchive] = None
self._download_mapping: Optional[DownloadMappings] = None self._download_mapping: Optional[DownloadMappings] = None
self._logger = Logger.get(name=subscription_name)
@property @property
def archive_file_name(self) -> str: def archive_file_name(self) -> str:
""" """
@ -460,10 +463,10 @@ class EnhancedDownloadArchive:
) )
for uid, mapping in stale_mappings.items(): for uid, mapping in stale_mappings.items():
print(f"[{uid}] Removing the following stale file(s):") self._logger.info("[%s] Removing the following stale file(s):", uid)
for file_name in mapping.file_names: for file_name in mapping.file_names:
file_path = Path(self.output_directory) / Path(file_name) file_path = Path(self.output_directory) / Path(file_name)
print(f" - {file_path}") self._logger.info(" - %s", file_path)
if os.path.exists(file_path): if os.path.exists(file_path):
os.remove(file_path) os.remove(file_path)