Standardize write_debug

This commit is contained in:
pukkandan 2021-05-14 13:15:29 +05:30
parent d908aa636a
commit 0760b0a7e2
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698
5 changed files with 34 additions and 34 deletions

View File

@ -542,8 +542,7 @@ def check_deprecated(param, option, suggestion):
def preload_download_archive(fn):
if fn is None:
return False
if self.params.get('verbose'):
self._write_string('[debug] Loading archive file %r\n' % fn)
self.write_debug('Loading archive file %r\n' % fn)
try:
with locked_file(fn, 'r', encoding='utf-8') as archive_file:
for line in archive_file:
@ -649,17 +648,11 @@ def _bidi_workaround(self, message):
for _ in range(line_count))
return res[:-len('\n')]
def to_screen(self, message, skip_eol=False):
"""Print message to stdout if not in quiet mode."""
return self.to_stdout(
message, skip_eol,
quiet=self.params.get('quiet', False))
def _write_string(self, s, out=None):
write_string(s, out=out, encoding=self.params.get('encoding'))
def to_stdout(self, message, skip_eol=False, quiet=False):
"""Print message to stdout if not in quiet mode."""
"""Print message to stdout"""
if self.params.get('logger'):
self.params['logger'].debug(message)
elif not quiet:
@ -670,7 +663,7 @@ def to_stdout(self, message, skip_eol=False, quiet=False):
self._write_string(output, self._screen_file)
def to_stderr(self, message):
"""Print message to stderr."""
"""Print message to stderr"""
assert isinstance(message, compat_str)
if self.params.get('logger'):
self.params['logger'].error(message)
@ -748,6 +741,11 @@ def trouble(self, message=None, tb=None):
raise DownloadError(message, exc_info)
self._download_retcode = 1
def to_screen(self, message, skip_eol=False):
"""Print message to stdout if not in quiet mode"""
self.to_stdout(
message, skip_eol, quiet=self.params.get('quiet', False))
def report_warning(self, message):
'''
Print the message to stderr, it will be prefixed with 'WARNING:'
@ -777,6 +775,16 @@ def report_error(self, message, tb=None):
error_message = '%s %s' % (_msg_header, message)
self.trouble(error_message, tb)
def write_debug(self, message):
'''Log debug message or Print message to stderr'''
if not self.params.get('verbose', False):
return
message = '[debug] %s' % message
if self.params.get('logger'):
self.params['logger'].debug(message)
else:
self._write_string('%s\n' % message)
def report_file_already_downloaded(self, file_name):
"""Report file has already been fully downloaded."""
try:
@ -2081,8 +2089,7 @@ def is_wellformed(f):
req_format = self.params.get('format')
if req_format is None:
req_format = self._default_format_spec(info_dict, download=download)
if self.params.get('verbose'):
self.to_screen('[debug] Default format spec: %s' % req_format)
self.write_debug('Default format spec: %s' % req_format)
format_selector = self.build_format_selector(req_format)
@ -2249,8 +2256,7 @@ def dl(self, name, info, subtitle=False, test=False):
if not test:
for ph in self._progress_hooks:
fd.add_progress_hook(ph)
if self.params.get('verbose'):
self.to_screen('[debug] Invoking downloader on %r' % info.get('url'))
self.write_debug('Invoking downloader on %r' % info.get('url'))
new_info = dict(info)
if new_info.get('http_headers') is None:
new_info['http_headers'] = self._calc_headers(new_info)

View File

@ -402,5 +402,4 @@ def _debug_cmd(self, args, exe=None):
if exe is None:
exe = os.path.basename(str_args[0])
self.to_screen('[debug] %s command line: %s' % (
exe, shell_quote(str_args)))
self.write_debug('%s command line: %s' % (exe, shell_quote(str_args)))

View File

@ -522,10 +522,8 @@ def _initialize_geo_bypass(self, geo_bypass_context):
if ip_block:
self._x_forwarded_for_ip = GeoUtils.random_ipv4(ip_block)
if self._downloader.params.get('verbose', False):
self._downloader.to_screen(
'[debug] Using fake IP %s as X-Forwarded-For.'
% self._x_forwarded_for_ip)
self._downloader.write_debug(
'[debug] Using fake IP %s as X-Forwarded-For' % self._x_forwarded_for_ip)
return
# Path 2: bypassing based on country code
@ -543,10 +541,8 @@ def _initialize_geo_bypass(self, geo_bypass_context):
if country:
self._x_forwarded_for_ip = GeoUtils.random_ipv4(country)
if self._downloader.params.get('verbose', False):
self._downloader.to_screen(
'[debug] Using fake IP %s (%s) as X-Forwarded-For.'
% (self._x_forwarded_for_ip, country.upper()))
self._downloader.write_debug(
'Using fake IP %s (%s) as X-Forwarded-For' % (self._x_forwarded_for_ip, country.upper()))
def extract(self, url):
"""Extracts URL information and returns it in list of dicts."""
@ -1594,12 +1590,12 @@ def add_item(field, reverse, closest, limit_text):
else limits[0] if has_limit and not has_multiple_limits
else None)
def print_verbose_info(self, to_screen):
def print_verbose_info(self, write_debug):
if self._sort_user:
to_screen('[debug] Sort order given by user: %s' % ', '.join(self._sort_user))
write_debug('Sort order given by user: %s' % ', '.join(self._sort_user))
if self._sort_extractor:
to_screen('[debug] Sort order given by extractor: %s' % ', '.join(self._sort_extractor))
to_screen('[debug] Formats sorted by: %s' % ', '.join(['%s%s%s' % (
write_debug('Sort order given by extractor: %s' % ', '.join(self._sort_extractor))
write_debug('Formats sorted by: %s' % ', '.join(['%s%s%s' % (
'+' if self._get_field_setting(field, 'reverse') else '', field,
'%s%s(%s)' % ('~' if self._get_field_setting(field, 'closest') else ':',
self._get_field_setting(field, 'limit_text'),
@ -1691,7 +1687,7 @@ def _sort_formats(self, formats, field_preference=[]):
format_sort = self.FormatSort() # params and to_screen are taken from the downloader
format_sort.evaluate_params(self._downloader.params, field_preference)
if self._downloader.params.get('verbose', False):
format_sort.print_verbose_info(self._downloader.to_screen)
format_sort.print_verbose_info(self._downloader.write_debug)
formats.sort(key=lambda f: format_sort.calculate_preference(f))
def _check_formats(self, formats, video_id):

View File

@ -27,7 +27,7 @@ def _real_extract(self, url):
'Simply remove the parameter in your command or configuration.'
) % url
if not self._downloader.params.get('verbose'):
msg += ' Add -v to the command line to see what arguments and configuration yt-dlp got.'
msg += ' Add -v to the command line to see what arguments and configuration yt-dlp has'
raise ExtractorError(msg, expected=True)

View File

@ -54,10 +54,9 @@ def report_error(self, text, *args, **kwargs):
if self._downloader:
return self._downloader.report_error(text, *args, **kwargs)
def write_debug(self, text, prefix=True, *args, **kwargs):
tag = '[debug] ' if prefix else ''
if self.get_param('verbose', False) and self._downloader:
return self._downloader.to_screen('%s%s' % (tag, text), *args, **kwargs)
def write_debug(self, text, *args, **kwargs):
if self._downloader:
return self._downloader.write_debug(text, *args, **kwargs)
def get_param(self, name, default=None, *args, **kwargs):
if self._downloader: