Changeset 3770

Show
Ignore:
Timestamp:
08/28/08 04:40:49 (4 months ago)
Author:
andar
Message:

Implement #352 set_torrent_options method in core

Location:
trunk/deluge/core
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/deluge/core/core.py

    r3753 r3770  
    521521            self.torrents[torrent_id].force_recheck() 
    522522 
     523    def export_set_torrent_options(self, torrent_ids, options): 
     524        """Sets the torrent options for torrent_ids""" 
     525        for torrent_id in torrent_ids: 
     526            self.torrents[torrent_id].set_options(options) 
     527             
    523528    def export_set_torrent_trackers(self, torrent_id, trackers): 
    524529        """Sets a torrents tracker list.  trackers will be [{"url", "tier"}]""" 
  • trunk/deluge/core/torrent.py

    r3727 r3770  
    4242from deluge.configmanager import ConfigManager 
    4343from deluge.log import LOG as log 
     44from deluge.core.preferencesmanager import DEFAULT_PREFS 
    4445import deluge.xmlrpclib 
    4546 
    4647TORRENT_STATE = deluge.common.TORRENT_STATE 
     48 
     49OPTIONS = { 
     50    "max_download_speed": DEFAULT_PREFS["max_download_speed_per_torrent"], 
     51    "max_upload_speed": DEFAULT_PREFS["max_upload_speed_per_torrent"], 
     52    "max_connections": DEFAULT_PREFS["max_connections_per_torrent"], 
     53    "max_upload_slots": DEFAULT_PREFS["max_upload_slots_per_torrent"], 
     54    "prioritize_first_last_pieces": DEFAULT_PREFS["prioritize_first_last_pieces"], 
     55    "auto_managed": DEFAULT_PREFS["auto_managed"], 
     56    "stop_at_ratio": DEFAULT_PREFS["stop_seed_at_ratio"], 
     57    "stop_ratio": DEFAULT_PREFS["stop_seed_ratio"], 
     58    "remove_at_ratio": DEFAULT_PREFS["remove_seed_at_ratio"], 
     59    "move_completed": DEFAULT_PREFS["move_completed"], 
     60    "move_completed_path": DEFAULT_PREFS["move_completed_path"], 
     61    "file_priorities": [], 
     62    "compact_allocation": DEFAULT_PREFS["compact_allocation"], 
     63    "download_location": DEFAULT_PREFS["download_location"], 
     64    "add_paused": DEFAULT_PREFS["add_paused"] 
     65} 
    4766 
    4867class Torrent: 
     
    7089        # Files dictionary 
    7190        self.files = self.get_files() 
    72         # Set the default file priorities to normal 
    73         self.file_priorities = [1]* len(self.files) 
    74  
     91         
    7592        # Default total_uploaded to 0, this may be changed by the state 
    7693        self.total_uploaded = 0 
    7794 
    78         # Set default auto_managed value 
    79         self.auto_managed = options["auto_managed"] 
    80         if not handle.is_paused(): 
    81             handle.auto_managed(self.auto_managed) 
    82  
     95        # Set the default options 
     96        self.options = OPTIONS 
     97        self.options.update(options) 
     98         
    8399        # We need to keep track if the torrent is finished in the state to prevent 
    84100        # some weird things on state load. 
    85101        self.is_finished = False 
    86102 
    87         # Queueing options 
    88         self.stop_at_ratio = False 
    89         self.stop_ratio = 2.00 
    90         self.remove_at_ratio = False 
    91  
    92         self.move_on_completed = False 
    93         self.move_on_completed_path = deluge.common.get_default_download_dir() 
    94  
    95103        # Load values from state if we have it 
    96         if state is not None: 
     104        if state: 
    97105            # This is for saving the total uploaded between sessions 
    98106            self.total_uploaded = state.total_uploaded 
     
    103111            self.is_finished = state.is_finished 
    104112            # Set the per-torrent queue options 
    105             self.set_stop_at_ratio(state.stop_at_ratio) 
    106             self.set_stop_ratio(state.stop_ratio) 
    107             self.set_remove_at_ratio(state.remove_at_ratio) 
     113            self.options["stop_at_ratio"] = state.stop_at_ratio 
     114            self.options["stop_ratio"] = state.stop_ratio 
     115            self.options["remove_at_ratio"] = state.remove_at_ratio 
    108116        else: 
    109117            # Tracker list 
     
    117125 
    118126        # Various torrent options 
    119         self.set_max_connections(options["max_connections_per_torrent"]) 
    120         self.set_max_upload_slots(options["max_upload_slots_per_torrent"]) 
    121         self.set_max_upload_speed(options["max_upload_speed_per_torrent"]) 
    122         self.set_max_download_speed(options["max_download_speed_per_torrent"]) 
    123         self.set_prioritize_first_last(options["prioritize_first_last_pieces"]) 
    124127        self.handle.resolve_countries(True) 
    125         if options.has_key("file_priorities"): 
    126             self.set_file_priorities(options["file_priorities"]) 
    127  
    128         # Set the allocation mode 
    129         self.compact = options["compact_allocation"] 
    130         # Where the torrent is being saved to 
    131         self.save_path = options["download_location"] 
     128        self.set_options(self.options) 
     129         
    132130        # Status message holds error info about the torrent 
    133131        self.statusmsg = "OK" 
     
    141139        log.debug("Torrent object created.") 
    142140 
    143     def set_tracker_status(self, status): 
    144         """Sets the tracker status""" 
    145         self.tracker_status = status 
    146  
     141    ## Options methods ## 
     142    def set_options(self, options): 
     143        OPTIONS_FUNCS = { 
     144            # Functions used for setting options 
     145            "max_download_speed": self.set_max_download_speed, 
     146            "max_upload_speed": self.set_max_upload_speed, 
     147            "max_connections": self.handle.set_max_connections, 
     148            "max_upload_slots": self.handle.set_max_uploads, 
     149            "prioritize_first_last_pieces": self.set_prioritize_first_last, 
     150            "auto_managed": self.set_auto_managed, 
     151            "file_priorities": self.set_file_priorities, 
     152            "download_location": self.set_save_path, 
     153        } 
     154        for (key, value) in options.items(): 
     155            if OPTIONS_FUNCS.has_key(key): 
     156                OPTIONS_FUNCS[key](value) 
     157         
     158        self.options.update(options) 
     159     
     160    def get_options(self): 
     161        return self.options 
     162         
    147163    def set_max_connections(self, max_connections): 
    148         self.max_connections = int(max_connections) 
    149         self.handle.set_max_connections(self.max_connections) 
     164        self.options["max_connections"] = int(max_connections) 
     165        self.handle.set_max_connections(max_connections) 
    150166 
    151167    def set_max_upload_slots(self, max_slots): 
    152         self.max_upload_slots = int(max_slots) 
    153         self.handle.set_max_uploads(self.max_upload_slots) 
     168        self.options["max_upload_slots"] = int(max_slots) 
     169        self.handle.set_max_uploads(max_slots) 
    154170 
    155171    def set_max_upload_speed(self, m_up_speed): 
    156         self.max_upload_speed = m_up_speed 
     172        self.options["max_upload_speed"] = m_up_speed 
    157173        if m_up_speed < 0: 
    158174            v = -1 
     
    163179 
    164180    def set_max_download_speed(self, m_down_speed): 
    165         self.max_download_speed = m_down_speed 
     181        self.options["max_download_speed"] = m_down_speed 
    166182        if m_down_speed < 0: 
    167183            v = -1 
     
    171187 
    172188    def set_prioritize_first_last(self, prioritize): 
    173         self.prioritize_first_last = prioritize 
    174         if self.prioritize_first_last: 
     189        self.options["prioritize_first_last_pieces"] = prioritize 
     190        if prioritize: 
    175191            if self.handle.get_torrent_info().num_files() == 1: 
    176192                # We only do this if one file is in the torrent 
     
    180196                self.handle.prioritize_pieces(priorities) 
    181197 
    182     def set_save_path(self, save_path): 
    183         self.save_path = save_path 
    184  
    185198    def set_auto_managed(self, auto_managed): 
    186         self.auto_managed = auto_managed 
     199        self.options["auto_managed"] = auto_managed 
    187200        self.handle.auto_managed(auto_managed) 
    188201        self.update_state() 
    189202 
    190203    def set_stop_ratio(self, stop_ratio): 
    191         self.stop_ratio = stop_ratio 
     204        self.options["stop_ratio"] = stop_ratio 
    192205 
    193206    def set_stop_at_ratio(self, stop_at_ratio): 
    194         self.stop_at_ratio = stop_at_ratio 
     207        self.options["stop_at_ratio"] = stop_at_ratio 
    195208 
    196209    def set_remove_at_ratio(self, remove_at_ratio): 
    197         self.remove_at_ratio = remove_at_ratio 
     210        self.options["remove_at_ratio"] = remove_at_ratio 
    198211 
    199212    def set_file_priorities(self, file_priorities): 
     
    205218        self.handle.prioritize_files(file_priorities) 
    206219 
    207         if 0 in self.file_priorities: 
     220        if 0 in self.options["file_priorities"]: 
    208221            # We have previously marked a file 'Do Not Download' 
    209222            # Check to see if we have changed any 0's to >0 and change state accordingly 
    210             for index, priority in enumerate(self.file_priorities): 
     223            for index, priority in enumerate(self.options["file_priorities"]): 
    211224                if priority == 0 and file_priorities[index] > 0: 
    212225                    # We have a changed 'Do Not Download' to a download priority 
     
    215228                    break 
    216229 
    217         self.file_priorities = file_priorities 
     230        self.options["file_priorities"] = file_priorities 
    218231 
    219232        # Set the first/last priorities if needed 
    220         self.set_prioritize_first_last(self.prioritize_first_last) 
    221  
     233        self.set_prioritize_first_last(self.options["prioritize_first_last_pieces"]) 
     234         
    222235    def set_trackers(self, trackers): 
    223236        """Sets trackers""" 
     
    244257            self.force_reannounce() 
    245258 
    246     def set_move_on_completed(self, value): 
    247         self.move_on_completed = value 
    248  
    249     def set_move_on_completed_path(self, value): 
    250         self.move_on_completed_path = value 
     259    ### End Options methods ### 
     260 
     261    def set_save_path(self, save_path): 
     262        self.options["download_location"] = save_path 
     263             
     264    def set_tracker_status(self, status): 
     265        """Sets the tracker status""" 
     266        self.tracker_status = status 
    251267 
    252268    def update_state(self): 
     
    256272        ltstate = int(self.handle.status().state) 
    257273 
    258         log.debug("set_state_based_on_ltstate: %s", ltstate) 
     274        log.debug("set_state_based_on_ltstate: %s", deluge.common.LT_TORRENT_STATE[ltstate]) 
    259275        log.debug("session.is_paused: %s", component.get("Core").session.is_paused()) 
    260276        if ltstate == LTSTATE["Queued"] or ltstate == LTSTATE["Checking"]: 
     
    453469            "trackers": self.trackers, 
    454470            "tracker_status": self.tracker_status, 
    455             "save_path": self.save_path, 
     471            "save_path": self.options["download_location"], 
    456472            "files": self.files, 
    457             "file_priorities": self.file_priorities, 
    458             "compact": self.compact, 
    459             "max_connections": self.max_connections, 
    460             "max_upload_slots": self.max_upload_slots, 
    461             "max_upload_speed": self.max_upload_speed, 
    462             "max_download_speed": self.max_download_speed, 
    463             "prioritize_first_last": self.prioritize_first_last, 
     473            "file_priorities": self.options["file_priorities"], 
     474            "compact": self.options["compact_allocation"], 
     475            "max_connections": self.options["max_connections"], 
     476            "max_upload_slots": self.options["max_upload_slots"], 
     477            "max_upload_speed": self.options["max_upload_speed"], 
     478            "max_download_speed": self.options["max_download_speed"], 
     479            "prioritize_first_last": self.options["prioritize_first_last_pieces"], 
    464480            "message": self.statusmsg, 
    465481            "hash": self.torrent_id, 
     
    467483            "seeding_time": self.status.seeding_time, 
    468484            "seed_rank": self.status.seed_rank, 
    469             "is_auto_managed": self.auto_managed, 
    470             "stop_ratio": self.stop_ratio, 
    471             "stop_at_ratio": self.stop_at_ratio, 
    472             "remove_at_ratio": self.remove_at_ratio, 
    473             "move_on_completed": self.move_on_completed, 
    474             "move_on_completed_path": self.move_on_completed_path 
     485            "is_auto_managed": self.options["auto_managed"], 
     486            "stop_ratio": self.options["stop_ratio"], 
     487            "stop_at_ratio": self.options["stop_at_ratio"], 
     488            "remove_at_ratio": self.options["remove_at_ratio"], 
     489            "move_on_completed": self.options["move_completed"], 
     490            "move_on_completed_path": self.options["move_completed_path"] 
    475491        } 
    476492 
  • trunk/deluge/core/torrentmanager.py

    r3760 r3770  
    4747from deluge.configmanager import ConfigManager 
    4848from deluge.core.torrent import Torrent 
     49from deluge.core.torrent import OPTIONS 
    4950import deluge.core.oldstateupgrader 
    5051 
     
    189190    def update(self): 
    190191        for torrent_id, torrent in self.torrents.items(): 
    191             if self.config["stop_seed_at_ratio"] or torrent.stop_at_ratio: 
     192            if self.config["stop_seed_at_ratio"] or torrent.options["stop_at_ratio"]: 
    192193                stop_ratio = self.config["stop_seed_ratio"] 
    193                 if torrent.stop_at_ratio: 
    194                     stop_ratio = torrent.stop_ratio 
     194                if torrent.options["stop_at_ratio"]: 
     195                    stop_ratio = torrent.options["stop_ratio"] 
    195196                if torrent.get_ratio() >= stop_ratio and torrent.is_finished: 
    196                     if self.config["remove_seed_at_ratio"] or torrent.remove_at_ratio: 
     197                    if self.config["remove_seed_at_ratio"] or torrent.options["remove_at_ratio"]: 
    197198                        self.remove(torrent_id) 
    198199                        break 
     
    254255            except Exception, e: 
    255256                log.error("Unable to decode torrent file!: %s", e) 
    256              
     257 
    257258        if torrent_info is None: 
    258259            # We have no torrent_info so we need to add the torrent with information 
     
    260261 
    261262            # Populate the options dict from state 
    262             options = {} 
    263             options["max_connections_per_torrent"] = state.max_connections 
    264             options["max_upload_slots_per_torrent"] = state.max_upload_slots 
    265             options["max_upload_speed_per_torrent"] = state.max_upload_speed 
    266             options["max_download_speed_per_torrent"] = state.max_download_speed 
     263            options = OPTIONS 
     264            options["max_connections"] = state.max_connections 
     265            options["max_upload_slots"] = state.max_upload_slots 
     266            options["max_upload_speed"] = state.max_upload_speed 
     267            options["max_download_speed"] = state.max_download_speed 
    267268            options["prioritize_first_last_pieces"] = state.prioritize_first_last 
    268269            options["file_priorities"] = state.file_priorities 
     
    283284            # We have a torrent_info object so we're not loading from state. 
    284285            # Check if options is None and load defaults 
    285             options_keys = [ 
    286                 "compact_allocation", 
    287                 "max_connections_per_torrent", 
    288                 "max_upload_slots_per_torrent", 
    289                 "max_upload_speed_per_torrent", 
    290                 "max_download_speed_per_torrent", 
    291                 "prioritize_first_last_pieces", 
    292                 "download_location", 
    293                 "add_paused", 
    294                 "auto_managed" 
    295             ] 
    296  
    297286            if options == None: 
    298                 options = {} 
    299                 for key in options_keys: 
    300                     options[key] = self.config[key] 
     287                options = OPTIONS 
    301288            else: 
    302                 for key in options_keys: 
    303                     if not options.has_key(key): 
    304                         options[key] = self.config[key]             
     289                o = OPTIONS 
     290                o.update(options) 
     291                options = o 
    305292 
    306293            add_torrent_params["ti"] = torrent_info 
     
    506493                torrent.get_status(["total_uploaded"])["total_uploaded"],  
    507494                torrent.trackers, 
    508                 torrent.compact,  
     495                torrent.options["compact_allocation"],  
    509496                paused,  
    510                 torrent.save_path, 
    511                 torrent.max_connections, 
    512                 torrent.max_upload_slots, 
    513                 torrent.max_upload_speed, 
    514                 torrent.max_download_speed, 
    515                 torrent.prioritize_first_last, 
    516                 torrent.file_priorities, 
     497                torrent.options["download_location"], 
     498                torrent.options["max_connections"], 
     499                torrent.options["max_upload_slots"], 
     500                torrent.options["max_upload_speed"], 
     501                torrent.options["max_download_speed"], 
     502                torrent.options["prioritize_first_last_pieces"], 
     503                torrent.options["file_priorities"], 
    517504                torrent.get_queue_position(), 
    518                 torrent.auto_managed, 
     505                torrent.options["auto_managed"], 
    519506                torrent.is_finished, 
    520                 torrent.stop_ratio, 
    521                 torrent.stop_at_ratio, 
    522                 torrent.remove_at_ratio 
     507                torrent.options["stop_ratio"], 
     508                torrent.options["stop_at_ratio"], 
     509                torrent.options["remove_at_ratio"] 
    523510            ) 
    524511            state.torrents.append(torrent_state) 
     
    607594        if not torrent.is_finished: 
    608595            move_path = None 
    609             if torrent.move_on_completed: 
    610                 move_path = torrent.move_on_completed_path 
     596            if torrent.options["move_completed"]: 
     597                move_path = torrent.options["move_completed_path"] 
    611598            elif self.config["move_completed"]: 
    612599                move_path = self.config["move_completed_path"]