summaryrefslogtreecommitdiff
path: root/cliapp/plugin.py
blob: 8d9c167f2c1c492a116597944e160156f80a784b (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
# Copyright (C) 2009-2012  Lars Wirzenius
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


'''A generic plugin manager.

The plugin manager finds files with plugins and loads them. It looks
for plugins in a number of locations specified by the caller. To add
a plugin to be loaded, it is enough to put it in one of the locations,
and name it *_plugin.py. (The naming convention is to allow having
other modules as well, such as unit tests, in the same locations.)

'''


class Plugin(object):

    '''Base class for plugins.

    A plugin MUST NOT have any side effects when it is instantiated.
    This is necessary so that it can be safely loaded by unit tests,
    and so that a user interface can allow the user to disable it,
    even if it is installed, with no ill effects. Any side effects
    that would normally happen should occur in the enable() method,
    and be undone by the disable() method. These methods must be
    callable any number of times.

    The subclass MAY define the following attributes:

    * name
    * description
    * version
    * required_application_version

    name is the user-visible identifier for the plugin. It defaults
    to the plugin's classname.

    description is the user-visible description of the plugin. It may
    be arbitrarily long, and can use pango markup language. Defaults
    to the empty string.

    version is the plugin version. Defaults to '0.0.0'. It MUST be a
    sequence of integers separated by periods. If several plugins with
    the same name are found, the newest version is used. Versions are
    compared integer by integer, starting with the first one, and a
    missing integer treated as a zero. If two plugins have the same
    version, either might be used.

    required_application_version gives the version of the minimal
    application version the plugin is written for. The first integer
    must match exactly: if the application is version 2.3.4, the
    plugin's required_application_version must be at least 2 and
    at most 2.3.4 to be loaded. Defaults to 0.

    '''

    @property
    def name(self):
        return self.__class__.__name__

    @property
    def description(self):
        return ''

    @property
    def version(self):
        return '0.0.0'

    @property
    def required_application_version(self):
        return '0.0.0'

    def setup(self):
        '''Setup plugin.

        This is called at plugin load time. It should not yet enable the
        plugin (the ``enable`` method does that), but it might do things
        like add itself into a hook that adds command line arguments
        to the application.

        '''

    def enable_wrapper(self):
        '''Enable plugin.

        The plugin manager will call this method, which then calls the
        enable method. Plugins should implement the enable method.
        The wrapper method is there to allow an application to provide
        an extended base class that does some application specific
        magic when plugins are enabled or disabled.

        '''

        self.enable()

    def disable_wrapper(self):
        '''Corresponds to enable_wrapper, but for disabling a plugin.'''
        self.disable()

    def enable(self):
        '''Enable the plugin.'''
        raise NotImplementedError()

    def disable(self):  # pragma: no cover
        '''Disable the plugin.'''
        pass