~dylanmccall/update-manager/dialogs-refactor

« back to all changes in this revision

Viewing changes to janitor/plugincore/plugin.py

  • Committer: Barry Warsaw
  • Date: 2012-06-11 15:08:35 UTC
  • mto: (2446.1.6 py3)
  • mto: This revision was merged to the branch mainline in revision 2454.
  • Revision ID: barry@python.org-20120611150835-lbx9cjwzfjy3y7bw
Re-integrate the Python 3 Computer Janitor refactoring branch for now.  The
code itself lives in ~computer-janitor-hackers/computer-janitor/py3refactor
and will eventually be ripped out of here, but for now, this unblocks other
folks.

Still getting test failures in `bzr bd -S` though.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2012  Canonical, Ltd.
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify it
 
4
# under the terms of the GNU General Public License as published by the Free
 
5
# Software Foundation, version 3 of the License.
 
6
#
 
7
# This program is distributed in the hope that it will be useful, but WITHOUT
 
8
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
9
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
10
# more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License along with
 
13
# this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
 
 
15
 
 
16
from __future__ import absolute_import, print_function, unicode_literals
 
17
 
 
18
__metaclass__ = type
 
19
__all__ = [
 
20
    'Plugin',
 
21
    ]
 
22
 
 
23
 
 
24
from janitor.plugincore.exceptions import UnimplementedMethod
 
25
 
 
26
 
 
27
class Plugin:
 
28
    """Base class for plugins.
 
29
 
 
30
    These plugins only do one thing: identify cruft. See the 'get_cruft'
 
31
    method for details.
 
32
    """
 
33
 
 
34
    # XXX BAW 2012-06-08: For historical reasons, we do not set
 
35
    # self._condition or self.app in a constructor.  This needs to be fixed.
 
36
 
 
37
    @property
 
38
    def condition(self):
 
39
        return (self._condition if hasattr(self, '_condition') else [])
 
40
 
 
41
    @condition.setter
 
42
    def condition(self, condition):
 
43
        self._condition = condition
 
44
 
 
45
    def set_application(self, app):
 
46
        """Set the Application instance this plugin belongs to."""
 
47
        self.app = app
 
48
 
 
49
    def do_cleanup_cruft(self):
 
50
        """Find cruft and clean it up.
 
51
 
 
52
        This is a helper method.
 
53
        """
 
54
        for cruft in self.get_cruft():
 
55
            cruft.cleanup()
 
56
        self.post_cleanup()
 
57
 
 
58
    def get_cruft(self):
 
59
        """Find some cruft in the system.
 
60
 
 
61
        This method MUST return an iterator (see 'yield' statement).
 
62
        This interface design allows cruft to be collected piecemeal,
 
63
        which makes it easier to show progress in the user interface.
 
64
 
 
65
        The base class default implementation of this raises an
 
66
        exception. Subclasses MUST override this method.
 
67
        """
 
68
        raise UnimplementedMethod(self.get_cruft)
 
69
 
 
70
    @property
 
71
    def cruft(self):
 
72
        for cruft in self.get_cruft():
 
73
            yield cruft
 
74
 
 
75
    def post_cleanup(self):
 
76
        """Do plugin-wide cleanup after the individual cleanup was performed.
 
77
 
 
78
        This is useful for stuff that needs to be processed in batches
 
79
        (e.g. for performance reasons) like package removal.
 
80
        """
 
81
        pass