Refactor code

This commit is contained in:
MrS0m30n3 2014-04-21 16:17:56 +03:00
parent d7235497ca
commit 77acafc6dd
4 changed files with 35 additions and 46 deletions

View File

@ -187,8 +187,8 @@ class YoutubeDL(object):
self._ies = []
self._ies_instances = {}
self._pps = []
self._stop = False
self._pause = False
self._stop_state = False
self._pause_state = False
self._progress_hooks = []
self._download_retcode = 0
self._num_downloads = 0
@ -984,8 +984,8 @@ class YoutubeDL(object):
for ph in self._progress_hooks:
fd.add_progress_hook(ph)
# Add stop, pause handlers
fd.add_stop_handler(self._stop_handler)
fd.add_pause_handler(self._pause_handler)
fd.set_stop_handler(self.get_stop_state)
fd.set_pause_handler(self.get_pause_state)
return fd.download(name, info)
if info_dict.get('requested_formats') is not None:
downloaded = []
@ -1019,7 +1019,7 @@ class YoutubeDL(object):
except (ContentTooShortError, ) as err:
self.report_error('content too short (expected %s bytes and served %s)' % (err.expected, err.downloaded))
return
except StopDownloads:
except StopDownloads:
return
if success:
@ -1031,27 +1031,17 @@ class YoutubeDL(object):
self.record_download_archive(info_dict)
def _stop_handler(self):
""" Return self._stop status """
return self._stop
def _pause_handler(self):
""" Return self._pause status """
return self._pause
def get_stop_state(self):
return self._stop_state
def stop(self):
""" Stop downloads """
if self._stop:
self._stop = False
else:
self._stop = True
def pause(self):
""" Pause/Resume downloads """
if self._pause:
self._pause = False
else:
self._pause = True
def get_pause_state(self):
return self._pause_state
def set_stop_state(self, stop_state):
self._stop_state = stop_state
def set_pause_state(self, pause_state):
self._pause_state = pause_state
def download(self, url_list):
"""Download a given list of URLs."""

View File

@ -301,27 +301,24 @@ class FileDownloader(object):
for ph in self._progress_hooks:
ph(status)
def _stop(self):
""" Check stop handler """
def _check_stop_handler(self):
if self._stop_handler is not None:
if self._stop_handler():
raise StopDownloads()
if self._stop_handler():
raise StopDownloads()
def _pause(self):
""" Check pause/resume handler """
def _check_pause_handler(self):
if self._pause_handler is not None:
while self._pause_handler():
# Break if stop handler enable
if self._stop_handler(): break
time.sleep(1)
while self._pause_handler():
if self._stop_handler(): break
time.sleep(1)
def add_stop_handler(self, shand):
""" shand gets checked. If True raise StopDownloads """
self._stop_handler = shand
def set_stop_handler(self, stop_handler):
""" stop_handler gets checked. If True raise StopDownloads """
self._stop_handler = stop_handler
def add_pause_handler(self, phand):
""" phand gets checked. If True pause downloads """
self._pause_handler = phand
def set_pause_handler(self, pause_handler):
""" pause_handler gets checked. If True pause downloads """
self._pause_handler = pause_handler
def add_progress_hook(self, ph):
""" ph gets called on download progress, with a dictionary with the entries

View File

@ -162,11 +162,9 @@ class HttpFD(FileDownloader):
'speed': speed,
})
# Check stop handler
self._stop()
self._check_stop_handler()
# Check pause handler
self._pause()
self._check_pause_handler()
# Apply rate limit
self.slow_down(start, byte_counter - resume_len)

View File

@ -656,7 +656,11 @@ class MaxDownloadsReached(Exception):
pass
class StopDownloads(Exception):
""" Stop Downloads """
""" Stop Downloads exception.
This exception will be raised by FileDownloader objects when
YoutubDL._stop_state is True
"""
pass
class UnavailableVideoError(Exception):