Rename --pp-params to --postprocessor-args and access value as super class attribute
[youtube-dl] / youtube_dl / postprocessor / common.py
1 from __future__ import unicode_literals
2
3 import os
4 import shlex
5
6 from ..utils import (
7     PostProcessingError,
8     encodeFilename,
9 )
10
11
12 class PostProcessor(object):
13     """Post Processor class.
14
15     PostProcessor objects can be added to downloaders with their
16     add_post_processor() method. When the downloader has finished a
17     successful download, it will take its internal chain of PostProcessors
18     and start calling the run() method on each one of them, first with
19     an initial argument and then with the returned value of the previous
20     PostProcessor.
21
22     The chain will be stopped if one of them ever returns None or the end
23     of the chain is reached.
24
25     PostProcessor objects follow a "mutual registration" process similar
26     to InfoExtractor objects. And it can receive parameters from CLI trough
27     --postprocessor-args.
28     """
29
30     _downloader = None
31
32     def __init__(self, downloader=None, extra_cmd_args=None):
33         self._extra_cmd_args = shlex.split(extra_cmd_args or '')
34         self._downloader = downloader
35
36     def set_downloader(self, downloader):
37         """Sets the downloader for this PP."""
38         self._downloader = downloader
39
40     def run(self, information):
41         """Run the PostProcessor.
42
43         The "information" argument is a dictionary like the ones
44         composed by InfoExtractors. The only difference is that this
45         one has an extra field called "filepath" that points to the
46         downloaded file.
47
48         This method returns a tuple, the first element is a list of the files
49         that can be deleted, and the second of which is the updated
50         information.
51
52         In addition, this method may raise a PostProcessingError
53         exception if post processing fails.
54         """
55         return [], information  # by default, keep file and do nothing
56
57     def try_utime(self, path, atime, mtime, errnote='Cannot update utime of file'):
58         try:
59             os.utime(encodeFilename(path), (atime, mtime))
60         except Exception:
61             self._downloader.report_warning(errnote)
62
63
64 class AudioConversionError(PostProcessingError):
65     pass