codekingpro/portable-devtools
114k
1# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"). You4# may not use this file except in compliance with the License. A copy of5# the License is located at6#7# http://aws.amazon.com/apache2.0/8#9# or in the "license" file accompanying this file. This file is10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF11# ANY KIND, either express or implied. See the License for the specific12# language governing permissions and limitations under the License.13from functools import lru_cache14 15from s3transfer.compat import accepts_kwargs16from s3transfer.exceptions import InvalidSubscriberMethodError17 18 19class BaseSubscriber:20 """The base subscriber class21 22 It is recommended that all subscriber implementations subclass and then23 override the subscription methods (i.e. on_{subsribe_type}() methods).24 """25 26 VALID_SUBSCRIBER_TYPES = ['queued', 'progress', 'done']27 28 def __new__(cls, *args, **kwargs):29 cls._validate_subscriber_methods()30 return super().__new__(cls)31 32 @classmethod33 @lru_cache()34 def _validate_subscriber_methods(cls):35 for subscriber_type in cls.VALID_SUBSCRIBER_TYPES:36 subscriber_method = getattr(cls, 'on_' + subscriber_type)37 if not callable(subscriber_method):38 raise InvalidSubscriberMethodError(39 'Subscriber method %s must be callable.'40 % subscriber_method41 )42 43 if not accepts_kwargs(subscriber_method):44 raise InvalidSubscriberMethodError(45 'Subscriber method %s must accept keyword '46 'arguments (**kwargs)' % subscriber_method47 )48 49 def on_queued(self, future, **kwargs):50 """Callback to be invoked when transfer request gets queued51 52 This callback can be useful for:53 54 * Keeping track of how many transfers have been requested55 * Providing the expected transfer size through56 future.meta.provide_transfer_size() so a HeadObject would not57 need to be made for copies and downloads.58 59 :type future: s3transfer.futures.TransferFuture60 :param future: The TransferFuture representing the requested transfer.61 """62 pass63 64 def on_progress(self, future, bytes_transferred, **kwargs):65 """Callback to be invoked when progress is made on transfer66 67 This callback can be useful for:68 69 * Recording and displaying progress70 71 :type future: s3transfer.futures.TransferFuture72 :param future: The TransferFuture representing the requested transfer.73 74 :type bytes_transferred: int75 :param bytes_transferred: The number of bytes transferred for that76 invocation of the callback. Note that a negative amount can be77 provided, which usually indicates that an in-progress request78 needed to be retried and thus progress was rewound.79 """80 pass81 82 def on_done(self, future, **kwargs):83 """Callback to be invoked once a transfer is done84 85 This callback can be useful for:86 87 * Recording and displaying whether the transfer succeeded or88 failed using future.result()89 * Running some task after the transfer completed like changing90 the last modified time of a downloaded file.91 92 :type future: s3transfer.futures.TransferFuture93 :param future: The TransferFuture representing the requested transfer.94 """95 pass96 