Browse Source

Change DownloadThread signals

doc-issue-template
MrS0m30n3 10 years ago
parent
commit
bddccee3a9
3 changed files with 60 additions and 49 deletions
  1. 55
      youtube_dl_gui/DownloadObject.py
  2. 43
      youtube_dl_gui/DownloadThread.py
  3. 11
      youtube_dl_gui/YoutubeDLGUI.py

55
youtube_dl_gui/DownloadObject.py

@ -24,13 +24,11 @@ class DownloadObject(object):
download()
Params: URL to download
Options list e.g. ['--help']
Return: See downlaoad() return codes
Return-Codes:
OK : 'Url downloaded successfully'
ERROR : 'Error occured while downloading'
ALREADY: 'Url is already downloaded'
Return: DownlaodObject.OK
DownloadObject.ERROR
DownloadObject.STOPPED
DownloadObject.ALREADY
stop()
Params: None
@ -39,15 +37,21 @@ class DownloadObject(object):
instance has downloaded.
Data_hook Keys
See self._init_data().
'playlist_index',
'playlist_size',
'filesize',
'filename',
'percent',
'status',
'speed',
'eta'
'''
# download() return codes
OK = 0
ERROR = 1
ALREADY = -1
STDERR_IGNORE = '' # Default filter for our self._log() method
STOPPED = 2
ALREADY = 3
def __init__(self, youtubedl_path, data_hook=None, logger=None):
self.youtubedl_path = youtubedl_path
@ -84,15 +88,13 @@ class DownloadObject(object):
stdout, stderr = self._read()
data = extract_data(stdout)
synced = self._sync_data(data)
updated = self._update_data(data)
if stderr != '':
self._return_code = self.ERROR
if self.logger is not None:
self._log(stderr)
if self.data_hook is not None and synced:
if updated:
self._hook_data()
return self._return_code
@ -100,12 +102,14 @@ class DownloadObject(object):
def stop(self):
if self._proc is not None:
self._proc.kill()
self._return_code = self.STOPPED
def _sync_data(self, data):
''' Sync data between extract_data() dictionary and self._data.
Return True if synced else return False.
def _update_data(self, data):
''' Update self._data from data.
Return True if updated else return False.
'''
synced = False
updated = False
for key in data:
if key == 'filename':
# Save full file path on files_list
@ -117,22 +121,25 @@ class DownloadObject(object):
# Set self._return_code to already downloaded
if data[key] == 'already_downloaded':
self._return_code = self.ALREADY
# Trash that key
data[key] = None
self._data[key] = data[key]
synced = True
updated = True
return synced
return updated
def _add_on_files_list(self, filename):
self.files_list.append(filename)
def _log(self, data):
if data != self.STDERR_IGNORE:
if self.logger is not None:
self.logger.log(data)
def _hook_data(self):
''' Pass self._data back to data_hook. '''
self.data_hook(self._data)
if self.data_hook is not None:
self.data_hook(self._data)
def _proc_is_alive(self):
''' Return True if self._proc is alive. '''
@ -218,6 +225,7 @@ def extract_data(stdout):
elif '%' in stdout[0]:
if stdout[0] == '100%':
data_dictionary['speed'] = ''
data_dictionary['eta'] = ''
else:
data_dictionary['percent'] = stdout[0]
data_dictionary['filesize'] = stdout[2]
@ -240,3 +248,4 @@ def extract_data(stdout):
data_dictionary['status'] = 'pre_process'
return data_dictionary

43
youtube_dl_gui/DownloadThread.py

@ -28,8 +28,8 @@ class DownloadManager(Thread):
self.download_list = download_list
self.opt_manager = opt_manager
self.log_manager = log_manager
self.stopped = False
self._threads_lst = []
self._stopped = False
self._running = True
self._kill = False
self.start()
@ -51,8 +51,12 @@ class DownloadManager(Thread):
sleep(0.1)
self._terminate_all()
if not self._kill:
self._callafter('finish')
if self._stopped:
self._callafter('closed')
else:
self._callafter('finished')
def downloading(self):
''' Return True if at least one download thread is alive '''
@ -66,10 +70,10 @@ class DownloadManager(Thread):
self.download_list.append(item)
def close(self, kill=False):
self._callafter('close')
self._callafter('closing')
self._running = False
self._stopped = True
self._kill = kill
self.stopped = True
def _download(self, url, index):
''' Download given url '''
@ -139,18 +143,18 @@ class DownloadThread(Thread):
if self.opt_manager.options['clear_dash_files']:
self._clear_dash()
if return_code == self._dl_object.OK:
self._callafter(self._get_status_pack('Finished'))
elif return_code == self._dl_object.ERROR:
self._callafter(self._get_status_pack('Error'))
elif return_code == self._dl_object.ALREADY:
self._callafter(self._get_status_pack('Already-Downloaded'))
if return_code == DownloadObject.OK:
self._callafter({'status': 'Finished'})
elif return_code == DownloadObject.ERROR:
self._callafter({'status': 'Error', 'speed': '', 'eta': ''})
elif return_code == DownloadObject.STOPPED:
self._callafter({'status': 'Stopped', 'speed': '', 'eta': ''})
elif return_code == DownloadObject.ALREADY:
self._callafter({'status': 'Already-Downloaded'})
def close(self):
if self._dl_object is not None:
self._callafter(self._get_status_pack('Stopping'))
self._callafter({'status': 'Stopping'})
self._dl_object.stop()
def _clear_dash(self):
@ -160,21 +164,17 @@ class DownloadThread(Thread):
remove_file(fl)
def _data_hook(self, data):
''' Add download index and send data back to caller '''
data['index'] = self.index
''' Extract process status and call CallAfter '''
data['status'] = self._get_status(data)
self._callafter(data)
def _callafter(self, data):
''' Add self.index on data and send data back to caller '''
data['index'] = self.index
CallAfter(Publisher.sendMessage, self.PUBLISHER_TOPIC, data)
def _get_status_pack(self, message):
''' Return simple status pack '''
data = {'index': self.index, 'status': message}
return data
def _get_status(self, data):
''' Return download process status '''
''' Return download process status from data['status'] '''
if data['playlist_index'] is not None:
playlist_info = '%s/%s' % (data['playlist_index'], data['playlist_size'])
else:
@ -196,3 +196,4 @@ class DownloadThread(Thread):
path = self.opt_manager.options['youtubedl_path']
path = fix_path(path) + get_youtubedl_filename()
return path

11
youtube_dl_gui/YoutubeDLGUI.py

@ -194,14 +194,15 @@ class MainFrame(wx.Frame):
self.status_list.write(data)
if topic == 'download_manager':
if data == 'close':
if data == 'closing':
self.status_bar_write('Stopping downloads')
if data == 'finish':
if data == 'closed':
self.status_bar_write('Downloads stopped')
self.reset()
if data == 'finished':
self.status_bar_write('Done')
stopped = self.download_thread.stopped
self.reset()
if not stopped:
self.fin_tasks()
self.fin_tasks()
def update_handler(self, msg):
if msg.data == 'finish':

Loading…
Cancel
Save