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.

95 lines
3.5 KiB

4 years ago
  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 botocore.compat import six
  14. from s3transfer.compat import accepts_kwargs
  15. from s3transfer.exceptions import InvalidSubscriberMethodError
  16. class BaseSubscriber(object):
  17. """The base subscriber class
  18. It is recommended that all subscriber implementations subclass and then
  19. override the subscription methods (i.e. on_{subsribe_type}() methods).
  20. """
  21. VALID_SUBSCRIBER_TYPES = [
  22. 'queued',
  23. 'progress',
  24. 'done'
  25. ]
  26. def __new__(cls, *args, **kwargs):
  27. cls._validate_subscriber_methods()
  28. return super(BaseSubscriber, cls).__new__(cls)
  29. @classmethod
  30. def _validate_subscriber_methods(cls):
  31. for subscriber_type in cls.VALID_SUBSCRIBER_TYPES:
  32. subscriber_method = getattr(cls, 'on_' + subscriber_type)
  33. if not six.callable(subscriber_method):
  34. raise InvalidSubscriberMethodError(
  35. 'Subscriber method %s must be callable.' %
  36. subscriber_method)
  37. if not accepts_kwargs(subscriber_method):
  38. raise InvalidSubscriberMethodError(
  39. 'Subscriber method %s must accept keyword '
  40. 'arguments (**kwargs)' % subscriber_method)
  41. def on_queued(self, future, **kwargs):
  42. """Callback to be invoked when transfer request gets queued
  43. This callback can be useful for:
  44. * Keeping track of how many transfers have been requested
  45. * Providing the expected transfer size through
  46. future.meta.provide_transfer_size() so a HeadObject would not
  47. need to be made for copies and downloads.
  48. :type future: s3transfer.futures.TransferFuture
  49. :param future: The TransferFuture representing the requested transfer.
  50. """
  51. pass
  52. def on_progress(self, future, bytes_transferred, **kwargs):
  53. """Callback to be invoked when progress is made on transfer
  54. This callback can be useful for:
  55. * Recording and displaying progress
  56. :type future: s3transfer.futures.TransferFuture
  57. :param future: The TransferFuture representing the requested transfer.
  58. :type bytes_transferred: int
  59. :param bytes_transferred: The number of bytes transferred for that
  60. invocation of the callback. Note that a negative amount can be
  61. provided, which usually indicates that an in-progress request
  62. needed to be retried and thus progress was rewound.
  63. """
  64. pass
  65. def on_done(self, future, **kwargs):
  66. """Callback to be invoked once a transfer is done
  67. This callback can be useful for:
  68. * Recording and displaying whether the transfer succeeded or
  69. failed using future.result()
  70. * Running some task after the transfer completed like changing
  71. the last modified time of a downloaded file.
  72. :type future: s3transfer.futures.TransferFuture
  73. :param future: The TransferFuture representing the requested transfer.
  74. """
  75. pass