You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.3 KiB

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