summaryrefslogtreecommitdiffstats
path: root/deluge/ui/console/cmdline/commands/plugin.py
blob: dd910d9dab3ffc05bf7e6253e4ee9beffcf0ee57 (plain)
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
# -*- coding: utf-8 -*-
#
# 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 __future__ import unicode_literals

import deluge.component as component
import deluge.configmanager
from deluge.ui.client import client

from . import BaseCommand


class Command(BaseCommand):
    """Manage plugins"""

    def add_arguments(self, parser):
        parser.add_argument(
            '-l', '--list', action='store_true', default=False, dest='list',
            help=_('Lists available plugins'),
        )
        parser.add_argument(
            '-s', '--show', action='store_true', default=False, dest='show',
            help=_('Shows enabled plugins'),
        )
        parser.add_argument('-e', '--enable', dest='enable', nargs='+', help=_('Enables a plugin'))
        parser.add_argument('-d', '--disable', dest='disable', nargs='+', help=_('Disables a plugin'))
        parser.add_argument(
            '-r', '--reload', action='store_true', default=False, dest='reload',
            help=_('Reload list of available plugins'),
        )
        parser.add_argument('-i', '--install', help=_('Install a plugin from an .egg file'))

    def handle(self, options):
        self.console = component.get('ConsoleUI')

        if options.reload:
            client.core.pluginmanager.rescan_plugins()
            self.console.write('{!green!}Plugin list successfully reloaded')
            return

        elif options.list:
            def on_available_plugins(result):
                self.console.write('{!info!}Available Plugins:')
                for p in result:
                    self.console.write('{!input!}  ' + p)

            return client.core.get_available_plugins().addCallback(on_available_plugins)

        elif options.show:
            def on_enabled_plugins(result):
                self.console.write('{!info!}Enabled Plugins:')
                for p in result:
                    self.console.write('{!input!}  ' + p)

            return client.core.get_enabled_plugins().addCallback(on_enabled_plugins)

        elif options.enable:
            def on_available_plugins(result):
                plugins = {}
                for p in result:
                    plugins[p.lower()] = p
                for arg in options.enable:
                    if arg.lower() in plugins:
                        client.core.enable_plugin(plugins[arg.lower()])

            return client.core.get_available_plugins().addCallback(on_available_plugins)

        elif options.disable:
            def on_enabled_plugins(result):
                plugins = {}
                for p in result:
                    plugins[p.lower()] = p
                for arg in options.disable:
                    if arg.lower() in plugins:
                        client.core.disable_plugin(plugins[arg.lower()])

            return client.core.get_enabled_plugins().addCallback(on_enabled_plugins)

        elif options.install:
            import os.path
            import base64
            import shutil

            filepath = options.install

            if not os.path.exists(filepath):
                self.console.write('{!error!}Invalid path: %s' % filepath)
                return

            config_dir = deluge.configmanager.get_config_dir()
            filename = os.path.split(filepath)[1]
            shutil.copyfile(filepath, os.path.join(config_dir, 'plugins', filename))

            client.core.rescan_plugins()

            if not client.is_localhost():
                # We need to send this plugin to the daemon
                with open(filepath, 'rb') as _file:
                    filedump = base64.encodestring(_file.read())
                try:
                    client.core.upload_plugin(filename, filedump)
                    client.core.rescan_plugins()
                except Exception:
                    self.console.write('{!error!}An error occurred, plugin was not installed')

            self.console.write('{!green!}Plugin was successfully installed: %s' % filename)

    def complete(self, line):
        return component.get('ConsoleUI').tab_complete_path(line, ext='.egg', sort='name', dirs_first=-1)