common.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. --pp-params.
  20. """
  21. _downloader = None
  22. def __init__(self, downloader=None):
  23. self._downloader = downloader
  24. def set_downloader(self, downloader):
  25. """Sets the downloader for this PP."""
  26. self._downloader = downloader
  27. def run(self, information):
  28. """Run the PostProcessor.
  29. The "information" argument is a dictionary like the ones
  30. composed by InfoExtractors. The only difference is that this
  31. one has an extra field called "filepath" that points to the
  32. downloaded file.
  33. This method returns a tuple, the first element is a list of the files
  34. that can be deleted, and the second of which is the updated
  35. information.
  36. In addition, this method may raise a PostProcessingError
  37. exception if post processing fails.
  38. """
  39. return [], information # by default, keep file and do nothing
  40. def try_utime(self, path, atime, mtime, errnote='Cannot update utime of file'):
  41. try:
  42. os.utime(encodeFilename(path), (atime, mtime))
  43. except Exception:
  44. self._downloader.report_warning(errnote)
  45. class AudioConversionError(PostProcessingError):
  46. pass