subscribers.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://aws.amazon.com/apache2.0/
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. from s3transfer.compat import accepts_kwargs
  14. from s3transfer.exceptions import InvalidSubscriberMethodError
  15. class BaseSubscriber:
  16. """The base subscriber class
  17. It is recommended that all subscriber implementations subclass and then
  18. override the subscription methods (i.e. on_{subsribe_type}() methods).
  19. """
  20. VALID_SUBSCRIBER_TYPES = ['queued', 'progress', 'done']
  21. def __new__(cls, *args, **kwargs):
  22. cls._validate_subscriber_methods()
  23. return super().__new__(cls)
  24. @classmethod
  25. def _validate_subscriber_methods(cls):
  26. for subscriber_type in cls.VALID_SUBSCRIBER_TYPES:
  27. subscriber_method = getattr(cls, 'on_' + subscriber_type)
  28. if not callable(subscriber_method):
  29. raise InvalidSubscriberMethodError(
  30. 'Subscriber method %s must be callable.'
  31. % subscriber_method
  32. )
  33. if not accepts_kwargs(subscriber_method):
  34. raise InvalidSubscriberMethodError(
  35. 'Subscriber method %s must accept keyword '
  36. 'arguments (**kwargs)' % subscriber_method
  37. )
  38. def on_queued(self, future, **kwargs):
  39. """Callback to be invoked when transfer request gets queued
  40. This callback can be useful for:
  41. * Keeping track of how many transfers have been requested
  42. * Providing the expected transfer size through
  43. future.meta.provide_transfer_size() so a HeadObject would not
  44. need to be made for copies and downloads.
  45. :type future: s3transfer.futures.TransferFuture
  46. :param future: The TransferFuture representing the requested transfer.
  47. """
  48. pass
  49. def on_progress(self, future, bytes_transferred, **kwargs):
  50. """Callback to be invoked when progress is made on transfer
  51. This callback can be useful for:
  52. * Recording and displaying progress
  53. :type future: s3transfer.futures.TransferFuture
  54. :param future: The TransferFuture representing the requested transfer.
  55. :type bytes_transferred: int
  56. :param bytes_transferred: The number of bytes transferred for that
  57. invocation of the callback. Note that a negative amount can be
  58. provided, which usually indicates that an in-progress request
  59. needed to be retried and thus progress was rewound.
  60. """
  61. pass
  62. def on_done(self, future, **kwargs):
  63. """Callback to be invoked once a transfer is done
  64. This callback can be useful for:
  65. * Recording and displaying whether the transfer succeeded or
  66. failed using future.result()
  67. * Running some task after the transfer completed like changing
  68. the last modified time of a downloaded file.
  69. :type future: s3transfer.futures.TransferFuture
  70. :param future: The TransferFuture representing the requested transfer.
  71. """
  72. pass