Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

# -*- coding: utf-8 -*- 

# 

# Copyright (C) 2008-2009 Ido Abramovich <ido.deluge@gmail.com> 

# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com> 

# 

# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with 

# the additional special exception to link portions of this program with the OpenSSL library. 

# See LICENSE for more details. 

# 

 

from optparse import make_option 

 

from twisted.internet import defer 

 

import deluge.component as component 

from deluge.ui.client import client 

from deluge.ui.console.main import BaseCommand 

 

torrent_options = { 

    "max_download_speed": float, 

    "max_upload_speed": float, 

    "max_connections": int, 

    "max_upload_slots": int, 

    "private": bool, 

    "prioritize_first_last": bool, 

    "is_auto_managed": bool, 

    "stop_at_ratio": bool, 

    "stop_ratio": float, 

    "remove_at_ratio": bool, 

    "move_on_completed": bool, 

    "move_on_completed_path": str 

} 

 

 

class Command(BaseCommand): 

    """Show and manage per-torrent options""" 

 

    option_list = BaseCommand.option_list + ( 

        make_option("-s", "--set", action="store", nargs=2, dest="set", help="set value for key"), 

    ) 

    usage = "Usage: manage <torrent-id> [<key1> [<key2> ...]]\n"\ 

            "       manage <torrent-id> --set <key> <value>" 

 

    def handle(self, *args, **options): 

        self.console = component.get("ConsoleUI") 

        if options['set']: 

            return self._set_option(*args, **options) 

        else: 

            return self._get_option(*args, **options) 

 

    def _get_option(self, *args, **options): 

 

        def on_torrents_status(status): 

            for torrentid, data in status.items(): 

                self.console.write('') 

                if 'name' in data: 

                    self.console.write('{!info!}Name: {!input!}%s' % data.get('name')) 

                self.console.write('{!info!}ID: {!input!}%s' % torrentid) 

                for k, v in data.items(): 

                    if k != 'name': 

                        self.console.write('{!info!}%s: {!input!}%s' % (k, v)) 

 

        def on_torrents_status_fail(reason): 

            self.console.write('{!error!}Failed to get torrent data.') 

 

        torrent_ids = [] 

        torrent_ids.extend(self.console.match_torrent(args[0])) 

 

        request_options = [] 

        for opt in args[1:]: 

            if opt not in torrent_options: 

                self.console.write('{!error!}Unknown torrent option: %s' % opt) 

                return 

            request_options.append(opt) 

        if not request_options: 

            request_options = [opt for opt in torrent_options.keys()] 

        request_options.append('name') 

 

        d = client.core.get_torrents_status({"id": torrent_ids}, request_options) 

        d.addCallback(on_torrents_status) 

        d.addErrback(on_torrents_status_fail) 

        return d 

 

    def _set_option(self, *args, **options): 

        deferred = defer.Deferred() 

        torrent_ids = [] 

        torrent_ids.extend(self.console.match_torrent(args[0])) 

        key = options["set"][0] 

        val = options["set"][1] + " " .join(args[1:]) 

 

        if key not in torrent_options: 

            self.console.write("{!error!}The key '%s' is invalid!" % key) 

            return 

 

        val = torrent_options[key](val) 

 

        def on_set_config(result): 

            self.console.write("{!success!}Torrent option successfully updated.") 

            deferred.callback(True) 

 

        self.console.write("Setting %s to %s for torrents %s.." % (key, val, torrent_ids)) 

 

        for tid in torrent_ids: 

            if key == "move_on_completed_path": 

                client.core.set_torrent_move_completed_path(tid, val).addCallback(on_set_config) 

            elif key == "move_on_completed": 

                client.core.set_torrent_move_completed(tid, val).addCallback(on_set_config) 

            elif key == "is_auto_managed": 

                client.core.set_torrent_auto_managed(tid, val).addCallback(on_set_config) 

            elif key == "remove_at_ratio": 

                client.core.set_torrent_remove_at_ratio(tid, val).addCallback(on_set_config) 

            elif key == "prioritize_first_last": 

                client.core.set_torrent_prioritize_first_last(tid, val).addCallback(on_set_config) 

            else: 

                client.core.set_torrent_options(torrent_ids, {key: val}).addCallback(on_set_config) 

                break 

        return deferred 

 

    def complete(self, line): 

        # We use the ConsoleUI torrent tab complete method 

        return component.get("ConsoleUI").tab_complete_torrent(line)