Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send only updated/changed torrents to the client #6

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DEPENDS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* geoip-database (optional)
* setproctitle (optional)
* pillow (optional)
* pydblite
* rencode >= 1.0.2 (optional), python port bundled.

=== Gtk UI ===
Expand Down
40 changes: 18 additions & 22 deletions deluge/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ def add_torrent_magnet(self, uri, options):

"""
log.debug('Attempting to add by magnet uri: %s', uri)

return self.torrentmanager.add(magnet=uri, options=options)

@export
Expand Down Expand Up @@ -491,12 +490,10 @@ def resume_torrent(self, torrent_ids):
for torrent_id in torrent_ids:
self.torrentmanager[torrent_id].resume()

def create_torrent_status(self, torrent_id, torrent_keys, plugin_keys, diff=False, update=False, all_keys=False):
def create_torrent_status(self, torrent_id, torrent_keys, plugin_keys, update=False, all_keys=False):
try:
status = self.torrentmanager[torrent_id].get_status(torrent_keys, diff, update=update, all_keys=all_keys)
status = self.torrentmanager[torrent_id].get_status(torrent_keys, update=update, all_keys=all_keys)
except KeyError:
import traceback
traceback.print_exc()
# Torrent was probaly removed meanwhile
return {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be just removed then?


Expand All @@ -506,18 +503,25 @@ def create_torrent_status(self, torrent_id, torrent_keys, plugin_keys, diff=Fals
return status

@export
def get_torrent_status(self, torrent_id, keys, diff=False):
torrent_keys, plugin_keys = self.torrentmanager.separate_keys(keys, [torrent_id])
return self.create_torrent_status(torrent_id, torrent_keys, plugin_keys, diff=diff, update=True,
all_keys=not keys)
def get_torrent_status(self, torrent_id, keys):
torrent_keys, plugin_keys = self.torrentmanager.separate_torrent_keys(keys, [torrent_id])
return self.create_torrent_status(torrent_id, torrent_keys, plugin_keys,
update=True, all_keys=not keys)

@export
def get_torrents_status(self, filter_dict, keys, diff=False):
"""
returns all torrents , optionally filtered by filter_dict.
def get_torrents_status(self, keys, only_updated=False):
"""Get the status for all the torrents

Args:
keys (list): the keys we want retrived
only_updated (bool): If only the torrents that have been updated since last call should be returned

Returns:
dict: The status for the torrents

"""
torrent_ids = self.filtermanager.filter_torrent_ids(filter_dict)
d = self.torrentmanager.torrents_status_update(torrent_ids, keys, diff=diff)
torrent_ids = self.filtermanager.get_torrent_list()
d = self.torrentmanager.torrents_status_update(torrent_ids, keys, only_updated=only_updated)

def add_plugin_fields(args):
status_dict, plugin_keys = args
Expand All @@ -529,14 +533,6 @@ def add_plugin_fields(args):
d.addCallback(add_plugin_fields)
return d

@export
def get_filter_tree(self, show_zero_hits=True, hide_cat=None):
"""
returns {field: [(value,count)] }
for use in sidebar(s)
"""
return self.filtermanager.get_filter_tree(show_zero_hits, hide_cat)

@export
def get_session_state(self):
"""Returns a list of torrent_ids in the session."""
Expand Down
64 changes: 49 additions & 15 deletions deluge/core/eventmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,42 @@

import logging

from twisted.internet import reactor

import deluge.component as component

log = logging.getLogger(__name__)


class EventManager(component.Component):
class EventManagerBase(object):
def __init__(self):
component.Component.__init__(self, 'EventManager')
self.handlers = {}
self.event_handlers = {}

def emit(self, event):
"""
Emits the event to interested clients.

:param event: DelugeEvent
"""
# Emit the event to the interested clients
component.get('RPCServer').emit_event(event)
# Call any handlers for the event
if event.name in self.handlers:
for handler in self.handlers[event.name]:
self.pre_emit(event)
self.handle_event(event)

def handle_event(self, event):
# Call any event_handlers for the event
if event.name in self.event_handlers:
for handler in self.event_handlers[event.name]:
# log.debug("Running handler %s for event %s with args: %s", event.name, handler, event.args)
try:
handler(*event.args)
self.call_handler(handler, event.args)
except Exception as ex:
log.error('Event handler %s failed in %s with exception %s', event.name, handler, ex)

def pre_emit(self, event):
pass

def call_handler(self, handler, args):
handler(*args)

def register_event_handler(self, event, handler):
"""
Registers a function to be called when a `:param:event` is emitted.
Expand All @@ -44,11 +53,11 @@ def register_event_handler(self, event, handler):
:param handler: function, to be called when `:param:event` is emitted

"""
if event not in self.handlers:
self.handlers[event] = []
if event not in self.event_handlers:
self.event_handlers[event] = []

if handler not in self.handlers[event]:
self.handlers[event].append(handler)
if handler not in self.event_handlers[event]:
self.event_handlers[event].append(handler)

def deregister_event_handler(self, event, handler):
"""
Expand All @@ -58,5 +67,30 @@ def deregister_event_handler(self, event, handler):
:param handler: function, currently registered to handle `:param:event`

"""
if event in self.handlers and handler in self.handlers[event]:
self.handlers[event].remove(handler)
if event in self.event_handlers and handler in self.event_handlers[event]:
self.event_handlers[event].remove(handler)

def has_handler(self, event):
return event in self.event_handlers


class EventManager(EventManagerBase, component.Component):
def __init__(self):
EventManagerBase.__init__(self)
component.Component.__init__(self, 'EventManager')

def pre_emit(self, event):
component.get('RPCServer').emit_event(event)


class EventManagerClient(EventManagerBase):
def __init__(self):
super(EventManagerClient, self).__init__()

def copy(self):
cp = EventManagerClient()
cp.event_handlers = dict(self.event_handlers)
return cp

def call_handler(self, handler, args):
reactor.callLater(0, handler, *args)
Loading