From 0760b0a7e2462b6513e1dd76f8389b8c0b185ac3 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Fri, 14 May 2021 13:15:29 +0530 Subject: [PATCH] Standardize `write_debug` --- yt_dlp/YoutubeDL.py | 34 ++++++++++++++++++------------ yt_dlp/downloader/common.py | 3 +-- yt_dlp/extractor/common.py | 22 ++++++++----------- yt_dlp/extractor/commonmistakes.py | 2 +- yt_dlp/postprocessor/common.py | 7 +++--- 5 files changed, 34 insertions(+), 34 deletions(-) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 1f9a2dcef..f06a3ddbd 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -542,8 +542,7 @@ class YoutubeDL(object): 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 @@ class YoutubeDL(object): 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 @@ class YoutubeDL(object): 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 @@ class YoutubeDL(object): 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 @@ class YoutubeDL(object): 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 @@ class YoutubeDL(object): 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 @@ class YoutubeDL(object): 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) diff --git a/yt_dlp/downloader/common.py b/yt_dlp/downloader/common.py index 0cf7b13e6..b8c81eaf6 100644 --- a/yt_dlp/downloader/common.py +++ b/yt_dlp/downloader/common.py @@ -402,5 +402,4 @@ class FileDownloader(object): 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))) diff --git a/yt_dlp/extractor/common.py b/yt_dlp/extractor/common.py index cd3aa52a0..119af1198 100644 --- a/yt_dlp/extractor/common.py +++ b/yt_dlp/extractor/common.py @@ -522,10 +522,8 @@ class InfoExtractor(object): 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 @@ class InfoExtractor(object): 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 @@ class InfoExtractor(object): 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 @@ class InfoExtractor(object): 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): diff --git a/yt_dlp/extractor/commonmistakes.py b/yt_dlp/extractor/commonmistakes.py index 593ff1835..1a5dcbd8b 100644 --- a/yt_dlp/extractor/commonmistakes.py +++ b/yt_dlp/extractor/commonmistakes.py @@ -27,7 +27,7 @@ class CommonMistakesIE(InfoExtractor): '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) diff --git a/yt_dlp/postprocessor/common.py b/yt_dlp/postprocessor/common.py index 3c316b349..b01ba5ee0 100644 --- a/yt_dlp/postprocessor/common.py +++ b/yt_dlp/postprocessor/common.py @@ -54,10 +54,9 @@ class PostProcessor(object): 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: