From f446cc66675629d3e043800d9ce74d3327f9fdfa Mon Sep 17 00:00:00 2001 From: pukkandan Date: Sun, 10 Jan 2021 19:14:54 +0530 Subject: [PATCH] Create `to_screen` and similar functions in postprocessor/common `to_screen`, `report_warning`, `report_error`, `write_debug`, `get_param` This is a first step in standardizing these function. This has to be done eventually for extractors and downloaders too --- youtube_dlc/postprocessor/common.py | 24 ++++++++++++++++++--- youtube_dlc/postprocessor/embedthumbnail.py | 8 +++---- youtube_dlc/postprocessor/ffmpeg.py | 23 ++++++++------------ youtube_dlc/postprocessor/sponskrub.py | 11 +++++----- youtube_dlc/postprocessor/xattrpp.py | 8 +++---- 5 files changed, 42 insertions(+), 32 deletions(-) diff --git a/youtube_dlc/postprocessor/common.py b/youtube_dlc/postprocessor/common.py index 6e84ff592..1a893d05f 100644 --- a/youtube_dlc/postprocessor/common.py +++ b/youtube_dlc/postprocessor/common.py @@ -37,7 +37,25 @@ class PostProcessor(object): self.PP_NAME = self.__class__.__name__[:-2] def to_screen(self, text, *args, **kwargs): - return self._downloader.to_screen('[%s] %s' % (self.PP_NAME, text), *args, **kwargs) + if self._downloader: + return self._downloader.to_screen('[%s] %s' % (self.PP_NAME, text), *args, **kwargs) + + def report_warning(self, text, *args, **kwargs): + if self._downloader: + return self._downloader.report_warning(text, *args, **kwargs) + + def report_error(self, text, *args, **kwargs): + if self._downloader: + return self._downloader.report_error(text, *args, **kwargs) + + def write_debug(self, text, *args, **kwargs): + if self.get_param('verbose', False): + return self._downloader.to_screen('[debug] %s' % text, *args, **kwargs) + + def get_param(self, name, default=None, *args, **kwargs): + if self._downloader: + return self._downloader.params.get(name, default, *args, **kwargs) + return default def set_downloader(self, downloader): """Sets the downloader for this PP.""" @@ -64,10 +82,10 @@ class PostProcessor(object): try: os.utime(encodeFilename(path), (atime, mtime)) except Exception: - self._downloader.report_warning(errnote) + self.report_warning(errnote) def _configuration_args(self, default=[]): - args = self._downloader.params.get('postprocessor_args', {}) + args = self.get_param('postprocessor_args', {}) if isinstance(args, list): # for backward compatibility args = {'default': args, 'sponskrub': []} return cli_configuration_args(args, self.PP_NAME.lower(), args.get('default', [])) diff --git a/youtube_dlc/postprocessor/embedthumbnail.py b/youtube_dlc/postprocessor/embedthumbnail.py index 3055a8c28..8e78ede00 100644 --- a/youtube_dlc/postprocessor/embedthumbnail.py +++ b/youtube_dlc/postprocessor/embedthumbnail.py @@ -41,8 +41,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): thumbnail_filename = info['thumbnails'][-1]['filename'] if not os.path.exists(encodeFilename(thumbnail_filename)): - self._downloader.report_warning( - 'Skipping embedding the thumbnail because the file is missing.') + self.report_warning('Skipping embedding the thumbnail because the file is missing.') return [], info def is_webp(path): @@ -125,8 +124,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): self.to_screen('Adding thumbnail to "%s"' % filename) - if self._downloader.params.get('verbose', False): - self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd)) + self.verbose_message('AtomicParsley command line: %s' % shell_quote(cmd)) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process_communicate_or_kill(p) @@ -140,7 +138,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): # for formats that don't support thumbnails (like 3gp) AtomicParsley # won't create to the temporary file if b'No changes' in stdout: - self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail') + self.report_warning('The file format doesn\'t support embedding a thumbnail') else: os.remove(encodeFilename(filename)) os.rename(encodeFilename(temp_filename), encodeFilename(filename)) diff --git a/youtube_dlc/postprocessor/ffmpeg.py b/youtube_dlc/postprocessor/ffmpeg.py index c6ba1e221..9c6065018 100644 --- a/youtube_dlc/postprocessor/ffmpeg.py +++ b/youtube_dlc/postprocessor/ffmpeg.py @@ -68,8 +68,7 @@ class FFmpegPostProcessor(PostProcessor): self._versions[self.basename], required_version): warning = 'Your copy of %s is outdated, update %s to version %s or newer if you encounter any errors.' % ( self.basename, self.basename, required_version) - if self._downloader: - self._downloader.report_warning(warning) + self.report_warning(warning) @staticmethod def get_versions(downloader=None): @@ -99,11 +98,11 @@ class FFmpegPostProcessor(PostProcessor): self._paths = None self._versions = None if self._downloader: - prefer_ffmpeg = self._downloader.params.get('prefer_ffmpeg', True) - location = self._downloader.params.get('ffmpeg_location') + prefer_ffmpeg = self.get_param('prefer_ffmpeg', True) + location = self.get_param('ffmpeg_location') if location is not None: if not os.path.exists(location): - self._downloader.report_warning( + self.report_warning( 'ffmpeg-location %s does not exist! ' 'Continuing without avconv/ffmpeg.' % (location)) self._versions = {} @@ -111,7 +110,7 @@ class FFmpegPostProcessor(PostProcessor): elif not os.path.isdir(location): basename = os.path.splitext(os.path.basename(location))[0] if basename not in programs: - self._downloader.report_warning( + self.report_warning( 'Cannot identify executable %s, its basename should be one of %s. ' 'Continuing without avconv/ffmpeg.' % (location, ', '.join(programs))) @@ -177,9 +176,7 @@ class FFmpegPostProcessor(PostProcessor): encodeFilename(self.executable, True), encodeArgument('-i')] cmd.append(encodeFilename(self._ffmpeg_filename_argument(path), True)) - if self._downloader.params.get('verbose', False): - self._downloader.to_screen( - '[debug] %s command line: %s' % (self.basename, shell_quote(cmd))) + self.write_debug('%s command line: %s' % (self.basename, shell_quote(cmd))) handle = subprocess.Popen( cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE) @@ -228,8 +225,7 @@ class FFmpegPostProcessor(PostProcessor): + [encodeArgument(o) for o in opts] + [encodeFilename(self._ffmpeg_filename_argument(out_path), True)]) - if self._downloader.params.get('verbose', False): - self._downloader.to_screen('[debug] ffmpeg command line: %s' % shell_quote(cmd)) + self.write_debug('ffmpeg command line: %s' % shell_quote(cmd)) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) stdout, stderr = process_communicate_or_kill(p) if p.returncode != 0: @@ -566,8 +562,7 @@ class FFmpegMergerPP(FFmpegPostProcessor): 'youtube-dlc will download single file media. ' 'Update %s to version %s or newer to fix this.') % ( self.basename, self.basename, required_version) - if self._downloader: - self._downloader.report_warning(warning) + self.report_warning(warning) return False return True @@ -656,7 +651,7 @@ class FFmpegSubtitlesConvertorPP(FFmpegPostProcessor): new_file = subtitles_filename(filename, lang, new_ext, info.get('ext')) if ext in ('dfxp', 'ttml', 'tt'): - self._downloader.report_warning( + self.report_warning( 'You have requested to convert dfxp (TTML) subtitles into another format, ' 'which results in style information loss') diff --git a/youtube_dlc/postprocessor/sponskrub.py b/youtube_dlc/postprocessor/sponskrub.py index 37f6c0290..9215913bc 100644 --- a/youtube_dlc/postprocessor/sponskrub.py +++ b/youtube_dlc/postprocessor/sponskrub.py @@ -46,16 +46,16 @@ class SponSkrubPP(PostProcessor): self.to_screen('Skipping sponskrub since it is not a YouTube video') return [], information if self.cutout and not self.force and not information.get('__real_download', False): - self._downloader.to_screen( - '[sponskrub] Skipping sponskrub since the video was already downloaded. ' + self.report_warning( + 'Skipping sponskrub since the video was already downloaded. ' 'Use --sponskrub-force to run sponskrub anyway') return [], information self.to_screen('Trying to %s sponsor sections' % ('remove' if self.cutout else 'mark')) if self.cutout: - self._downloader.to_screen('WARNING: Cutting out sponsor segments will cause the subtitles to go out of sync.') + self.report_warning('Cutting out sponsor segments will cause the subtitles to go out of sync.') if not information.get('__real_download', False): - self._downloader.to_screen('WARNING: If sponskrub is run multiple times, unintended parts of the video could be cut out.') + self.report_warning('If sponskrub is run multiple times, unintended parts of the video could be cut out.') filename = information['filepath'] temp_filename = filename + '.' + self._temp_ext + os.path.splitext(filename)[1] @@ -68,8 +68,7 @@ class SponSkrubPP(PostProcessor): cmd += ['--', information['id'], filename, temp_filename] cmd = [encodeArgument(i) for i in cmd] - if self._downloader.params.get('verbose', False): - self._downloader.to_screen('[debug] sponskrub command line: %s' % shell_quote(cmd)) + self.write_debug('sponskrub command line: %s' % shell_quote(cmd)) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) stdout, stderr = p.communicate() diff --git a/youtube_dlc/postprocessor/xattrpp.py b/youtube_dlc/postprocessor/xattrpp.py index 85834db45..3d31f0ce5 100644 --- a/youtube_dlc/postprocessor/xattrpp.py +++ b/youtube_dlc/postprocessor/xattrpp.py @@ -57,16 +57,16 @@ class XAttrMetadataPP(PostProcessor): return [], info except XAttrUnavailableError as e: - self._downloader.report_error(str(e)) + self.report_error(str(e)) return [], info except XAttrMetadataError as e: if e.reason == 'NO_SPACE': - self._downloader.report_warning( + self.report_warning( 'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. ' + (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize()) elif e.reason == 'VALUE_TOO_LONG': - self._downloader.report_warning( + self.report_warning( 'Unable to write extended attributes due to too long values.') else: msg = 'This filesystem doesn\'t support extended attributes. ' @@ -74,5 +74,5 @@ class XAttrMetadataPP(PostProcessor): msg += 'You need to use NTFS.' else: msg += '(You may have to enable them in your /etc/fstab)' - self._downloader.report_error(msg) + self.report_error(msg) return [], info