mirror of
https://github.com/codex-storage/deluge.git
synced 2025-01-30 05:06:50 +00:00
refactor torrent.set_options
This commit is contained in:
parent
1d68579b57
commit
a9ed6fe46a
@ -603,7 +603,7 @@ class Core(component.Component):
|
|||||||
def set_torrent_prioritize_first_last(self, torrent_id, value):
|
def set_torrent_prioritize_first_last(self, torrent_id, value):
|
||||||
# Deprecated method, use set_torrent_options instead
|
# Deprecated method, use set_torrent_options instead
|
||||||
"""Sets a higher priority to the first and last pieces"""
|
"""Sets a higher priority to the first and last pieces"""
|
||||||
return self.torrentmanager[torrent_id].set_prioritize_first_last(value)
|
return self.torrentmanager[torrent_id].set_prioritize_first_last_pieces(value)
|
||||||
|
|
||||||
@export
|
@export
|
||||||
def set_torrent_auto_managed(self, torrent_id, value):
|
def set_torrent_auto_managed(self, torrent_id, value):
|
||||||
|
@ -81,6 +81,36 @@ def sanitize_filepath(filepath, folder=False):
|
|||||||
|
|
||||||
|
|
||||||
class TorrentOptions(dict):
|
class TorrentOptions(dict):
|
||||||
|
''' The torrent options
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
max_connections (int): Sets maximum number of connection this torrent will open.
|
||||||
|
This must be at least 2. The default is unlimited (-1).
|
||||||
|
max_upload_slots (int): Sets the maximum number of peers that are
|
||||||
|
unchoked at the same time on this torrent. This defaults to infinite (-1).
|
||||||
|
max_upload_speed (int): Will limit the upload bandwidth used by this torrent to the limit
|
||||||
|
you set. The default is unlimited (-1) but will not exceed global limit.
|
||||||
|
max_download_speed (int): Will limit the download bandwidth used by this torrent to the
|
||||||
|
limit you set.The default is unlimited (-1) but will not exceed global limit.
|
||||||
|
prioritize_first_last_pieces (bool): Prioritize the first and last pieces in the torrent.
|
||||||
|
sequential_download (bool): Download the pieces of the torrent in order.
|
||||||
|
compact_allocation (bool): Use compact allocation instead of full allocation
|
||||||
|
for this torrent's data.
|
||||||
|
download_location (str): The path for the torrent data to be stored while downloading.
|
||||||
|
auto_managed (bool): Set torrent to auto managed mode, i.e. will be started or queued automatically.
|
||||||
|
stop_at_ratio (bool): Stop the torrent when it has reached stop_ratio.
|
||||||
|
stop_ratio (float): The seeding ratio to stop (or remove) the torrent at.
|
||||||
|
remove_at_ratio (bool): Remove the torrent when it has reached the stop_ratio
|
||||||
|
move_completed (bool): Move the torrent when downloading has finished.
|
||||||
|
move_completed_path (str): The path to move torrent to when downloading has finished.
|
||||||
|
add_paused (bool): Add the torrrent in a paused state.
|
||||||
|
shared (bool): Enable the torrent to be seen by other Deluge users.
|
||||||
|
super_seeding (bool): Enable super seeding/initial seeding.
|
||||||
|
priority (int): Torrent bandwidth priority with a range [0..255], 0 is lowest and default priority.
|
||||||
|
file_priorities (list of int): The priority for files in torrent, range is [0..7] however
|
||||||
|
only [0, 1, 5, 7] are normally used and correspond to [Do Not Download, Normal, High, Highest]
|
||||||
|
mapped_files (dict): A mapping of the renamed filenames in 'index:filename' pairs.
|
||||||
|
'''
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
config = ConfigManager("core.conf").config
|
config = ConfigManager("core.conf").config
|
||||||
options_conf_map = {
|
options_conf_map = {
|
||||||
@ -217,40 +247,28 @@ class Torrent(object):
|
|||||||
self.has_metadata = True
|
self.has_metadata = True
|
||||||
self.torrent_info = self.handle.get_torrent_info()
|
self.torrent_info = self.handle.get_torrent_info()
|
||||||
if self.options["prioritize_first_last_pieces"]:
|
if self.options["prioritize_first_last_pieces"]:
|
||||||
self.set_prioritize_first_last(True)
|
self.set_prioritize_first_last_pieces(True)
|
||||||
self.write_torrentfile()
|
self.write_torrentfile()
|
||||||
|
|
||||||
## Options methods ##
|
## Options methods ##
|
||||||
def set_options(self, options):
|
def set_options(self, options):
|
||||||
OPTIONS_FUNCS = {
|
if options is self.options:
|
||||||
# Functions used for setting options
|
options = options.copy()
|
||||||
"auto_managed": self.set_auto_managed,
|
|
||||||
"download_location": self.set_save_path,
|
|
||||||
"file_priorities": self.set_file_priorities,
|
|
||||||
"max_connections": self.handle.set_max_connections,
|
|
||||||
"max_download_speed": self.set_max_download_speed,
|
|
||||||
"max_upload_slots": self.set_max_upload_slots,
|
|
||||||
"max_upload_speed": self.set_max_upload_speed,
|
|
||||||
"prioritize_first_last_pieces": self.set_prioritize_first_last,
|
|
||||||
"sequential_download": self.set_sequential_download,
|
|
||||||
"super_seeding": self.set_super_seeding,
|
|
||||||
"stop_ratio": self.set_stop_ratio,
|
|
||||||
"stop_at_ratio": self.set_stop_at_ratio,
|
|
||||||
"remove_at_ratio": self.set_remove_at_ratio,
|
|
||||||
"move_completed": self.set_move_completed,
|
|
||||||
"move_completed_path": self.set_move_completed_path,
|
|
||||||
"priority": self.set_priority,
|
|
||||||
}
|
|
||||||
|
|
||||||
# set_prioritize_first_last is called by set_file_priorities,
|
# set_prioritize_first_last is called by set_file_priorities so only run if not in options
|
||||||
# so remove if file_priorities is set in options.
|
|
||||||
if "file_priorities" in options:
|
if "file_priorities" in options:
|
||||||
del OPTIONS_FUNCS["prioritize_first_last_pieces"]
|
self.options["prioritize_first_last_pieces"] = options["prioritize_first_last_pieces"]
|
||||||
|
del options["prioritize_first_last_pieces"]
|
||||||
|
|
||||||
for (key, value) in options.items():
|
for key, value in options.items():
|
||||||
if key in OPTIONS_FUNCS:
|
if key in self.options:
|
||||||
OPTIONS_FUNCS[key](value)
|
options_set_func = getattr(self, "set_" + key, None)
|
||||||
self.options.update(options)
|
if options_set_func:
|
||||||
|
options_set_func(value)
|
||||||
|
del options[key]
|
||||||
|
else:
|
||||||
|
# Update config options that do not have funcs
|
||||||
|
self.options.update({key:value})
|
||||||
|
|
||||||
def get_options(self):
|
def get_options(self):
|
||||||
return self.options
|
return self.options
|
||||||
@ -280,6 +298,10 @@ class Torrent(object):
|
|||||||
self.handle.set_download_limit(value)
|
self.handle.set_download_limit(value)
|
||||||
|
|
||||||
def set_prioritize_first_last(self, prioritize):
|
def set_prioritize_first_last(self, prioritize):
|
||||||
|
# Deprecated due to mismatch between option and func name
|
||||||
|
self.set_prioritize_first_last_pieces(prioritize)
|
||||||
|
|
||||||
|
def set_prioritize_first_last_pieces(self, prioritize):
|
||||||
self.options["prioritize_first_last_pieces"] = prioritize
|
self.options["prioritize_first_last_pieces"] = prioritize
|
||||||
if not prioritize:
|
if not prioritize:
|
||||||
# If we are turning off this option, call set_file_priorities to
|
# If we are turning off this option, call set_file_priorities to
|
||||||
@ -289,8 +311,7 @@ class Torrent(object):
|
|||||||
if not self.has_metadata:
|
if not self.has_metadata:
|
||||||
return
|
return
|
||||||
if self.options["compact_allocation"]:
|
if self.options["compact_allocation"]:
|
||||||
log.debug("Setting first/last priority with compact "
|
log.debug("Setting first/last priority with compact allocation does not work!")
|
||||||
"allocation does not work!")
|
|
||||||
return
|
return
|
||||||
# A list of priorities for each piece in the torrent
|
# A list of priorities for each piece in the torrent
|
||||||
priorities = self.handle.piece_priorities()
|
priorities = self.handle.piece_priorities()
|
||||||
@ -384,11 +405,14 @@ class Torrent(object):
|
|||||||
|
|
||||||
# Set the first/last priorities if needed
|
# Set the first/last priorities if needed
|
||||||
if self.options["prioritize_first_last_pieces"]:
|
if self.options["prioritize_first_last_pieces"]:
|
||||||
self.set_prioritize_first_last(self.options["prioritize_first_last_pieces"])
|
self.set_prioritize_first_last_pieces(self.options["prioritize_first_last_pieces"])
|
||||||
|
|
||||||
def set_save_path(self, save_path):
|
def set_save_path(self, download_location):
|
||||||
self.options["download_location"] = save_path
|
# Deprecated, use download_location
|
||||||
|
self.set_download_location(download_location)
|
||||||
|
|
||||||
|
def set_download_location(self, download_location):
|
||||||
|
self.options["download_location"] = download_location
|
||||||
|
|
||||||
def set_priority(self, priority):
|
def set_priority(self, priority):
|
||||||
"""
|
"""
|
||||||
@ -744,7 +768,8 @@ class Torrent(object):
|
|||||||
"progress": lambda: self.status.progress * 100,
|
"progress": lambda: self.status.progress * 100,
|
||||||
"shared": lambda: self.options["shared"],
|
"shared": lambda: self.options["shared"],
|
||||||
"remove_at_ratio": lambda: self.options["remove_at_ratio"],
|
"remove_at_ratio": lambda: self.options["remove_at_ratio"],
|
||||||
"save_path": lambda: self.options["download_location"],
|
"save_path": lambda: self.options["download_location"], # Deprecated, use download_location
|
||||||
|
"download_location": lambda: self.options["download_location"],
|
||||||
"seeding_time": lambda: self.status.seeding_time,
|
"seeding_time": lambda: self.status.seeding_time,
|
||||||
"seeds_peers_ratio": lambda: -1.0 if self.status.num_incomplete == 0 else
|
"seeds_peers_ratio": lambda: -1.0 if self.status.num_incomplete == 0 else
|
||||||
self.status.num_complete / float(self.status.num_incomplete), # Use -1.0 to signify infinity
|
self.status.num_complete / float(self.status.num_incomplete), # Use -1.0 to signify infinity
|
||||||
@ -1026,9 +1051,9 @@ class Torrent(object):
|
|||||||
Cleans up after libtorrent folder renames.
|
Cleans up after libtorrent folder renames.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
info = self.get_status(['save_path'])
|
info = self.get_status(['download_location'])
|
||||||
# Regex removes leading slashes that causes join function to ignore save_path
|
# Regex removes leading slashes that causes join function to ignore download_location
|
||||||
folder_full_path = os.path.join(info['save_path'], re.sub("^/*", "", folder))
|
folder_full_path = os.path.join(info['download_location'], re.sub("^/*", "", folder))
|
||||||
folder_full_path = os.path.normpath(folder_full_path)
|
folder_full_path = os.path.normpath(folder_full_path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -1034,7 +1034,7 @@ class TorrentManager(component.Component):
|
|||||||
torrent = self.torrents[str(alert.handle.info_hash())]
|
torrent = self.torrents[str(alert.handle.info_hash())]
|
||||||
except (RuntimeError, KeyError):
|
except (RuntimeError, KeyError):
|
||||||
return
|
return
|
||||||
torrent.set_save_path(os.path.normpath(alert.handle.save_path()))
|
torrent.set_download_location(os.path.normpath(alert.handle.save_path()))
|
||||||
torrent.set_move_completed(False)
|
torrent.set_move_completed(False)
|
||||||
|
|
||||||
def on_alert_storage_moved_failed(self, alert):
|
def on_alert_storage_moved_failed(self, alert):
|
||||||
|
@ -218,7 +218,7 @@ class Core(CorePluginBase):
|
|||||||
torrent.set_max_upload_speed(options["max_upload_speed"])
|
torrent.set_max_upload_speed(options["max_upload_speed"])
|
||||||
torrent.set_max_connections(options["max_connections"])
|
torrent.set_max_connections(options["max_connections"])
|
||||||
torrent.set_max_upload_slots(options["max_upload_slots"])
|
torrent.set_max_upload_slots(options["max_upload_slots"])
|
||||||
torrent.set_prioritize_first_last(options["prioritize_first_last"])
|
torrent.set_prioritize_first_last_pieces(options["prioritize_first_last"])
|
||||||
|
|
||||||
if options["apply_queue"]:
|
if options["apply_queue"]:
|
||||||
torrent.set_auto_managed(options['is_auto_managed'])
|
torrent.set_auto_managed(options['is_auto_managed'])
|
||||||
@ -243,7 +243,7 @@ class Core(CorePluginBase):
|
|||||||
torrent.set_max_upload_speed(self.core_cfg.config["max_upload_speed_per_torrent"])
|
torrent.set_max_upload_speed(self.core_cfg.config["max_upload_speed_per_torrent"])
|
||||||
torrent.set_max_connections(self.core_cfg.config["max_connections_per_torrent"])
|
torrent.set_max_connections(self.core_cfg.config["max_connections_per_torrent"])
|
||||||
torrent.set_max_upload_slots(self.core_cfg.config["max_upload_slots_per_torrent"])
|
torrent.set_max_upload_slots(self.core_cfg.config["max_upload_slots_per_torrent"])
|
||||||
torrent.set_prioritize_first_last(self.core_cfg.config["prioritize_first_last_pieces"])
|
torrent.set_prioritize_first_last_pieces(self.core_cfg.config["prioritize_first_last_pieces"])
|
||||||
|
|
||||||
if options["apply_queue"]:
|
if options["apply_queue"]:
|
||||||
torrent.set_auto_managed(self.core_cfg.config['auto_managed'])
|
torrent.set_auto_managed(self.core_cfg.config['auto_managed'])
|
||||||
|
@ -78,19 +78,19 @@ class TorrentTestCase(unittest.TestCase):
|
|||||||
atp["duplicate_is_error"] = True
|
atp["duplicate_is_error"] = True
|
||||||
return atp
|
return atp
|
||||||
|
|
||||||
def test_set_prioritize_first_last(self):
|
def test_set_prioritize_first_last_pieces(self):
|
||||||
piece_indexes = [(0, 1), (0, 1), (0, 1), (0, 1), (0, 2), (50, 52),
|
piece_indexes = [(0, 1), (0, 1), (0, 1), (0, 1), (0, 2), (50, 52),
|
||||||
(51, 53), (110, 112), (111, 114), (200, 203),
|
(51, 53), (110, 112), (111, 114), (200, 203),
|
||||||
(202, 203), (212, 213), (212, 218), (457, 463)]
|
(202, 203), (212, 213), (212, 218), (457, 463)]
|
||||||
self.run_test_set_prioritize_first_last("dir_with_6_files.torrent", piece_indexes)
|
self.run_test_set_prioritize_first_last_pieces("dir_with_6_files.torrent", piece_indexes)
|
||||||
|
|
||||||
def run_test_set_prioritize_first_last(self, torrent_file, prioritized_piece_indexes):
|
def run_test_set_prioritize_first_last_pieces(self, torrent_file, prioritized_piece_indexes):
|
||||||
atp = self.get_torrent_atp(torrent_file)
|
atp = self.get_torrent_atp(torrent_file)
|
||||||
handle = self.session.add_torrent(atp)
|
handle = self.session.add_torrent(atp)
|
||||||
|
|
||||||
self.torrent = Torrent(handle, {})
|
self.torrent = Torrent(handle, {})
|
||||||
priorities_original = handle.piece_priorities()
|
priorities_original = handle.piece_priorities()
|
||||||
prioritized_pieces, new_priorites = self.torrent.set_prioritize_first_last(True)
|
prioritized_pieces, new_priorites = self.torrent.set_prioritize_first_last_pieces(True)
|
||||||
priorities = handle.piece_priorities()
|
priorities = handle.piece_priorities()
|
||||||
non_prioritized_pieces = list(range(len(priorities)))
|
non_prioritized_pieces = list(range(len(priorities)))
|
||||||
|
|
||||||
@ -113,14 +113,14 @@ class TorrentTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
#self.print_priority_list(priorities)
|
#self.print_priority_list(priorities)
|
||||||
|
|
||||||
def test_set_prioritize_first_last_false(self):
|
def test_set_prioritize_first_last_pieces_false(self):
|
||||||
atp = self.get_torrent_atp("dir_with_6_files.torrent")
|
atp = self.get_torrent_atp("dir_with_6_files.torrent")
|
||||||
handle = self.session.add_torrent(atp)
|
handle = self.session.add_torrent(atp)
|
||||||
self.torrent = Torrent(handle, {})
|
self.torrent = Torrent(handle, {})
|
||||||
# First set some pieces prioritized
|
# First set some pieces prioritized
|
||||||
self.torrent.set_prioritize_first_last(True)
|
self.torrent.set_prioritize_first_last_pieces(True)
|
||||||
# Reset pirorities
|
# Reset pirorities
|
||||||
self.torrent.set_prioritize_first_last(False)
|
self.torrent.set_prioritize_first_last_pieces(False)
|
||||||
priorities = handle.piece_priorities()
|
priorities = handle.piece_priorities()
|
||||||
|
|
||||||
# Test the priority of the prioritized pieces
|
# Test the priority of the prioritized pieces
|
||||||
|
Loading…
x
Reference in New Issue
Block a user