common.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. And it can receive parameters from CLI trough
  19. --postprocessor-args.
  20. """
  21. _downloader = None
  22. def __init__(self, downloader=None):
  23. self._extra_cmd_args = downloader.params.get('postprocessor_args')
  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. class AudioConversionError(PostProcessingError):
  47. pass