From a02257a906086d84997dde22c9b2bf3ce10c8479 Mon Sep 17 00:00:00 2001 From: Ruud Date: Mon, 29 Oct 2012 16:06:32 +0100 Subject: [PATCH] Enable both nzb and torrent by default Use default user download dir and enable blackhole --- .../core/downloaders/blackhole/__init__.py | 4 +- couchpotato/core/event.py | 16 ++++- couchpotato/core/helpers/variable.py | 25 +++++++- couchpotato/core/logger.py | 3 + couchpotato/core/plugins/browser/main.py | 11 +--- .../core/plugins/wizard/static/wizard.js | 2 +- .../core/providers/nzb/mysterbin/__init__.py | 2 +- .../core/providers/nzb/nzbindex/__init__.py | 1 + .../torrent/kickasstorrents/__init__.py | 2 +- .../providers/torrent/kickasstorrents/main.py | 1 + .../providers/torrent/publichd/__init__.py | 2 +- .../torrent/thepiratebay/__init__.py | 2 +- libs/axl/axel.py | 60 +++++++++++++------ 13 files changed, 91 insertions(+), 40 deletions(-) diff --git a/couchpotato/core/downloaders/blackhole/__init__.py b/couchpotato/core/downloaders/blackhole/__init__.py index c287538b..71649df1 100644 --- a/couchpotato/core/downloaders/blackhole/__init__.py +++ b/couchpotato/core/downloaders/blackhole/__init__.py @@ -1,4 +1,5 @@ from .main import Blackhole +from couchpotato.core.helpers.variable import getDownloadDir def start(): return Blackhole() @@ -16,7 +17,7 @@ config = [{ 'options': [ { 'name': 'enabled', - 'default': 0, + 'default': True, 'type': 'enabler', 'radio_group': 'nzb,torrent', }, @@ -24,6 +25,7 @@ config = [{ 'name': 'directory', 'type': 'directory', 'description': 'Directory where the .nzb (or .torrent) file is saved to.', + 'default': getDownloadDir() }, { 'name': 'use_for', diff --git a/couchpotato/core/event.py b/couchpotato/core/event.py index 99ca6f50..c64e35f0 100644 --- a/couchpotato/core/event.py +++ b/couchpotato/core/event.py @@ -63,11 +63,21 @@ def fireEvent(name, *args, **kwargs): except: pass e = events[name] - if not options['in_order']: e.lock.acquire() + + # Lock this event + e.lock.acquire() + e.asynchronous = False - e.in_order = options['in_order'] + + # Make sure only 1 event is fired at a time when order is wanted + kwargs['event_order_lock'] = threading.RLock() if options['in_order'] or options['single'] else None + kwargs['event_return_on_result'] = options['single'] + + # Fire result = e(*args, **kwargs) - if not options['in_order']: e.lock.release() + + # Release lock for this event + e.lock.release() if options['single'] and not options['merge']: results = None diff --git a/couchpotato/core/helpers/variable.py b/couchpotato/core/helpers/variable.py index dea01686..06f22bb4 100644 --- a/couchpotato/core/helpers/variable.py +++ b/couchpotato/core/helpers/variable.py @@ -9,15 +9,34 @@ import sys log = CPLog(__name__) +def getUserDir(): + try: + import pwd + os.environ['HOME'] = pwd.getpwuid(os.geteuid()).pw_dir + except: + pass + + return os.path.expanduser('~') + +def getDownloadDir(): + user_dir = getUserDir() + + # OSX + if 'darwin' in platform.platform().lower(): + return os.path.join(user_dir, 'Downloads') + + if os.name == 'nt': + return os.path.join(user_dir, 'Downloads') + + return user_dir + def getDataDir(): # Windows if os.name == 'nt': return os.path.join(os.environ['APPDATA'], 'CouchPotato') - import pwd - os.environ['HOME'] = pwd.getpwuid(os.geteuid()).pw_dir - user_dir = os.path.expanduser('~') + user_dir = getUserDir() # OSX if 'darwin' in platform.platform().lower(): diff --git a/couchpotato/core/logger.py b/couchpotato/core/logger.py index 7a357b36..09875a67 100644 --- a/couchpotato/core/logger.py +++ b/couchpotato/core/logger.py @@ -17,6 +17,9 @@ class CPLog(object): def info(self, msg, replace_tuple = ()): self.logger.info(self.addContext(msg, replace_tuple)) + def info2(self, msg, replace_tuple = ()): + self.logger.log(19, self.addContext(msg, replace_tuple)) + def debug(self, msg, replace_tuple = ()): self.logger.debug(self.addContext(msg, replace_tuple)) diff --git a/couchpotato/core/plugins/browser/main.py b/couchpotato/core/plugins/browser/main.py index b84284b1..b5839e73 100644 --- a/couchpotato/core/plugins/browser/main.py +++ b/couchpotato/core/plugins/browser/main.py @@ -1,5 +1,6 @@ from couchpotato.api import addApiView from couchpotato.core.helpers.request import getParam, jsonified +from couchpotato.core.helpers.variable import getUserDir from couchpotato.core.plugins.base import Plugin import ctypes import os @@ -65,15 +66,7 @@ class FileBrowser(Plugin): def view(self): path = getParam('path', '/') - - # Set proper home dir for some systems - try: - import pwd - os.environ['HOME'] = pwd.getpwuid(os.geteuid()).pw_dir - except: - pass - - home = os.path.expanduser('~') + home = getUserDir() if not path: path = home diff --git a/couchpotato/core/plugins/wizard/static/wizard.js b/couchpotato/core/plugins/wizard/static/wizard.js index 5d087add..75cd63be 100644 --- a/couchpotato/core/plugins/wizard/static/wizard.js +++ b/couchpotato/core/plugins/wizard/static/wizard.js @@ -37,7 +37,7 @@ Page.Wizard = new Class({ }, 'downloaders': { 'title': 'What download apps are you using?', - 'description': 'CP needs an external download app to work with. Choose one below. For more downloaders check settings after you have filled in the wizard. If your download app isn\'t in the list, use Blackhole.' + 'description': 'CP needs an external download app to work with. Choose one below. For more downloaders check settings after you have filled in the wizard. If your download app isn\'t in the list, use the default Blackhole.' }, 'providers': { 'title': 'Are you registered at any of these sites?', diff --git a/couchpotato/core/providers/nzb/mysterbin/__init__.py b/couchpotato/core/providers/nzb/mysterbin/__init__.py index fd4de655..a28d9a88 100644 --- a/couchpotato/core/providers/nzb/mysterbin/__init__.py +++ b/couchpotato/core/providers/nzb/mysterbin/__init__.py @@ -15,7 +15,7 @@ config = [{ { 'name': 'enabled', 'type': 'enabler', - 'default': False, + 'default': True, }, ], }, diff --git a/couchpotato/core/providers/nzb/nzbindex/__init__.py b/couchpotato/core/providers/nzb/nzbindex/__init__.py index 51ee6d94..04d5022e 100644 --- a/couchpotato/core/providers/nzb/nzbindex/__init__.py +++ b/couchpotato/core/providers/nzb/nzbindex/__init__.py @@ -15,6 +15,7 @@ config = [{ { 'name': 'enabled', 'type': 'enabler', + 'default': True, }, ], }, diff --git a/couchpotato/core/providers/torrent/kickasstorrents/__init__.py b/couchpotato/core/providers/torrent/kickasstorrents/__init__.py index ffa3934c..d31250fb 100644 --- a/couchpotato/core/providers/torrent/kickasstorrents/__init__.py +++ b/couchpotato/core/providers/torrent/kickasstorrents/__init__.py @@ -16,7 +16,7 @@ config = [{ { 'name': 'enabled', 'type': 'enabler', - 'default': False, + 'default': True, }, ], }, diff --git a/couchpotato/core/providers/torrent/kickasstorrents/main.py b/couchpotato/core/providers/torrent/kickasstorrents/main.py index f59d8df5..e8c3cd16 100644 --- a/couchpotato/core/providers/torrent/kickasstorrents/main.py +++ b/couchpotato/core/providers/torrent/kickasstorrents/main.py @@ -94,6 +94,7 @@ class KickAssTorrents(TorrentProvider): is_correct_movie = fireEvent('searcher.correct_movie', nzb = new, movie = movie, quality = quality, imdb_results = True, single = True) + if is_correct_movie: results.append(new) self.found(new) diff --git a/couchpotato/core/providers/torrent/publichd/__init__.py b/couchpotato/core/providers/torrent/publichd/__init__.py index 648c6766..49a9d96c 100644 --- a/couchpotato/core/providers/torrent/publichd/__init__.py +++ b/couchpotato/core/providers/torrent/publichd/__init__.py @@ -15,7 +15,7 @@ config = [{ { 'name': 'enabled', 'type': 'enabler', - 'default': False, + 'default': True, }, ], }, diff --git a/couchpotato/core/providers/torrent/thepiratebay/__init__.py b/couchpotato/core/providers/torrent/thepiratebay/__init__.py index 9c56eb8f..f890ca0e 100644 --- a/couchpotato/core/providers/torrent/thepiratebay/__init__.py +++ b/couchpotato/core/providers/torrent/thepiratebay/__init__.py @@ -16,7 +16,7 @@ config = [{ { 'name': 'enabled', 'type': 'enabler', - 'default': False + 'default': True }, { 'name': 'domain', diff --git a/libs/axl/axel.py b/libs/axl/axel.py index 8e9b6072..d90e5793 100644 --- a/libs/axl/axel.py +++ b/libs/axl/axel.py @@ -95,7 +95,6 @@ class Event(object): (None, None, handler), ... # asynchronous execution ) """ - self.in_order = False self.name = name self.asynchronous = asynch self.exc_info = exc_info @@ -142,11 +141,16 @@ class Event(object): def fire(self, *args, **kwargs): """ Stores all registered handlers in a queue for processing """ self.queue = Queue.Queue() - self.result = {} + result = {} if self.handlers: - max_threads = self._threads() + max_threads = 1 if kwargs.get('event_order_lock') else self._threads() + + # Set global result + def add_to(key, value): + result[key] = value + kwargs['event_add_to_result'] = add_to for i in range(max_threads): t = threading.Thread(target = self._execute, @@ -159,17 +163,12 @@ class Event(object): if self.asynchronous: handler_, memoize, timeout = self.handlers[handler] - self.result[handler] = (None, None, handler_) + result[handler] = (None, None, handler_) if not self.asynchronous: self.queue.join() - res = self.result or None - - # Cleanup - self.result = {} - - return res + return result def count(self): """ Returns the count of registered handlers """ @@ -181,24 +180,47 @@ class Event(object): self.memoize.clear() def _execute(self, *args, **kwargs): + + # Remove get and set from kwargs + add_to_result = kwargs.get('event_add_to_result') + del kwargs['event_add_to_result'] + + # Get and remove order lock + order_lock = kwargs.get('event_order_lock') + try: del kwargs['event_order_lock'] + except: pass + + # Get and remove return on first + return_on_result = kwargs.get('event_return_on_result') + try: del kwargs['event_return_on_result'] + except: pass + + got_results = False + """ Executes all handlers stored in the queue """ while True: + try: h_ = self.queue.get(timeout = 2) handler, memoize, timeout = self.handlers[h_] - if self.lock and self.in_order: - self.lock.acquire() + if return_on_result and got_results: + continue + + if order_lock: + order_lock.acquire() try: r = self._memoize(memoize, timeout, handler, *args, **kwargs) if not self.asynchronous: - self.result[h_] = tuple(r) + if not return_on_result or (return_on_result and r[1]): + add_to_result(h_, tuple(r)) + got_results = True except Exception: if not self.asynchronous: - self.result[h_] = (False, self._error(sys.exc_info()), - handler) + add_to_result(h_, (False, self._error(sys.exc_info()), + handler)) else: self.error_handler(sys.exc_info()) finally: @@ -206,8 +228,8 @@ class Event(object): if not self.asynchronous: self.queue.task_done() - if self.lock and self.in_order: - self.lock.release() + if order_lock: + order_lock.release() if self.queue.empty(): raise Queue.Empty @@ -257,7 +279,7 @@ class Event(object): args.insert(0, self.sender) if not memoize: - if timeout <= 0: #no time restriction + if timeout <= 0: #no time restriction result = [True, handler(*args, **kwargs), handler] return result @@ -273,7 +295,7 @@ class Event(object): if args_ == args and kwargs_ == kwargs: return [True, result, handler] - if timeout <= 0: #no time restriction + if timeout <= 0: #no time restriction result = handler(*args, **kwargs) else: result = self._timeout(timeout, handler, *args, **kwargs)