a bit of cleanup

This commit is contained in:
Anders Jensen
2017-12-17 19:38:53 +01:00
parent 84dd3c4fe7
commit e6948fa90f
6 changed files with 45 additions and 40 deletions

View File

@@ -39,18 +39,21 @@
from deluge.plugins.init import PluginInitBase
class CorePlugin(PluginInitBase):
def __init__(self, plugin_name):
from core import Core as _plugin_cls
self._plugin_cls = _plugin_cls
super(CorePlugin, self).__init__(plugin_name)
class GtkUIPlugin(PluginInitBase):
def __init__(self, plugin_name):
from gtkui import GtkUI as _plugin_cls
self._plugin_cls = _plugin_cls
super(GtkUIPlugin, self).__init__(plugin_name)
class WebUIPlugin(PluginInitBase):
def __init__(self, plugin_name):
from webui import WebUI as _plugin_cls

View File

@@ -50,12 +50,12 @@ from copy import copy
from deluge import component, configmanager
from deluge._libtorrent import lt
from deluge.core.rpcserver import export, check_ssl_keys
from deluge.core.rpcserver import export
from deluge.plugins.pluginbase import CorePluginBase
from twisted.internet import reactor, defer, task
from twisted.internet import reactor, defer
from twisted.python import randbytes
from twisted.web import server, resource, static, http, client
from twisted.web import server, resource, static, client
from .filelike import FilelikeObjectResource
from .resource import Resource
@@ -80,11 +80,13 @@ DEFAULT_PREFS = {
PRIORITY_INCREASE = 5
def sleep(seconds):
d = defer.Deferred()
reactor.callLater(seconds, d.callback, seconds)
return d
class ServerContextFactory(object):
def __init__(self, cert_file, key_file):
self._cert_file = cert_file
@@ -103,6 +105,7 @@ class ServerContextFactory(object):
ctx.use_privatekey_file(self._key_file)
return ctx
class FileServeResource(resource.Resource):
isLeaf = True
@@ -131,6 +134,7 @@ class FileServeResource(resource.Resource):
tfr = f.open()
return FilelikeObjectResource(tfr, f.size).render_GET(request)
class StreamResource(Resource):
isLeaf = True
@@ -180,12 +184,15 @@ class StreamResource(Resource):
result = yield self.client.stream_torrent(infohash=infohash, filepath_or_index=path, wait_for_end_pieces=wait_for_end_pieces)
defer.returnValue(json.dumps(result))
class UnknownTorrentException(Exception):
pass
class UnknownFileException(Exception):
pass
class TorrentFileReader(object):
def __init__(self, torrent_file):
self.torrent_file = torrent_file
@@ -222,7 +229,8 @@ class TorrentFileReader(object):
def seek(self, offset, whence=os.SEEK_SET):
self.position = offset
class TorrentFile(object): # can be read from, knows about itself
class TorrentFile(object): # can be read from, knows about itself
def __init__(self, torrent, first_piece, last_piece, piece_size, offset, path, full_path, size, index):
self.torrent = torrent
self.first_piece = first_piece
@@ -244,7 +252,6 @@ class TorrentFile(object): # can be read from, knows about itself
self.alerts = component.get("AlertManager")
def open(self):
"""
Returns a filelike object
@@ -335,6 +342,7 @@ class TorrentFile(object): # can be read from, knows about itself
def shutdown(self):
self.do_shutdown = True
class Torrent(object):
def __init__(self, torrent_handler, infohash):
self.infohash = infohash
@@ -347,8 +355,7 @@ class Torrent(object):
self.torrent_files = None
self.priority_increased = defaultdict(set)
self.do_shutdown = False
self.torrent_released = True # set to True if all the files are set to download
self.torrent_released = True # set to True if all the files are set to download
self.populate_files()
self.file_priorities = [0] * len(self.torrent_files)
@@ -427,7 +434,7 @@ class Torrent(object):
if self.torrent_released:
should_update_priorities = True
if should_update_priorities and not f.is_complete(): # Need to do this stuff on seek too
if should_update_priorities and not f.is_complete(): # Need to do this stuff on seek too
self.torrent.set_file_priorities(self.file_priorities)
return f
@@ -456,7 +463,7 @@ class Torrent(object):
for tf in self.torrent_files:
tf.shutdown()
def update_piece_priority(self): # if file streamed has reached end, unblacklist all prior pieces
def update_piece_priority(self): # if file streamed has reached end, unblacklist all prior pieces
if self.do_shutdown:
return
@@ -464,13 +471,13 @@ class Torrent(object):
currently_downloading = self.get_currently_downloading()
for f in self.torrent_files:
if not f.file_requested and not f.current_readers: # nobody wants the file and nobody is watching
if not f.file_requested and not f.current_readers: # nobody wants the file and nobody is watching
continue
logger.debug('Rescheduling file %s' % (f.path, ))
heads = set()
if f.file_requested: # we expect a piece head to be at start
if f.file_requested: # we expect a piece head to be at start
heads.add(f.first_piece)
waiting_for_pieces = set()
@@ -480,7 +487,7 @@ class Torrent(object):
waiting_for_pieces.add(tfr.waiting_for_piece)
piece = max(tfr.waiting_for_piece, tfr.current_piece)
if piece is not None:
if piece is not None:
heads.add(piece)
if not heads:
@@ -491,7 +498,6 @@ class Torrent(object):
for head_piece in heads:
priority_increased = 0
for piece, status in enumerate(self.torrent.status.pieces[head_piece:f.last_piece+1], head_piece):
#logger.debug('Checking status for %s/%s/%s/%s' % (head_piece, piece, status, self.torrent.handle.piece_priority(piece)))
if status or piece in currently_downloading:
continue
@@ -547,6 +553,7 @@ class Torrent(object):
reactor.callLater(1, self.update_piece_priority)
class TorrentHandler(object):
def __init__(self, config):
self.torrents = {}
@@ -584,6 +591,7 @@ class TorrentHandler(object):
for torrent in self.torrents.values():
torrent.shutdown()
class Core(CorePluginBase):
listening = None
base_url = None
@@ -618,7 +626,7 @@ class Core(CorePluginBase):
self.base_url = 'http'
if self.config['serve_method'] == 'standalone':
if self.config['use_ssl'] and self.check_ssl(): # use default deluge (or webui), input custom
if self.config['use_ssl'] and self.check_ssl(): # use default deluge (or webui), input custom
if self.config['ssl_source'] == 'daemon':
web_config = configmanager.ConfigManager("web.conf", {"pkey": "ssl/daemon.pkey",
"cert": "ssl/daemon.cert"})
@@ -642,7 +650,7 @@ class Core(CorePluginBase):
port = self.config['port']
ip = self.config['ip']
elif self.config['serve_method'] == 'webui' and self.check_webui(): # this webserver is fubar
elif self.config['serve_method'] == 'webui' and self.check_webui(): # this webserver is fubar
plugin_manager = component.get("CorePluginManager")
webui_plugin = plugin_manager['WebUi'].plugin

View File

@@ -30,8 +30,7 @@ class SingleRangeStaticProducer(static.SingleRangeStaticProducer):
if not self.request:
return
data = yield defer.maybeDeferred(self.fileObject.read,
min(self.bufferSize, self.size - self.bytesWritten))
data = yield defer.maybeDeferred(self.fileObject.read, min(self.bufferSize, self.size - self.bytesWritten))
if not self.request:
return
@@ -62,9 +61,7 @@ class MultipleRangeStaticProducer(static.MultipleRangeStaticProducer):
dataLength += len(self.partBoundary)
data.append(self.partBoundary)
self.partBoundary = None
p = yield defer.maybeDeferred(self.fileObject.read,
min(self.bufferSize - dataLength,
self._partSize - self._partBytesWritten))
p = yield defer.maybeDeferred(self.fileObject.read, min(self.bufferSize - dataLength, self._partSize - self._partBytesWritten))
self._partBytesWritten += len(p)
dataLength += len(p)
data.append(p)

View File

@@ -37,7 +37,6 @@
# statement from all source files in the program, then also delete it here.
#
import json
import gtk
import os
import subprocess
@@ -50,8 +49,7 @@ from deluge.plugins.pluginbase import GtkPluginBase
import deluge.component as component
import deluge.common
from twisted.internet import reactor, defer, threads
from twisted.web import server, resource
from twisted.internet import defer, threads
from common import get_resource

View File

@@ -1,5 +1,5 @@
from twisted.web.resource import Resource as TwistedResource, _computeAllowedMethods
from twisted.web import server, error
from twisted.web import server
from twisted.internet import defer
@@ -11,7 +11,7 @@ class Resource(TwistedResource):
self.password = password
TwistedResource.__init__(self, *args, **kwargs)
def render(self, request): # Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
def render(self, request): # Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
"""
Adds support for deferred render methods
"""
@@ -28,7 +28,6 @@ class Resource(TwistedResource):
if self.username == username and self.password == password:
authenticated = True
if not authenticated:
print auth_header
print self.username, self.password

View File

@@ -44,6 +44,6 @@ from deluge.plugins.pluginbase import WebPluginBase
from common import get_resource
class WebUI(WebPluginBase):
class WebUI(WebPluginBase):
scripts = [get_resource("streaming.js")]