]> jfr.im git - yt-dlp.git/blame - youtube_dlc/postprocessor/common.py
[postprocessor] fix write_debug when no _downloader
[yt-dlp.git] / youtube_dlc / postprocessor / common.py
CommitLineData
dcddc10a
PH
1from __future__ import unicode_literals
2
dd29eb7f
S
3import os
4
43820c03 5from ..compat import compat_str
dd29eb7f 6from ..utils import (
eab9b2bc 7 cli_configuration_args,
dd29eb7f 8 encodeFilename,
eab9b2bc 9 PostProcessingError,
dd29eb7f 10)
496c1923
PH
11
12
13class PostProcessor(object):
14 """Post Processor class.
15
16 PostProcessor objects can be added to downloaders with their
17 add_post_processor() method. When the downloader has finished a
18 successful download, it will take its internal chain of PostProcessors
19 and start calling the run() method on each one of them, first with
20 an initial argument and then with the returned value of the previous
21 PostProcessor.
22
23 The chain will be stopped if one of them ever returns None or the end
24 of the chain is reached.
25
26 PostProcessor objects follow a "mutual registration" process similar
e35b23f5
S
27 to InfoExtractor objects.
28
29 Optionally PostProcessor can use a list of additional command-line arguments
30 with self._configuration_args.
496c1923
PH
31 """
32
33 _downloader = None
34
aa5d9a79 35 def __init__(self, downloader=None):
496c1923 36 self._downloader = downloader
43820c03 37 self.PP_NAME = self.pp_key()
38
39 @classmethod
40 def pp_key(cls):
41 name = cls.__name__[:-2]
42 return compat_str(name[6:]) if name[:6].lower() == 'ffmpeg' else name
1b77b347 43
fbced734 44 def to_screen(self, text, prefix=True, *args, **kwargs):
45 tag = '[%s] ' % self.PP_NAME if prefix else ''
f446cc66 46 if self._downloader:
fbced734 47 return self._downloader.to_screen('%s%s' % (tag, text), *args, **kwargs)
f446cc66 48
49 def report_warning(self, text, *args, **kwargs):
50 if self._downloader:
51 return self._downloader.report_warning(text, *args, **kwargs)
52
53 def report_error(self, text, *args, **kwargs):
54 if self._downloader:
55 return self._downloader.report_error(text, *args, **kwargs)
56
fbced734 57 def write_debug(self, text, prefix=True, *args, **kwargs):
58 tag = '[debug] ' if prefix else ''
2d692121 59 if self.get_param('verbose', False) and self._downloader:
fbced734 60 return self._downloader.to_screen('%s%s' % (tag, text), *args, **kwargs)
f446cc66 61
62 def get_param(self, name, default=None, *args, **kwargs):
63 if self._downloader:
64 return self._downloader.params.get(name, default, *args, **kwargs)
65 return default
496c1923
PH
66
67 def set_downloader(self, downloader):
68 """Sets the downloader for this PP."""
69 self._downloader = downloader
70
71 def run(self, information):
72 """Run the PostProcessor.
73
74 The "information" argument is a dictionary like the ones
75 composed by InfoExtractors. The only difference is that this
76 one has an extra field called "filepath" that points to the
77 downloaded file.
78
592e97e8
JMF
79 This method returns a tuple, the first element is a list of the files
80 that can be deleted, and the second of which is the updated
81 information.
496c1923
PH
82
83 In addition, this method may raise a PostProcessingError
84 exception if post processing fails.
85 """
592e97e8 86 return [], information # by default, keep file and do nothing
496c1923 87
dd29eb7f
S
88 def try_utime(self, path, atime, mtime, errnote='Cannot update utime of file'):
89 try:
90 os.utime(encodeFilename(path), (atime, mtime))
91 except Exception:
f446cc66 92 self.report_warning(errnote)
dd29eb7f 93
43820c03 94 def _configuration_args(self, default=[], exe=None):
eab9b2bc 95 key = self.pp_key().lower()
96 args, is_compat = cli_configuration_args(
97 self._downloader.params, 'postprocessor_args', key, default, exe)
98 return args if not is_compat or key != 'sponskrub' else default
e35b23f5 99
496c1923
PH
100
101class AudioConversionError(PostProcessingError):
102 pass