From dc0ea5b3f61a6f6d1492a72b32106af4ad487c12 Mon Sep 17 00:00:00 2001 From: Ruud Date: Thu, 6 Mar 2014 21:30:59 +0100 Subject: [PATCH] Use proper sorting --- couchpotato/core/event.py | 9 ++------- couchpotato/core/helpers/variable.py | 13 +++---------- libs/axl/axel.py | 7 +++++-- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/couchpotato/core/event.py b/couchpotato/core/event.py index a36c430..8af2967 100644 --- a/couchpotato/core/event.py +++ b/couchpotato/core/event.py @@ -1,5 +1,5 @@ from axl.axel import Event -from couchpotato.core.helpers.variable import mergeDicts, natcmp +from couchpotato.core.helpers.variable import mergeDicts, natsortKey from couchpotato.core.logger import CPLog import threading import traceback @@ -51,11 +51,6 @@ def addEvent(name, handler, priority = 100): }) -def removeEvent(name, handler): - e = events[name] - e -= handler - - def fireEvent(name, *args, **kwargs): if name not in events: return @@ -106,7 +101,7 @@ def fireEvent(name, *args, **kwargs): result = e(*args, **kwargs) result_keys = result.keys() - result_keys.sort(natcmp) + result_keys.sort(key = natsortKey) if options['single'] and not options['merge']: results = None diff --git a/couchpotato/core/helpers/variable.py b/couchpotato/core/helpers/variable.py index 64cdce2..8be6f29 100644 --- a/couchpotato/core/helpers/variable.py +++ b/couchpotato/core/helpers/variable.py @@ -214,16 +214,9 @@ def tryFloat(s): return float(s) except: return 0 - -def natsortKey(s): - return map(tryInt, re.findall(r'(\d+|\D+)', s)) - - -def natcmp(a, b): - a2 = natsortKey(a) - b2 = natsortKey(b) - - return (a2 > b2) - (a2 < b2) +def natsortKey(string_): + """See http://www.codinghorror.com/blog/archives/001018.html""" + return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_)] def toIterable(value): diff --git a/libs/axl/axel.py b/libs/axl/axel.py index 46940da..d0f069a 100644 --- a/libs/axl/axel.py +++ b/libs/axl/axel.py @@ -11,7 +11,7 @@ # Source: http://pypi.python.org/pypi/axel # Docs: http://packages.python.org/axel -from couchpotato.core.helpers.variable import natcmp +from couchpotato.core.helpers.variable import natsortKey import Queue import hashlib import sys @@ -158,7 +158,10 @@ class Event(object): t.daemon = True t.start() - for handler in sorted(self.handlers.iterkeys(), cmp = natcmp): + handler_keys = self.handlers.keys() + handler_keys.sort(key = natsortKey) + + for handler in handler_keys: self.queue.put(handler) if self.asynchronous: