summaryrefslogtreecommitdiffstats
path: root/deluge/ui/console/cmdline/commands/plugin.py
blob: 72cecb40f6d3dc47c4782cba3f3d0b201cfe476b (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- 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 shutil
            from base64 import b64encode

            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 = b64encode(_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
        )