[core] Simplify code for report_warning().

This commit is contained in:
mk-pmb 2024-05-15 06:16:56 +02:00
parent e0727e4ab6
commit 76eab55b36

View File

@ -625,6 +625,18 @@ class YoutubeDL(object):
raise DownloadError(message, exc_info) raise DownloadError(message, exc_info)
self._download_retcode = 1 self._download_retcode = 1
def __can_use_color_codes(self, output_file=None):
"""Decide if we should use color codes for the given output channel."""
# Try to keep criteria ordered by computational effort, easiest first.
if compat_os_name == 'nt':
return False
if self.params.get('no_color'):
return False
if output_file is not None:
if not output_file.isatty():
return False
return True
def report_warning(self, message, only_once=False, _cache={}): def report_warning(self, message, only_once=False, _cache={}):
''' '''
Print the message to stderr, it will be prefixed with 'WARNING:' Print the message to stderr, it will be prefixed with 'WARNING:'
@ -637,17 +649,18 @@ class YoutubeDL(object):
if m_cnt > 0: if m_cnt > 0:
return return
if self.params.get('logger') is not None: custom_logger = self.params.get('logger')
self.params['logger'].warning(message) if custom_logger is not None:
else: custom_logger.warning(message)
return
if self.params.get('no_warnings'): if self.params.get('no_warnings'):
return return
if not self.params.get('no_color') and self._err_file.isatty() and compat_os_name != 'nt':
_msg_header = '\033[0;33mWARNING:\033[0m' prefix = 'WARNING:'
else: if self.__can_use_color_codes(output_file=self._err_file):
_msg_header = 'WARNING:' prefix = '\033[0;33m' + prefix + '\033[0m'
warning_message = '%s %s' % (_msg_header, message) self.to_stderr(prefix + ' ' + message)
self.to_stderr(warning_message)
def report_error(self, message, *args, **kwargs): def report_error(self, message, *args, **kwargs):
''' '''