~qbzr-dev/qbzr/show-merge-depth

« back to all changes in this revision

Viewing changes to lib/plugins.py

  • Committer: Gary van der Merwe
  • Date: 2009-08-19 12:41:14 UTC
  • mfrom: (677.1.243 trunk)
  • Revision ID: garyvdm@gmail.com-20090819124114-jz8zf4o4dlcl0a4r
Merge Trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# QBzr - Qt frontend to Bazaar commands
 
4
# Copyright (C) 2009 Canonical Ltd
 
5
#
 
6
# This program is free software; you can redistribute it and/or
 
7
# modify it under the terms of the GNU General Public License
 
8
# as published by the Free Software Foundation; either version 2
 
9
# of the License, or (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
19
 
 
20
from inspect import getdoc
 
21
 
 
22
from PyQt4 import QtCore, QtGui
 
23
 
 
24
from bzrlib import (
 
25
    _format_version_tuple,
 
26
    osutils,
 
27
    plugin as mod_plugin,
 
28
    )
 
29
 
 
30
from bzrlib.plugins.qbzr.lib.conditional_dataview import (
 
31
    QBzrConditionalDataView,
 
32
    )
 
33
from bzrlib.plugins.qbzr.lib.i18n import gettext
 
34
from bzrlib.plugins.qbzr.lib.util import (
 
35
    BTN_CLOSE,
 
36
    QBzrWindow,
 
37
    )
 
38
 
 
39
 
 
40
class QBzrPluginsWindow(QBzrWindow):
 
41
 
 
42
    def __init__(self, parent=None):
 
43
        QBzrWindow.__init__(self, [], parent)
 
44
        self.set_title(gettext("Plugins"))
 
45
        self.restoreSize("plugins", (400,256))
 
46
        view = self.build_view()
 
47
        btns = self.create_button_box(BTN_CLOSE)
 
48
        layout = QtGui.QVBoxLayout(self.centralWidget())
 
49
        layout.addWidget(view)
 
50
        layout.addWidget(btns)
 
51
        self.refresh_view()
 
52
 
 
53
    def build_view(self):
 
54
        """Build and return the widget displaying the data."""
 
55
        summary_headers = [
 
56
            gettext("Name"),
 
57
            gettext("Version"),
 
58
            gettext("Description"),
 
59
            ]
 
60
        locations_headers = [
 
61
            gettext("Name"),
 
62
            gettext("Directory"),
 
63
            ]
 
64
        footer = gettext("Plugins installed: %(rows)d")
 
65
        details = None
 
66
        self._summary_viewer = QBzrConditionalDataView("tree",
 
67
            summary_headers, footer, details)
 
68
        self._locations_viewer = QBzrConditionalDataView("tree",
 
69
            locations_headers, footer, details)
 
70
        tabs = QtGui.QTabWidget()
 
71
        tabs.addTab(self._summary_viewer, gettext("Summary"))
 
72
        tabs.addTab(self._locations_viewer, gettext("Locations"))
 
73
        return tabs
 
74
 
 
75
    def refresh_view(self):
 
76
        """Update the data in the view."""
 
77
        plugins = mod_plugin.plugins()
 
78
        summary_data = []
 
79
        locations_data = []
 
80
        for name in sorted(plugins):
 
81
            plugin = plugins[name]
 
82
            version = format_plugin_version(plugin)
 
83
            description = format_plugin_description(plugin)
 
84
            directory = osutils.dirname(plugin.path())
 
85
            summary_data.append((name, version, description))
 
86
            locations_data.append((name, directory))
 
87
        self._summary_viewer.setData(summary_data)
 
88
        self._locations_viewer.setData(locations_data)
 
89
 
 
90
 
 
91
def format_plugin_version(plugin):
 
92
    """Return the version of a plugin as a formatted string."""
 
93
    version_info = plugin.version_info()
 
94
    if version_info is None:
 
95
        result = ''
 
96
    else:
 
97
        try:
 
98
            result = _format_version_tuple(version_info)
 
99
        except ValueError:
 
100
            # Version info fails the expected rules.
 
101
            # Format it nicely anyhow.
 
102
            result = ".".join([str(part) for part in version_info])
 
103
    return result
 
104
 
 
105
 
 
106
def format_plugin_description(plugin):
 
107
    d = getdoc(plugin.module)
 
108
    if d:
 
109
        doc = d.split('\n')[0]
 
110
    else:
 
111
        doc = gettext('(no description)')
 
112
    return doc