Ticket #1549: deluge-console-add-gio.patch

File deluge-console-add-gio.patch, 1.7 KB (added by mathbr, 13 years ago)

Use Gio to process the file argument

  • deluge/ui/console/commands/add.py

    diff --git a/deluge/ui/console/commands/add.py b/deluge/ui/console/commands/add.py
    index 7878438..6583d6f 100644
    a b  
    4242
    4343from optparse import make_option
    4444import os
    45 import base64
     45import base6
     46import gio
    4647
    4748class Command(BaseCommand):
    4849    """Add a torrent"""
    def handle(self, *args, **options):  
    6364        # Keep a list of deferreds to make a DeferredList
    6465        deferreds = []
    6566        for arg in args:
    66             if not os.path.exists(arg):
     67            filename = os.path.split(arg)[-1]
     68            gfile = gio.File(arg)
     69
     70            if not gfile.query_exists():
    6771                self.console.write("{!error!}%s doesn't exist!" % arg)
    6872                continue
    69             if not os.path.isfile(arg):
     73            if gfile.query_file_type(gio.FILE_QUERY_INFO_NONE) is gio.FILE_TYPE_DIRECTORY:
    7074                self.console.write("{!error!}This is a directory!")
    7175                continue
    72             self.console.write("{!info!}Attempting to add torrent: %s" % arg)
    73             filename = os.path.split(arg)[-1]
    74             filedump = base64.encodestring(open(arg, "rb").read())
     76            self.console.write("{!info!}Attempting to add torrent: %s" % arg
     77
     78            try:
     79                stream = gfile.read()
     80            except gio.Error:
     81                self.console.write("{!error!}Torrent was not added! Error while reading stream.")
     82            else:
     83                filedump = base64.encodestring(stream.read())
     84            finally:
     85                stream.close())
    7586
    7687            def on_success(result):
    7788                self.console.write("{!success!}Torrent added!")