[Core] Refactor TM register of alert and set funcs

This commit is contained in:
Calum Lind 2016-11-04 00:18:05 +00:00
parent c5e623ae45
commit 59b01b363c
1 changed files with 21 additions and 31 deletions

View File

@ -134,38 +134,28 @@ class TorrentManager(component.Component):
self.last_state_update_alert_ts = 0 self.last_state_update_alert_ts = 0
# Register set functions # Register set functions
self.config.register_set_function('max_connections_per_torrent', set_config_keys = ['max_connections_per_torrent', 'max_upload_slots_per_torrent',
self.on_set_max_connections_per_torrent) 'max_upload_speed_per_torrent', 'max_download_speed_per_torrent']
self.config.register_set_function('max_upload_slots_per_torrent',
self.on_set_max_upload_slots_per_torrent) for config_key in set_config_keys:
self.config.register_set_function('max_upload_speed_per_torrent', on_set_func = getattr(self, ''.join(['on_set_', config_key]))
self.on_set_max_upload_speed_per_torrent) self.config.register_set_function(config_key, on_set_func)
self.config.register_set_function('max_download_speed_per_torrent',
self.on_set_max_download_speed_per_torrent)
# Register alert functions # Register alert functions
self.alerts.register_handler('torrent_finished_alert', self.on_alert_torrent_finished) alert_handles = [
self.alerts.register_handler('torrent_paused_alert', self.on_alert_torrent_paused) 'external_ip_alert', 'performance_alert', 'add_torrent_alert',
self.alerts.register_handler('torrent_checked_alert', self.on_alert_torrent_checked) 'metadata_received_alert', 'torrent_finished_alert', 'torrent_paused_alert',
self.alerts.register_handler('tracker_reply_alert', self.on_alert_tracker_reply) 'torrent_checked_alert', 'torrent_resumed_alert', 'tracker_reply_alert',
self.alerts.register_handler('tracker_announce_alert', self.on_alert_tracker_announce) 'tracker_announce_alert', 'tracker_warning_alert', 'tracker_error_alert',
self.alerts.register_handler('tracker_warning_alert', self.on_alert_tracker_warning) 'file_renamed_alert', 'file_error_alert', 'file_completed_alert',
self.alerts.register_handler('tracker_error_alert', self.on_alert_tracker_error) 'storage_moved_alert', 'storage_moved_failed_alert', 'state_update_alert',
self.alerts.register_handler('storage_moved_alert', self.on_alert_storage_moved) 'state_changed_alert', 'save_resume_data_alert', 'save_resume_data_failed_alert',
self.alerts.register_handler('storage_moved_failed_alert', self.on_alert_storage_moved_failed) 'fastresume_rejected_alert'
self.alerts.register_handler('torrent_resumed_alert', self.on_alert_torrent_resumed) ]
self.alerts.register_handler('state_changed_alert', self.on_alert_state_changed)
self.alerts.register_handler('save_resume_data_alert', self.on_alert_save_resume_data) for alert_handle in alert_handles:
self.alerts.register_handler('save_resume_data_failed_alert', self.on_alert_save_resume_data_failed) on_alert_func = getattr(self, ''.join(['on_alert_', alert_handle.replace('_alert', '')]))
self.alerts.register_handler('file_renamed_alert', self.on_alert_file_renamed) self.alerts.register_handler(alert_handle, on_alert_func)
self.alerts.register_handler('metadata_received_alert', self.on_alert_metadata_received)
self.alerts.register_handler('file_error_alert', self.on_alert_file_error)
self.alerts.register_handler('file_completed_alert', self.on_alert_file_completed)
self.alerts.register_handler('state_update_alert', self.on_alert_state_update)
self.alerts.register_handler('external_ip_alert', self.on_alert_external_ip)
self.alerts.register_handler('performance_alert', self.on_alert_performance)
self.alerts.register_handler('fastresume_rejected_alert', self.on_alert_fastresume_rejected)
self.alerts.register_handler('add_torrent_alert', self.on_add_torrent_alert)
# Define timers # Define timers
self.save_state_timer = LoopingCall(self.save_state) self.save_state_timer = LoopingCall(self.save_state)
@ -407,7 +397,7 @@ class TorrentManager(component.Component):
raise AddTorrentError('Unable to add torrent to session: %s' % ex) raise AddTorrentError('Unable to add torrent to session: %s' % ex)
return d return d
def on_add_torrent_alert(self, alert): def on_alert_add_torrent(self, alert):
"""Alert handler for libtorrent add_torrent_alert""" """Alert handler for libtorrent add_torrent_alert"""
if not alert.handle.is_valid(): if not alert.handle.is_valid():
log.warn('Torrent handle is invalid!') log.warn('Torrent handle is invalid!')