From eec89fe4a7b0340e7106b5e523ac6195a38d1dc9 Mon Sep 17 00:00:00 2001
From: Eirik Byrkjeflot Anonsen <eirik@eirikba.org>
Date: Tue, 17 May 2011 20:51:14 +0200
Subject: [PATCH] Add --sort option to deluge-console's "info" command.

---
 deluge/ui/console/commands/info.py |   25 +++++++++++++++++++++++--
 1 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/deluge/ui/console/commands/info.py b/deluge/ui/console/commands/info.py
index 7852399..da92750 100644
--- a/deluge/ui/console/commands/info.py
+++ b/deluge/ui/console/commands/info.py
@@ -93,11 +93,17 @@ def format_progressbar(progress, width):
 class Command(BaseCommand):
     """Show information about the torrents"""
 
+    sort_help = 'sort items.  Possible keys: ' + ', '.join(status_keys)
+
     option_list = BaseCommand.option_list + (
             make_option('-v', '--verbose', action='store_true', default=False, dest='verbose',
                         help='shows more information per torrent'),
             make_option('-i', '--id', action='store_true', default=False, dest='tid',
                         help='use internal id instead of torrent name'),
+            make_option('--sort', action='store', type='string', default='', dest='sort',
+                        help=sort_help),
+            make_option('--sort-reverse', action='store', type='string', default='', dest='sort_rev',
+                        help='sort items in reverse order.  Same keys allowed as for --sort.')
     )
 
     usage =  "Usage: info [<torrent-id> [<torrent-id> ...]]\n"\
@@ -115,8 +121,23 @@ def handle(self, *args, **options):
 
         def on_torrents_status(status):
             # Print out the information for each torrent
-            for key, value in status.items():
-                self.show_info(key, value, options["verbose"])
+            sort_key = options['sort']
+            sort_reverse = False
+            if not sort_key:
+                sort_key = options['sort_rev']
+                sort_reverse = True
+            if not sort_key:
+                sort_key = 'name'
+                sort_reverse = False
+            if sort_key not in status_keys:
+                self.console.write('')
+                self.console.write("{!error!}Unknown sort key: " + sort_key + ", will sort on name")
+                sort_key = 'name'
+                sort_reverse = False
+            for key, value in sorted(status.items(),
+                                     key=lambda x : x[1].get(sort_key),
+                                     reverse=sort_reverse):
+                self.show_info(key, status[key], options["verbose"])
 
         def on_torrents_status_fail(reason):
             self.console.write("{!error!}Error getting torrent info: %s" % reason)
-- 
1.7.2.5

