[Core] Refactor of priorities code and test

This commit is contained in:
Calum Lind 2016-11-30 18:07:14 +00:00
parent 425af00ebf
commit c5e623ae45
2 changed files with 37 additions and 43 deletions

View File

@ -232,6 +232,7 @@ class Torrent(object):
try:
self.torrent_info = self.handle.get_torrent_info()
except RuntimeError:
# Deprecated in libtorrent 1.1 (i.e. exception check is not needed)
self.torrent_info = None
self.has_metadata = self.status.has_metadata
@ -372,42 +373,44 @@ class Torrent(object):
Args:
prioritize (bool): Prioritize the first and last pieces.
Returns:
tuple of lists: The prioritized pieces and the torrent piece priorities.
"""
if not self.has_metadata:
return
self.options['prioritize_first_last_pieces'] = prioritize
if not prioritize:
# If we are turning off this option, call set_file_priorities to
# reset all the piece priorities
self.set_file_priorities(self.options['file_priorities'])
return None, None
if not self.has_metadata:
return None, None
return
# A list of priorities for each piece in the torrent
priorities = self.handle.piece_priorities()
prioritized_pieces = []
t_info = self.torrent_info
for i in range(t_info.num_files()):
_file = t_info.file_at(i)
two_percent_bytes = int(0.02 * _file.size)
def get_file_piece(idx, byte_offset):
return self.torrent_info.map_file(idx, byte_offset, 0).piece
for idx in range(self.torrent_info.num_files()):
try:
file_size = self.torrent_info.files().file_size(idx)
except AttributeError:
# Deprecated in libtorrent 1.1
file_size = self.torrent_info.file_at(idx).size
two_percent_bytes = int(0.02 * file_size)
# Get the pieces for the byte offsets
first_start = t_info.map_file(i, 0, 0).piece
first_end = t_info.map_file(i, two_percent_bytes, 0).piece
last_start = t_info.map_file(i, _file.size - two_percent_bytes, 0).piece
last_end = t_info.map_file(i, max(_file.size - 1, 0), 0).piece
first_start = get_file_piece(idx, 0)
first_end = get_file_piece(idx, two_percent_bytes) + 1
last_start = get_file_piece(idx, file_size - two_percent_bytes)
last_end = get_file_piece(idx, max(file_size - 1, 0)) + 1
first_end += 1
last_end += 1
prioritized_pieces.append((first_start, first_end))
prioritized_pieces.append((last_start, last_end))
# Set the pieces in our first and last ranges to priority 7
# Set the pieces in first and last ranges to priority 7
# if they are not marked as do not download
priorities[first_start:first_end] = [p and 7 for p in priorities[first_start:first_end]]
priorities[last_start:last_end] = [p and 7 for p in priorities[last_start:last_end]]
# Setting the priorites for all the pieces of this torrent
self.handle.prioritize_pieces(priorities)
return prioritized_pieces, priorities
def set_sequential_download(self, set_sequencial):
"""Sets whether to download the pieces of the torrent in order.
@ -830,10 +833,10 @@ class Torrent(object):
"""Calculates the file progress as a percentage.
Returns:
list of floats: The file progress (0.0 -> 1.0)
list of floats: The file progress (0.0 -> 1.0), empty list if n/a.
"""
if not self.has_metadata:
return 0.0
return []
return [progress / _file.size if _file.size else 0.0 for progress, _file in
zip(self.handle.file_progress(), self.torrent_info.files())]

View File

@ -64,9 +64,8 @@ class TorrentTestCase(BaseTestCase):
return atp
def test_set_prioritize_first_last_pieces(self):
piece_indexes = [(0, 1), (0, 1), (0, 1), (0, 1), (0, 2), (50, 52),
(51, 53), (110, 112), (111, 114), (200, 203),
(202, 203), (212, 213), (212, 218), (457, 463)]
piece_indexes = [0, 1, 50, 51, 52, 110, 111, 112, 113, 200, 201, 202, 212,
213, 214, 215, 216, 217, 457, 458, 459, 460, 461, 462]
self.run_test_set_prioritize_first_last_pieces('dir_with_6_files.torrent', piece_indexes)
def run_test_set_prioritize_first_last_pieces(self, torrent_file, prioritized_piece_indexes):
@ -75,26 +74,18 @@ class TorrentTestCase(BaseTestCase):
self.torrent = Torrent(handle, {})
priorities_original = handle.piece_priorities()
prioritized_pieces, new_priorites = self.torrent.set_prioritize_first_last_pieces(True)
self.torrent.set_prioritize_first_last_pieces(True)
priorities = handle.piece_priorities()
non_prioritized_pieces = list(range(len(priorities)))
# The prioritized indexes are the same as we expect
self.assertEquals(prioritized_pieces, prioritized_piece_indexes)
# Test the priority of the prioritized pieces
for first, last in prioritized_pieces:
for i in range(first, last):
if i in non_prioritized_pieces:
non_prioritized_pieces.remove(i)
self.assertEquals(priorities[i], 7)
# Test the priority of all the non-prioritized pieces
for i in non_prioritized_pieces:
self.assertEquals(priorities[i], 1)
# The length of the list of new priorites is the same as the original
self.assertEquals(len(priorities_original), len(new_priorites))
self.assertEquals(len(priorities_original), len(priorities))
# Test the priority of all the pieces against the calculated indexes.
for idx, priority in enumerate(priorities):
if idx in prioritized_piece_indexes:
self.assertEquals(priorities[idx], 7)
else:
self.assertEquals(priorities[idx], 1)
# self.print_priority_list(priorities)