Ticket #3380: deluge-2.0.3+on_pre_torrent_remove-Extractor_plugin.patch

File deluge-2.0.3+on_pre_torrent_remove-Extractor_plugin.patch, 8.4 KB (added by Lucio Andrés Illanes Albornoz, 4 years ago)
  • plugins/Extractor-0.7.egg/deluge_extractor/core.py

    old new  
    1616import errno
    1717import logging
    1818import os
     19import shutil
     20import traceback
    1921
    2022from twisted.internet.utils import getProcessOutputAndValue
    2123from twisted.python.procutils import which
     
    2830
    2931log = logging.getLogger(__name__)
    3032
    31 DEFAULT_PREFS = {'extract_path': '', 'use_name_folder': True}
     33DEFAULT_PREFS = {'extract_path': '', 'use_name_folder': True, 'auto_delete': False}
    3234
    3335if windows_check():
    3436    win_7z_exes = [
     
    110112        component.get('EventManager').register_event_handler(
    111113            'TorrentFinishedEvent', self._on_torrent_finished
    112114        )
     115        component.get('EventManager').register_event_handler(
     116            'PreTorrentRemovedEvent', self._on_pre_torrent_removed
     117        )
    113118
    114119    def disable(self):
    115120        component.get('EventManager').deregister_event_handler(
    116121            'TorrentFinishedEvent', self._on_torrent_finished
    117122        )
     123        component.get('EventManager').deregister_event_handler(
     124            'PreTorrentRemovedEvent', self._on_pre_torrent_removed
     125        )
    118126
    119127    def update(self):
    120128        pass
    121129
     130    def _check_torrent_file(self, f):
     131        checkfl, file_ext = False, None
     132        file_root, file_ext = os.path.splitext(f['path'])
     133        file_ext_sec = os.path.splitext(file_root)[1]
     134        if file_ext_sec and file_ext_sec + file_ext in EXTRACT_COMMANDS:
     135            checkfl, file_ext = True, file_ext_sec + file_ext
     136        elif file_ext not in EXTRACT_COMMANDS or file_ext_sec == '.tar':
     137            log.debug('Cannot extract file with unknown file type: %s', f['path'])
     138        elif file_ext == '.rar' and 'part' in file_ext_sec:
     139            part_num = file_ext_sec.split('part')[1]
     140            if part_num.isdigit() and int(part_num) != 1:
     141                log.debug('Skipping remaining multi-part rar files: %s', f['path'])
     142            else:
     143                checkfl = True
     144        else:
     145            checkfl = True
     146        return checkfl, file_ext
     147
     148    def _check_torrent_files(self, tid):
     149        files = tid.get_files()
     150        for f in files:
     151            checkfl, file_ext = self._check_torrent_file(f)
     152            if not checkfl:
     153                continue
     154            else:
     155                return True
     156        return False
     157
    122158    def _on_torrent_finished(self, torrent_id):
    123159        """
    124160        This is called when a torrent finishes and checks if any files to extract.
    125161        """
    126162        tid = component.get('TorrentManager').torrents[torrent_id]
    127163        tid_status = tid.get_status(['download_location', 'name'])
    128 
    129164        files = tid.get_files()
    130165        for f in files:
    131             file_root, file_ext = os.path.splitext(f['path'])
    132             file_ext_sec = os.path.splitext(file_root)[1]
    133             if file_ext_sec and file_ext_sec + file_ext in EXTRACT_COMMANDS:
    134                 file_ext = file_ext_sec + file_ext
    135             elif file_ext not in EXTRACT_COMMANDS or file_ext_sec == '.tar':
    136                 log.debug('Cannot extract file with unknown file type: %s', f['path'])
     166            checkfl, file_ext = self._check_torrent_file(f)
     167            if not checkfl:
    137168                continue
    138             elif file_ext == '.rar' and 'part' in file_ext_sec:
    139                 part_num = file_ext_sec.split('part')[1]
    140                 if part_num.isdigit() and int(part_num) != 1:
    141                     log.debug('Skipping remaining multi-part rar files: %s', f['path'])
    142                     continue
    143 
    144169            cmd = EXTRACT_COMMANDS[file_ext]
    145170            fpath = os.path.join(
    146171                tid_status['download_location'], os.path.normpath(f['path'])
     
    179204            )
    180205            d.addCallback(on_extract, torrent_id, fpath)
    181206
     207    def _on_pre_torrent_removed(self, torrent_id):
     208        """
     209        This is called when a torrent is removed.
     210        """
     211        if self.config['auto_delete']:
     212            tid = component.get('TorrentManager').torrents[torrent_id]
     213            tid_status = tid.get_status(['download_location', 'name'])
     214            if self._check_torrent_files(tid):
     215                dest = os.path.normpath(self.config['extract_path'])
     216                dest = os.path.join(dest, tid_status['name'])
     217                if os.path.isdir(dest):
     218                    def on_rmtree_error(function, path, excinfo):
     219                        log.error(
     220                            'Removing previously extracted files for torrent %s in %s failed: %s',
     221                            torrent_id,
     222                            dest,
     223                            traceback.format_exception(*excinfo),
     224                        )
     225
     226                    log.debug(
     227                        'Removing previously extracted files for torrent %s in %s',
     228                        torrent_id,
     229                        dest,
     230                    )
     231                    shutil.rmtree(dest, onerror=on_rmtree_error)
     232
    182233    @export
    183234    def set_config(self, config):
    184235        """Sets the config dictionary."""
  • plugins/Extractor-0.7.egg/deluge_extractor/data/extractor.js

    old new  
    5656            boxLabel: _('Create torrent name sub-folder'),
    5757        });
    5858
     59        this.auto_delete = fieldset.add({
     60            xtype: 'checkbox',
     61            name: 'auto_delete',
     62            height: 22,
     63            hideLabel: true,
     64            boxLabel: _('Auto-delete previously extracted files on deletion'),
     65        });
     66
    5967        this.on('show', this.updateConfig, this);
    6068    },
    6169
     
    6573
    6674        config['extract_path'] = this.extract_path.getValue();
    6775        config['use_name_folder'] = this.use_name_folder.getValue();
     76        config['auto_delete'] = this.auto_delete.getValue();
    6877
    6978        deluge.client.extractor.set_config(config);
    7079    },
     
    7887            success: function(config) {
    7988                this.extract_path.setValue(config['extract_path']);
    8089                this.use_name_folder.setValue(config['use_name_folder']);
     90                this.auto_delete.setValue(config['auto_delete']);
    8191            },
    8292            scope: this,
    8393        });
  • plugins/Extractor-0.7.egg/deluge_extractor/data/extractor_prefs.ui

    old new  
    100100                    <property name="position">1</property>
    101101                  </packing>
    102102                </child>
     103                <child>
     104                  <object class="GtkCheckButton" id="chk_auto_delete">
     105                    <property name="label" translatable="yes">Auto-delete previously extracted files on deletion</property>
     106                    <property name="visible">True</property>
     107                    <property name="can_focus">True</property>
     108                    <property name="receives_default">False</property>
     109                    <property name="tooltip_text" translatable="yes">This option will automatically delete previously extracted files on torrent deletion.</property>
     110                    <property name="draw_indicator">True</property>
     111                  </object>
     112                  <packing>
     113                    <property name="expand">False</property>
     114                    <property name="fill">False</property>
     115                    <property name="position">1</property>
     116                  </packing>
     117                </child>
    103118              </object>
    104119            </child>
    105120            <child type="label">
  • plugins/Extractor-0.7.egg/deluge_extractor/gtkui.py

    old new  
    6969        config = {
    7070            'extract_path': path,
    7171            'use_name_folder': self.builder.get_object('chk_use_name').get_active(),
     72            'auto_delete': self.builder.get_object('chk_auto_delete').get_active(),
    7273        }
    7374
    7475        client.extractor.set_config(config)
     
    9293            self.builder.get_object('chk_use_name').set_active(
    9394                config['use_name_folder']
    9495            )
     96            self.builder.get_object('chk_auto_delete').set_active(
     97                config['auto_delete']
     98            )
    9599
    96100        client.extractor.get_config().addCallback(on_get_config)