Skip to content

Commit

Permalink
[core] Simplify code for report_warning().
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-pmb committed May 15, 2024
1 parent e0727e4 commit a5130a6
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions youtube_dl/YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,18 @@ def trouble(self, *args, **kwargs):
raise DownloadError(message, exc_info)
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={}):
'''
Print the message to stderr, it will be prefixed with 'WARNING:'
Expand All @@ -637,17 +649,19 @@ def report_warning(self, message, only_once=False, _cache={}):
if m_cnt > 0:
return

if self.params.get('logger') is not None:
self.params['logger'].warning(message)
else:
if self.params.get('no_warnings'):
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'
else:
_msg_header = 'WARNING:'
warning_message = '%s %s' % (_msg_header, message)
self.to_stderr(warning_message)
custom_logger = self.params.get('logger')
if custom_logger is not None:
custom_logger.warning(message)
return

if self.params.get('no_warnings'):
return

msg = 'WARNING:'
if self.can_use_color_codes(output_file=self._err_file):
msg = '\033[0;33m' + msg + '\033[0m'
msg += ' ' + message
self.to_stderr(msg)

def report_error(self, message, *args, **kwargs):
'''
Expand Down

0 comments on commit a5130a6

Please sign in to comment.