~jkakar/commandant/trunk

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
# Commandant is a framework for building command-oriented tools.
# Copyright (C) 2009-2010 Jamshed Kakar.
#
# 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.

"""Infrastructure for building L{HelpTopic} components."""

from inspect import getdoc


class HelpTopic(object):
    """A help topic."""

    controller = None

    def get_summary(self):
        """Get a short topic summary for use in a topic listing."""
        raise NotImplementedError("Must be implemented by sub-class.")

    def get_text(self):
        """Get topic content."""
        raise NotImplementedError("Must be implemented by sub-class.")


class DocstringHelpTopic(object):
    """A help topic that loads content from its docstring."""

    def __init__(self):
        super(DocstringHelpTopic, self).__init__()
        self._summary = None
        self._text = None

    def _get_docstring(self):
        """Get the docstring for this help topic."""
        return getdoc(self)

    def _load_help_text(self):
        """Load summary and text content."""
        docstring = self._get_docstring()
        if not docstring:
            return "", ""
        else:
            return self._load_docstring(docstring)

    def _load_docstring(self, docstring):
        """Load summary and text content from a docstring."""
        lines = [line for line in docstring.splitlines()]
        return lines[0], "\n".join(lines[1:]).strip()

    def get_summary(self):
        """Get a short topic summary for use in a topic listing."""
        if self._summary is None:
            (self._summary, self._text) = self._load_help_text()
        return self._summary

    def get_text(self):
        """Get topic content."""
        if self._text is None:
            (self._summary, self._text) = self._load_help_text()
        return self._text


class FileHelpTopic(HelpTopic):
    """A help topic that loads content from a file."""

    path = None

    def __init__(self):
        super(FileHelpTopic, self).__init__()
        self._summary = None
        self._text = None

    def _load(self):
        """Load and cache summary and text content."""
        file = open(self.path, "r")
        self._summary = file.readline().strip()
        self._text = file.read().strip()
        # FIXME Test this.
        file.close()

    def get_summary(self):
        """Get a short topic summary for use in a topic listing."""
        if self._summary is None:
            self._load()
        return self._summary

    def get_text(self):
        """Get topic content."""
        if self._text is None:
            self._load()
        return self._text


class CommandHelpTopic(DocstringHelpTopic):
    """
    A help topic that loads content from a C{bzrlib.commands.Command}
    docstring.
    """

    def __init__(self, command):
        super(CommandHelpTopic, self).__init__()
        self.command = command

    def _get_docstring(self):
        """Get the docstring for the command."""
        return getdoc(self.command)

    def _load_docstring(self, docstring):
        """Load summary and text content from a docstring."""
        lines = [line for line in docstring.splitlines()]
        summary = lines[0]
        try:
            text = self.command.get_help_text().strip()
            text = text.replace("bzr", self.controller.program_name)
        except NotImplementedError:
            pass
        else:
            return summary, text
        return summary, ""