<s> """<STR_LIT>""" <EOL> from zope . interface import implements <EOL> from lbrynet . core . client . StreamProgressManager import FullStreamProgressManager <EOL> from lbrynet . core . StreamDescriptor import StreamMetadata <EOL> from lbrynet . lbryfile . client . LBRYFileDownloader import LBRYFileSaver , LBRYFileDownloader <EOL> from lbrynet . lbryfilemanager . LBRYFileStatusReport import LBRYFileStatusReport <EOL> from lbrynet . interfaces import IStreamDownloaderFactory <EOL> from lbrynet . lbryfile . StreamDescriptor import save_sd_info <EOL> from twisted . internet import defer <EOL> class ManagedLBRYFileDownloader ( LBRYFileSaver ) : <EOL> STATUS_RUNNING = "<STR_LIT>" <EOL> STATUS_STOPPED = "<STR_LIT>" <EOL> STATUS_FINISHED = "<STR_LIT>" <EOL> def __init__ ( self , rowid , stream_hash , peer_finder , rate_limiter , blob_manager , stream_info_manager , <EOL> lbry_file_manager , payment_rate_manager , wallet , download_directory , upload_allowed , <EOL> file_name = None ) : <EOL> LBRYFileSaver . __init__ ( self , stream_hash , peer_finder , rate_limiter , blob_manager , <EOL> stream_info_manager , payment_rate_manager , wallet , download_directory , <EOL> upload_allowed , file_name ) <EOL> self . rowid = rowid <EOL> self . lbry_file_manager = lbry_file_manager <EOL> self . saving_status = False <EOL> def restore ( self ) : <EOL> d = self . lbry_file_manager . get_lbry_file_status ( self ) <EOL> def restore_status ( status ) : <EOL> if status == ManagedLBRYFileDownloader . STATUS_RUNNING : <EOL> return self . start ( ) <EOL> elif status == ManagedLBRYFileDownloader . STATUS_STOPPED : <EOL> return defer . succeed ( False ) <EOL> elif status == ManagedLBRYFileDownloader . STATUS_FINISHED : <EOL> self . completed = True <EOL> return defer . succeed ( True ) <EOL> d . addCallback ( restore_status ) <EOL> return d <EOL> def stop ( self , err = None , change_status = True ) : <EOL> def set_saving_status_done ( ) : <EOL> self . saving_status = False <EOL> d = LBRYFileDownloader . stop ( self , err = err ) <EOL> if change_status is True : <EOL> self . saving_status = True <EOL> d . addCallback ( lambda _ : self . _save_status ( ) ) <EOL> d . addCallback ( lambda _ : set_saving_status_done ( ) ) <EOL> return d <EOL> def status ( self ) : <EOL> def find_completed_blobhashes ( blobs ) : <EOL> blobhashes = [ b [ <NUM_LIT:0> ] for b in blobs if b [ <NUM_LIT:0> ] is not None ] <EOL> def get_num_completed ( completed_blobs ) : <EOL> return len ( completed_blobs ) , len ( blobhashes ) <EOL> inner_d = self . blob_manager . completed_blobs ( blobhashes ) <EOL> inner_d . addCallback ( get_num_completed ) <EOL> return inner_d <EOL> def make_full_status ( progress ) : <EOL> num_completed = progress [ <NUM_LIT:0> ] <EOL> num_known = progress [ <NUM_LIT:1> ] <EOL> if self . completed is True : <EOL> s = "<STR_LIT>" <EOL> elif self . stopped is True : <EOL> s = "<STR_LIT>" <EOL> else : <EOL> s = "<STR_LIT>" <EOL> status = LBRYFileStatusReport ( self . file_name , num_completed , num_known , s ) <EOL> return status <EOL> d = self . stream_info_manager . get_blobs_for_stream ( self . stream_hash ) <EOL> d . addCallback ( find_completed_blobhashes ) <EOL> d . addCallback ( make_full_status ) <EOL> return d <EOL> def _start ( self ) : <EOL> d = LBRYFileSaver . _start ( self ) <EOL> d . addCallback ( lambda _ : self . _save_status ( ) ) <EOL> return d <EOL> def _get_finished_deferred_callback_value ( self ) : <EOL> if self . completed is True : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> def _save_status ( self ) : <EOL> if self . completed is True : <EOL> s = ManagedLBRYFileDownloader . STATUS_FINISHED <EOL> elif self . stopped is True : <EOL> s = ManagedLBRYFileDownloader . STATUS_STOPPED <EOL> else : <EOL> s = ManagedLBRYFileDownloader . STATUS_RUNNING <EOL> return self . lbry_file_manager . change_lbry_file_status ( self , s ) <EOL> def _get_progress_manager ( self , download_manager ) : <EOL> return FullStreamProgressManager ( self . _finished_downloading , self . blob_manager , download_manager ) <EOL> class ManagedLBRYFileDownloaderFactory ( object ) : <EOL> implements ( IStreamDownloaderFactory ) <EOL> def __init__ ( self , lbry_file_manager ) : <EOL> self . lbry_file_manager = lbry_file_manager <EOL> def can_download ( self , sd_validator ) : <EOL> return True <EOL> def make_downloader ( self , metadata , options , payment_rate_manager , download_directory = None ) : <EOL> data_rate = options [ <NUM_LIT:0> ] <EOL> upload_allowed = options [ <NUM_LIT:1> ] <EOL> def save_source_if_blob ( stream_hash ) : <EOL> if metadata . metadata_source == StreamMetadata . FROM_BLOB : <EOL> d = self . lbry_file_manager . stream_info_manager . save_sd_blob_hash_to_stream ( stream_hash , <EOL> metadata . source_blob_hash ) <EOL> else : <EOL> d = defer . succeed ( True ) <EOL> d . addCallback ( lambda _ : stream_hash ) <EOL> return d <EOL> d = save_sd_info ( self . lbry_file_manager . stream_info_manager , metadata . validator . raw_info ) <EOL> d . addCallback ( save_source_if_blob ) <EOL> d . addCallback ( lambda stream_hash : self . lbry_file_manager . add_lbry_file ( stream_hash , <EOL> payment_rate_manager , <EOL> data_rate , <EOL> upload_allowed , <EOL> download_directory = download_directory ) ) <EOL> return d <EOL> @ staticmethod <EOL> def get_description ( ) : <EOL> return "<STR_LIT>" </s>
