Ticket #1856: 0002-Add-sort-option-to-deluge-console-s-info-command.patch

File 0002-Add-sort-option-to-deluge-console-s-info-command.patch, 2.4 KB (added by eirikba, 13 years ago)
  • deluge/ui/console/commands/info.py

    From 7305cf3bcfa9b45c6b8ad8fe4a1b4270fabc4ef2 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 2/4] Add --sort option to deluge-console's "info" command.
    
    ---
     deluge/ui/console/commands/info.py |   19 +++++++++++++++++--
     1 files changed, 17 insertions(+), 2 deletions(-)
    
    diff --git a/deluge/ui/console/commands/info.py b/deluge/ui/console/commands/info.py
    index 7852399..e775bbe 100644
    a b def format_progressbar(progress, width):  
    9393class Command(BaseCommand):
    9494    """Show information about the torrents"""
    9595
     96    sort_help = 'sort items.  append "_rev" to the key to sort in reverse (e.g. eta -> eta_rev).  Possible keys: ' + ', '.join(status_keys)
     97
    9698    option_list = BaseCommand.option_list + (
    9799            make_option('-v', '--verbose', action='store_true', default=False, dest='verbose',
    98100                        help='shows more information per torrent'),
    99101            make_option('-i', '--id', action='store_true', default=False, dest='tid',
    100102                        help='use internal id instead of torrent name'),
     103            make_option('--sort', action='store', type='string', default='', dest='sort',
     104                        help=sort_help)
    101105    )
    102106
    103107    usage =  "Usage: info [<torrent-id> [<torrent-id> ...]]\n"\
    def handle(self, *args, **options):  
    115119
    116120        def on_torrents_status(status):
    117121            # Print out the information for each torrent
    118             for key, value in status.items():
    119                 self.show_info(key, value, options["verbose"])
     122            keys = [ k for k in status.keys() ]
     123            if options["sort"]:
     124                reverse = False
     125                sort_key = options["sort"]
     126                if sort_key.endswith("_rev"):
     127                    reverse = True
     128                    sort_key = sort_key[:-4]
     129                if sort_key not in status_keys:
     130                    self.console.write('')
     131                    self.console.write("{!error!}Unknown sort key: " + sort_key)
     132                keys.sort(key=lambda x : status[x].get(sort_key), reverse=reverse)
     133            for key in keys:
     134                self.show_info(key, status[key], options["verbose"])
    120135
    121136        def on_torrents_status_fail(reason):
    122137            self.console.write("{!error!}Error getting torrent info: %s" % reason)