~bzr-pqm/bzr/bzr.dev

2070.4.5 by John Arbash Meinel
cleanup copyright line
1
# Copyright (C) 2006 Canonical Ltd
2023.1.2 by ghigo
add help topics module
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""A collection of extra help information for using bzr.
18
19
Help topics are meant to be help for items that aren't commands, but will
20
help bzr become fully learnable without referring to a tutorial.
21
"""
22
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
23
from bzrlib import registry
24
25
26
class HelpTopicRegistry(registry.Registry):
2070.4.15 by John Arbash Meinel
Fixes from Martin
27
    """A Registry customized for handling help topics."""
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
28
29
    def register(self, topic, detail, summary):
30
        """Register a new help topic.
31
32
        :param topic: Name of documentation entry
33
        :param detail: Function or string object providing detailed
34
            documentation for topic.  Function interface is detail(topic).
35
            This should return a text string of the detailed information.
36
        :param summary: String providing single-line documentation for topic.
37
        """
38
        # The detail is stored as the 'object' and the 
39
        super(HelpTopicRegistry, self).register(topic, detail, info=summary)
40
41
    def register_lazy(self, topic, module_name, member_name, summary):
42
        """Register a new help topic, and import the details on demand.
43
44
        :param topic: Name of documentation entry
45
        :param module_name: The module to find the detailed help.
46
        :param member_name: The member of the module to use for detailed help.
47
        :param summary: String providing single-line documentation for topic.
48
        """
2070.4.15 by John Arbash Meinel
Fixes from Martin
49
        super(HelpTopicRegistry, self).register_lazy(topic, module_name,
50
                                                     member_name, info=summary)
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
51
52
    def get_detail(self, topic):
53
        """Get the detailed help on a given topic."""
54
        obj = self.get(topic)
55
        if callable(obj):
56
            return obj(topic)
57
        else:
58
            return obj
59
60
    def get_summary(self, topic):
61
        """Get the single line summary for the topic."""
62
        return self.get_info(topic)
63
64
65
topic_registry = HelpTopicRegistry()
2023.1.2 by ghigo
add help topics module
66
67
68
#----------------------------------------------------
69
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
70
def _help_on_topics(dummy):
2023.1.2 by ghigo
add help topics module
71
    """Write out the help for topics to outfile"""
72
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
73
    topics = topic_registry.keys()
2070.4.2 by John Arbash Meinel
cleanup help_topics.py
74
    lmax = max(len(topic) for topic in topics)
2023.1.4 by ghigo
the ''bzr help topics'' output is shorter
75
        
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
76
    out = []
2023.1.2 by ghigo
add help topics module
77
    for topic in topics:
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
78
        summary = topic_registry.get_summary(topic)
79
        out.append("%-*s %s\n" % (lmax, topic, summary))
80
    return ''.join(out)
81
82
83
def _help_on_revisionspec(name):
2070.4.7 by ghigo
Updates on the basis of the Richard Wilbur suggestions
84
    """"Write the summary help for all documented topics to outfile."""
2023.1.2 by ghigo
add help topics module
85
    import bzrlib.revisionspec
86
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
87
    out = []
88
    out.append("\nRevision prefix specifier:"
89
               "\n--------------------------\n")
2023.1.2 by ghigo
add help topics module
90
91
    for i in bzrlib.revisionspec.SPEC_TYPES:
2070.4.14 by John Arbash Meinel
Switch revisionspec to use the help defined as help_txt rather than the doc string
92
        doc = i.help_txt
93
        if doc == bzrlib.revisionspec.RevisionSpec.help_txt:
2023.1.2 by ghigo
add help topics module
94
            doc = "N/A\n"
2070.4.2 by John Arbash Meinel
cleanup help_topics.py
95
        while (doc[-2:] == '\n\n' or doc[-1:] == ' '):
2023.1.2 by ghigo
add help topics module
96
            doc = doc[:-1]
97
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
98
        out.append("  %s %s\n\n" % (i.prefix, doc))
99
100
    return ''.join(out)
2023.1.2 by ghigo
add help topics module
101
102
103
_basic_help= \
104
"""Bazaar -- a free distributed version-control tool
105
http://bazaar-vcs.org/
106
107
Basic commands:
108
109
  bzr init           makes this directory a versioned branch
110
  bzr branch         make a copy of another branch
111
112
  bzr add            make files or directories versioned
113
  bzr ignore         ignore a file or pattern
114
  bzr mv             move or rename a versioned file
115
116
  bzr status         summarize changes in working copy
117
  bzr diff           show detailed diffs
118
119
  bzr merge          pull in changes from another branch
120
  bzr commit         save some or all changes
121
122
  bzr log            show history of changes
123
  bzr check          validate storage
124
125
  bzr help init      more help on e.g. init command
126
  bzr help commands  list all commands
127
  bzr help topics    list all help topics
128
"""
129
130
2070.4.13 by John Arbash Meinel
Switch help_topics to use a Registry.
131
topic_registry.register("revisionspec", _help_on_revisionspec,
132
                        "Explain how to use --revision")
133
topic_registry.register('basic', _basic_help, "Basic commands")
134
topic_registry.register('topics', _help_on_topics, "Topics list")
2204.4.1 by Aaron Bentley
Add 'formats' help topic
135
def get_format_topic(topic):
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
136
    from bzrlib import bzrdir
2204.4.1 by Aaron Bentley
Add 'formats' help topic
137
    return bzrdir.format_registry.help_topic(topic)
2204.4.10 by Aaron Bentley
Capitalize 'D' in 'directory formats'
138
topic_registry.register('formats', get_format_topic, 'Directory formats')