Ticket #1858: 0001-Implement-console-command-for-displaying-and-setting.4.patch

File 0001-Implement-console-command-for-displaying-and-setting.4.patch, 12.6 KB (added by eirikba, 12 years ago)
  • new file deluge/ui/console/commands/manage.py

    From 53228f9d15ff5fdb2e1146bc757ef7105ad44e60 Mon Sep 17 00:00:00 2001
    From: Eirik Byrkjeflot Anonsen <eirik@eirikba.org>
    Date: Thu, 19 May 2011 21:19:13 +0200
    Subject: [PATCH] Implement console command for displaying and setting per-torrent options.
    
    ---
     deluge/ui/console/commands/manage.py |  269 ++++++++++++++++++++++++++++++++++
     1 files changed, 269 insertions(+), 0 deletions(-)
     create mode 100644 deluge/ui/console/commands/manage.py
    
    diff --git a/deluge/ui/console/commands/manage.py b/deluge/ui/console/commands/manage.py
    new file mode 100644
    index 0000000..1f0debd
    - +  
     1# 
     2# manage.py 
     3# 
     4# Copyright (C) 2008-2009 Ido Abramovich <ido.deluge@gmail.com> 
     5# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com> 
     6# 
     7# Deluge is free software. 
     8# 
     9# You may redistribute it and/or modify it under the terms of the 
     10# GNU General Public License, as published by the Free Software 
     11# Foundation; either version 3 of the License, or (at your option) 
     12# any later version. 
     13# 
     14# deluge is distributed in the hope that it will be useful, 
     15# but WITHOUT ANY WARRANTY; without even the implied warranty of 
     16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
     17# See the GNU General Public License for more details. 
     18# 
     19# You should have received a copy of the GNU General Public License 
     20# along with deluge.    If not, write to: 
     21#       The Free Software Foundation, Inc., 
     22#       51 Franklin Street, Fifth Floor 
     23#       Boston, MA  02110-1301, USA. 
     24# 
     25#    In addition, as a special exception, the copyright holders give 
     26#    permission to link the code of portions of this program with the OpenSSL 
     27#    library. 
     28#    You must obey the GNU General Public License in all respects for all of 
     29#    the code used other than OpenSSL. If you modify file(s) with this 
     30#    exception, you may extend this exception to your version of the file(s), 
     31#    but you are not obligated to do so. If you do not wish to do so, delete 
     32#    this exception statement from your version. If you delete this exception 
     33#    statement from all source files in the program, then also delete it here. 
     34# 
     35# 
     36 
     37from deluge.ui.console.main import BaseCommand 
     38import deluge.ui.console.colors as colors 
     39from deluge.ui.client import client 
     40import deluge.component as component 
     41from deluge.log import LOG as log 
     42 
     43from optparse import make_option 
     44 
     45 
     46# torrent.py's Torrent.get_status() renames some of the options. 
     47# This maps the getter names to the setter names 
     48get_torrent_options = { 
     49    'max_download_speed': 'max_download_speed', 
     50    'max_upload_speed': 'max_upload_speed', 
     51    'max_connections': 'max_connections', 
     52    'max_upload_slots': 'max_upload_slots', 
     53    'prioritize_first_last': 'prioritize_first_last_pieces', 
     54    'is_auto_managed': 'auto_managed', 
     55    'stop_at_ratio': 'stop_at_ratio', 
     56    'stop_ratio': 'stop_ratio', 
     57    'remove_at_ratio': 'remove_at_ratio', 
     58    'move_on_completed': 'move_completed', 
     59    'move_on_completed_path': 'move_completed_path', 
     60    #'file_priorities': 'file_priorities', 
     61    #'compact': 'compact_allocation', 
     62    #'save_path': 'download_location' 
     63    } 
     64 
     65# These are the things that can be set, as far as I can tell.  The 
     66# ones that aren't commented out are the ones that look like they 
     67# probably will behave as expected if set. 
     68# 
     69# Each value is [ type, getter name, help text ] 
     70set_torrent_options = { 
     71    # These are handled by torrent.py's Torrent.set_options() 
     72    'auto_managed': [bool, None, "Makes torrent obey deluge's queue settings.  (see FAQ for details)."], 
     73    #'download_location': str, # Probably not useful to set 
     74    #'file_priorities': ???, # Not a simple value to set, probably needs its own command 
     75    'max_connections': [int, None, 'Maximum number of connections to use for this torrent.'], 
     76    'max_download_speed': [float, None, 'Maximum total download speed to allow this torrent to use (KiB/s).'], 
     77    'max_upload_slots': [int, None, 'Maximum number of connections to allow this torrent to use for uploading.'], 
     78    'max_upload_speed': [float, None, 'Maximum total upload speed to allow this torrent to use (KiB/s).'], 
     79    'prioritize_first_last_pieces': [bool, None, 'Whether to download the first and last piece of the torrent first.\nNOTE: Only has effect if the torrent contains a single file.'], # Only has effect if torrent contains a single file.  Can only be set, not cleared. 
     80 
     81    # These are "handled" by being set directly on the options dict 
     82    'stop_at_ratio': [bool, None, 'Whether to stop seeding when share ratio reaches "stop_ratio".'], 
     83    'stop_ratio': [float, None, 'The ratio at which to stop seeding (if "stop_at_ratio" is True).'], 
     84    'remove_at_ratio': [bool, None, 'Whether to remove torrent when share ratio reaches "stop_ratio".'], 
     85    'move_completed': [bool, None, 'Whether to move the downloaded data when downloading is complete.'], 
     86    'move_completed_path': [str, None, 'Where to move completed data to (if "move_completed" is True).'], 
     87 
     88    #'compact_allocation': bool, # Unclear what setting this would do 
     89    #'add_paused': ???, # Not returned by get_status, unclear what setting it would do 
     90    #'mapped_files': ??? # Not returned by get_status, unclear what setting it would do 
     91    } 
     92for k,v in get_torrent_options.items(): 
     93    set_torrent_options[v][1] = k 
     94 
     95 
     96def layout_option_help(opt, text): 
     97    base_indent = 7 
     98    hanging_indent = 15 
     99    offset_indent = 20 - len(opt) 
     100    split_text = ('\n' + ' ' * hanging_indent).join(text.split('\n')) 
     101    if offset_indent > 0: 
     102        split_text = ' ' * offset_indent + split_text 
     103    else: 
     104        split_text = '\n' + ' ' * hanging_indent + split_text 
     105    return ' ' * base_indent + opt + ': ' + split_text 
     106 
     107class Command(BaseCommand): 
     108    """Show and set per-torrent options""" 
     109 
     110    option_help = [ layout_option_help(k, v[2]) for k,v in set_torrent_options.items() ] 
     111    option_help.sort() 
     112 
     113    option_list = BaseCommand.option_list + ( 
     114            make_option('-s', '--set', action='store', nargs=2, dest='set', 
     115                        help='set value for key'), 
     116    ) 
     117    usage = '''Usage: manage <torrent-id> [<torrent-id> ...] [<key1> [<key2> ...]] 
     118       manage <torrent-id> [<torrent-id> ...] --set <key> <value> 
     119 
     120       The torrent-id * (a single asterisk) means "all loaded torrents". 
     121       The value -1 means unlimited (for max_connections, max_download_speed, max_upload_slots and max_upload_speed). 
     122 
     123Available keys: 
     124''' + '\n'.join(option_help) 
     125 
     126 
     127    def handle(self, *args, **options): 
     128        self.console = component.get('ConsoleUI') 
     129        if options['set']: 
     130            return self._set_option(*args, **options) 
     131        else: 
     132            return self._get_option(*args, **options) 
     133 
     134 
     135    def _get_option(self, *args, **options): 
     136 
     137        def on_torrents_status(status): 
     138            for torrentid, data in status.items(): 
     139                self.console.write('\n') 
     140                if 'name' in data: 
     141                    self.console.write('{!info!}Name: {!input!}%s' % data.get('name')) 
     142                self.console.write('{!info!}ID: {!input!}%s' % torrentid) 
     143                for k, v in data.items(): 
     144                    if k != 'name': 
     145                        displayname = get_torrent_options.get(k, '???' + k) 
     146                        self.console.write('{!info!}%s: {!input!}%s' % (displayname, v)) 
     147 
     148        def on_torrents_status_fail(reason): 
     149            self.console.write('{!error!}Failed to get torrent data.') 
     150 
     151        torrent_ids = [] 
     152        request_options = [] 
     153 
     154        for arg in args: 
     155            if arg in set_torrent_options: 
     156                request_options.append(set_torrent_options[arg][1]) 
     157            else: 
     158                if arg == '*': 
     159                    ids = self.console.match_torrent('') 
     160                else: 
     161                    ids = self.console.match_torrent(arg) 
     162                if not ids: 
     163                    self.console.write("{!error!}The argument '" + arg + "' is not a recognized option nor did it match any torrents") 
     164                    return 
     165                torrent_ids.extend(ids) 
     166 
     167        if not torrent_ids: 
     168            self.console.write('{!error!}No torrents mentioned.  To request info on all torrents use "manage * [<key>...]".') 
     169            return 
     170 
     171        if not request_options: 
     172            request_options = [ opt for opt in get_torrent_options ] 
     173        request_options.append('name') 
     174 
     175        d = client.core.get_torrents_status({'id': torrent_ids}, request_options) 
     176        d.addCallback(on_torrents_status) 
     177        d.addErrback(on_torrents_status_fail) 
     178        return d 
     179 
     180 
     181    def _set_prioritize_first_last_pieces(self, torrent_ids, val): 
     182 
     183        def on_option_set(status): 
     184            self.console.write('{!success!}Torrent option successfully updated.') 
     185 
     186        def on_set_option_failed(reason): 
     187            self.console.write("{!error!}Error setting torrent option: %s" % reason) 
     188 
     189        def on_got_info(status): 
     190            single_ids = [] 
     191            multi_ids = [] 
     192            error_ids = [] 
     193            for tid in torrent_ids: 
     194                if tid not in status: 
     195                    error_ids.append(tid) 
     196                elif len(status[tid]['files']) > 1: 
     197                    multi_ids.append(tid) 
     198                else: 
     199                    single_ids.append(tid) 
     200            if multi_ids: 
     201                torrent_names = [ self.console.get_torrent_name(tid) for tid in multi_ids ] 
     202                self.console.write('{!error!}Not setting prioritize_first_last_pieces to ' + str(val) + 
     203                                   ' (multiple files in torrent) for torrents: ' + ', '.join(torrent_names)) 
     204            if error_ids: 
     205                torrent_names = [ self.console.get_torrent_name(tid) for tid in error_ids ] 
     206                self.console.write('{!error!}Will try to set prioritize_first_last_pieces to  ' + str(val) + 
     207                                   ', but may fail silently for torrents: ' + ', '.join(torrent_names)) 
     208 
     209            use_ids = single_ids + error_ids 
     210            if use_ids: 
     211                torrent_names = [ self.console.get_torrent_name(tid) for tid in single_ids ] 
     212                self.console.write('{!info!}Setting prioritize_first_last_pieces to %s for torrent(s): %s' % (val, ', '.join(torrent_names))) 
     213                d = client.core.set_torrent_options(use_ids, {'prioritize_first_last_pieces': val}) 
     214                d.addCallback(on_option_set) 
     215                d.addErrback(on_set_option_failed) 
     216            else: 
     217                self.console.write('{!error!}No valid torrents to set prioritize_first_last_pieces on') 
     218 
     219        def on_info_failed(reason): 
     220            self.console.write("{!error!}Error getting torrent info (for setting prioritize_last_first_pieces): %s" % reason) 
     221 
     222        d = client.core.get_torrents_status({"id": torrent_ids}, [ "files" ]) 
     223        d.addCallback(on_got_info) 
     224        d.addErrback(on_info_failed) 
     225 
     226    def _set_option(self, *args, **options): 
     227        torrent_ids = [] 
     228        for arg in args: 
     229            if arg == '*': 
     230                ids = self.console.match_torrent('') 
     231            else: 
     232                ids = self.console.match_torrent(arg) 
     233            if not ids: 
     234                self.console.write("{!error!}The argument '" + arg + "' did not match any torrents") 
     235                return 
     236            torrent_ids.extend(ids) 
     237        if not torrent_ids: 
     238            self.console.write('{!error!}No torrents mentioned.') 
     239            return 
     240        key = options['set'][0] 
     241        val = options['set'][1] 
     242 
     243        if key not in set_torrent_options: 
     244            self.console.write("{!error!}The key '%s' is invalid!" % key) 
     245            return 
     246 
     247        val = set_torrent_options[key][0](val) 
     248 
     249        if key == 'prioritize_first_last_pieces': 
     250            self._set_prioritize_first_last_pieces(torrent_ids, val) 
     251            return 
     252 
     253        def on_option_set(result): 
     254            self.console.write('{!success!}Torrent option successfully updated.') 
     255 
     256        def on_set_option_failed(reason): 
     257            self.console.write("{!error!}Error setting torrent option: %s" % reason) 
     258 
     259        torrent_names = [ self.console.get_torrent_name(tid) for tid in torrent_ids ] 
     260        self.console.write('{!info!}Setting %s to %s for torrent(s): %s' % (key, val, ', '.join(torrent_names))) 
     261        d = client.core.set_torrent_options(torrent_ids, {key: val}) 
     262        d.addCallback(on_option_set) 
     263        d.addErrback(on_set_option_failed) 
     264 
     265    def complete(self, text): 
     266        torrents = component.get('ConsoleUI').tab_complete_torrent(text) 
     267        options = [ x for x in set_torrent_options if x.startswith(text) ] 
     268        # This should probably only return options immediately after --set. 
     269        return torrents + options