/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/help_topics.py

  • Committer: Vincent Ladeuil
  • Date: 2007-11-14 08:20:59 UTC
  • mfrom: (2990 +trunk)
  • mto: (2990.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 2991.
  • Revision ID: v.ladeuil+lp@free.fr-20071114082059-bx03o3laiqfnyleb
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Canonical Ltd
 
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
Limited formatting of help text is permitted to make the text useful
 
23
both within the reference manual (reStructuredText) and on the screen.
 
24
The help text should be reStructuredText with formatting kept to a
 
25
minimum and, in particular, no headings. The onscreen renderer applies
 
26
the following simple rules before rendering the text:
 
27
 
 
28
    1. A '::' appearing on the end of a line is replaced with ':'.
 
29
    2. Lines starting with a ':' have it stripped.
 
30
 
 
31
These rules mean that literal blocks and field lists respectively can
 
32
be used in the help text, producing sensible input to a manual while
 
33
rendering on the screen naturally.
 
34
"""
 
35
 
 
36
from bzrlib import registry
 
37
 
 
38
 
 
39
# Section identifiers (map topics to the right place in the manual)
 
40
SECT_COMMAND = "command"
 
41
SECT_CONCEPT = "concept"
 
42
SECT_HIDDEN =  "hidden"
 
43
SECT_LIST    = "list"
 
44
SECT_PLUGIN  = "plugin"
 
45
 
 
46
 
 
47
class HelpTopicRegistry(registry.Registry):
 
48
    """A Registry customized for handling help topics."""
 
49
 
 
50
    def register(self, topic, detail, summary, section=SECT_LIST):
 
51
        """Register a new help topic.
 
52
 
 
53
        :param topic: Name of documentation entry
 
54
        :param detail: Function or string object providing detailed
 
55
            documentation for topic.  Function interface is detail(topic).
 
56
            This should return a text string of the detailed information.
 
57
            See the module documentation for details on help text formatting.
 
58
        :param summary: String providing single-line documentation for topic.
 
59
        :param section: Section in reference manual - see SECT_* identifiers.
 
60
        """
 
61
        # The detail is stored as the 'object' and the metadata as the info
 
62
        info=(summary,section)
 
63
        super(HelpTopicRegistry, self).register(topic, detail, info=info)
 
64
 
 
65
    def register_lazy(self, topic, module_name, member_name, summary,
 
66
                      section=SECT_LIST):
 
67
        """Register a new help topic, and import the details on demand.
 
68
 
 
69
        :param topic: Name of documentation entry
 
70
        :param module_name: The module to find the detailed help.
 
71
        :param member_name: The member of the module to use for detailed help.
 
72
        :param summary: String providing single-line documentation for topic.
 
73
        :param section: Section in reference manual - see SECT_* identifiers.
 
74
        """
 
75
        # The detail is stored as the 'object' and the metadata as the info
 
76
        info=(summary,section)
 
77
        super(HelpTopicRegistry, self).register_lazy(topic, module_name,
 
78
                                                     member_name, info=info)
 
79
 
 
80
    def get_detail(self, topic):
 
81
        """Get the detailed help on a given topic."""
 
82
        obj = self.get(topic)
 
83
        if callable(obj):
 
84
            return obj(topic)
 
85
        else:
 
86
            return obj
 
87
 
 
88
    def get_summary(self, topic):
 
89
        """Get the single line summary for the topic."""
 
90
        info = self.get_info(topic)
 
91
        if info is None:
 
92
            return None
 
93
        else:
 
94
            return info[0]
 
95
 
 
96
    def get_section(self, topic):
 
97
        """Get the section for the topic."""
 
98
        info = self.get_info(topic)
 
99
        if info is None:
 
100
            return None
 
101
        else:
 
102
            return info[1]
 
103
 
 
104
    def get_topics_for_section(self, section):
 
105
        """Get the set of topics in a section."""
 
106
        result = set()
 
107
        for topic in self.keys():
 
108
            if section == self.get_section(topic):
 
109
                result.add(topic)
 
110
        return result
 
111
 
 
112
 
 
113
topic_registry = HelpTopicRegistry()
 
114
 
 
115
 
 
116
#----------------------------------------------------
 
117
 
 
118
def _help_on_topics(dummy):
 
119
    """Write out the help for topics to outfile"""
 
120
 
 
121
    topics = topic_registry.keys()
 
122
    lmax = max(len(topic) for topic in topics)
 
123
        
 
124
    out = []
 
125
    for topic in topics:
 
126
        summary = topic_registry.get_summary(topic)
 
127
        out.append("%-*s %s\n" % (lmax, topic, summary))
 
128
    return ''.join(out)
 
129
 
 
130
 
 
131
def _help_on_revisionspec(name):
 
132
    """Generate the help for revision specs."""
 
133
    import re
 
134
    import bzrlib.revisionspec
 
135
 
 
136
    out = []
 
137
    out.append("Revision Identifiers\n")
 
138
    out.append("A revision, or a range bound, can be one of the following.\n")
 
139
    details = []
 
140
    details.append("\nFurther details are given below.\n")
 
141
 
 
142
    # The help text is indented 4 spaces - this re cleans that up below
 
143
    indent_re = re.compile(r'^    ', re.MULTILINE)
 
144
    for i in bzrlib.revisionspec.SPEC_TYPES:
 
145
        doc = i.help_txt
 
146
        if doc == bzrlib.revisionspec.RevisionSpec.help_txt:
 
147
            summary = "N/A"
 
148
            doc = summary + "\n"
 
149
        else:
 
150
            # Extract out the top line summary from the body and
 
151
            # clean-up the unwanted whitespace
 
152
            summary,doc = doc.split("\n", 1)
 
153
            #doc = indent_re.sub('', doc)
 
154
            while (doc[-2:] == '\n\n' or doc[-1:] == ' '):
 
155
                doc = doc[:-1]
 
156
        
 
157
        # Note: The leading : here are HACKs to get reStructuredText
 
158
        # 'field' formatting - we know that the prefix ends in a ':'.
 
159
        out.append(":%s\n\t%s" % (i.prefix, summary))
 
160
        details.append(":%s\n%s" % (i.prefix, doc))
 
161
 
 
162
    return '\n'.join(out + details)
 
163
 
 
164
 
 
165
def _help_on_transport(name):
 
166
    from bzrlib.transport import (
 
167
        transport_list_registry,
 
168
    )
 
169
    import textwrap
 
170
 
 
171
    def add_string(proto, help, maxl, prefix_width=20):
 
172
       help_lines = textwrap.wrap(help, maxl - prefix_width)
 
173
       line_with_indent = '\n' + ' ' * prefix_width
 
174
       help_text = line_with_indent.join(help_lines)
 
175
       return "%-20s%s\n" % (proto, help_text)
 
176
 
 
177
    def sort_func(a,b):
 
178
        a1 = a[:a.rfind("://")]
 
179
        b1 = b[:b.rfind("://")]
 
180
        if a1>b1:
 
181
            return +1
 
182
        elif a1<b1:
 
183
            return -1
 
184
        else:
 
185
            return 0
 
186
 
 
187
    protl = []
 
188
    decl = []
 
189
    protos = transport_list_registry.keys( )
 
190
    protos.sort(sort_func)
 
191
    for proto in protos:
 
192
        shorthelp = transport_list_registry.get_help(proto)
 
193
        if not shorthelp:
 
194
            continue
 
195
        if proto.endswith("://"):
 
196
            protl.append(add_string(proto, shorthelp, 79))
 
197
        else:
 
198
            decl.append(add_string(proto, shorthelp, 79))
 
199
 
 
200
 
 
201
    out = "URL Identifiers\n\n" + \
 
202
            "Supported URL prefixes::\n\n  " + \
 
203
            '  '.join(protl)
 
204
 
 
205
    if len(decl):
 
206
        out += "\nSupported modifiers::\n\n  " + \
 
207
            '  '.join(decl)
 
208
 
 
209
    return out
 
210
 
 
211
 
 
212
_basic_help = \
 
213
"""Bazaar -- a free distributed version-control tool
 
214
http://bazaar-vcs.org/
 
215
 
 
216
Basic commands:
 
217
  bzr init           makes this directory a versioned branch
 
218
  bzr branch         make a copy of another branch
 
219
 
 
220
  bzr add            make files or directories versioned
 
221
  bzr ignore         ignore a file or pattern
 
222
  bzr mv             move or rename a versioned file
 
223
 
 
224
  bzr status         summarize changes in working copy
 
225
  bzr diff           show detailed diffs
 
226
 
 
227
  bzr merge          pull in changes from another branch
 
228
  bzr commit         save some or all changes
 
229
 
 
230
  bzr log            show history of changes
 
231
  bzr check          validate storage
 
232
 
 
233
  bzr help init      more help on e.g. init command
 
234
  bzr help commands  list all commands
 
235
  bzr help topics    list all help topics
 
236
"""
 
237
 
 
238
 
 
239
_global_options = \
 
240
"""Global Options
 
241
 
 
242
These options may be used with any command, and may appear in front of any
 
243
command.  (e.g. "bzr --profile help").
 
244
 
 
245
--version      Print the version number. Must be supplied before the command.
 
246
--no-aliases   Do not process command aliases when running this command.
 
247
--builtin      Use the built-in version of a command, not the plugin version.
 
248
               This does not suppress other plugin effects.
 
249
--no-plugins   Do not process any plugins.
 
250
 
 
251
--profile      Profile execution using the hotshot profiler.
 
252
--lsprof       Profile execution using the lsprof profiler.
 
253
--lsprof-file  Profile execution using the lsprof profiler, and write the
 
254
               results to a specified file.  If the filename ends with ".txt",
 
255
               text format will be used.  If the filename either starts with
 
256
               "callgrind.out" or end with ".callgrind", the output will be
 
257
               formatted for use with KCacheGrind. Otherwise, the output
 
258
               will be a pickle.
 
259
 
 
260
See doc/developers/profiling.txt for more information on profiling.
 
261
A number of debug flags are also available to assist troubleshooting and
 
262
development.
 
263
 
 
264
-Dauth         Trace authentication sections used.
 
265
-Derror        Instead of normal error handling, always print a traceback on
 
266
               error.
 
267
-Devil         Capture call sites that do expensive or badly-scaling
 
268
               operations.
 
269
-Dhashcache    Log every time a working file is read to determine its hash.
 
270
-Dhooks        Trace hook execution.
 
271
-Dhpss         Trace smart protocol requests and responses.
 
272
-Dindex        Trace major index operations.
 
273
-Dlock         Trace when lockdir locks are taken or released.
 
274
"""
 
275
 
 
276
_standard_options = \
 
277
"""Standard Options
 
278
 
 
279
Standard options are legal for all commands.
 
280
      
 
281
--help, -h     Show help message.
 
282
--verbose, -v  Display more information.
 
283
--quiet, -q    Only display errors and warnings.
 
284
 
 
285
Unlike global options, standard options can be used in aliases.
 
286
"""
 
287
 
 
288
 
 
289
_checkouts = \
 
290
"""Checkouts
 
291
 
 
292
Checkouts are source trees that are connected to a branch, so that when
 
293
you commit in the source tree, the commit goes into that branch.  They
 
294
allow you to use a simpler, more centralized workflow, ignoring some of
 
295
Bazaar's decentralized features until you want them. Using checkouts
 
296
with shared repositories is very similar to working with SVN or CVS, but
 
297
doesn't have the same restrictions.  And using checkouts still allows
 
298
others working on the project to use whatever workflow they like.
 
299
 
 
300
A checkout is created with the bzr checkout command (see "help checkout").
 
301
You pass it a reference to another branch, and it will create a local copy
 
302
for you that still contains a reference to the branch you created the
 
303
checkout from (the master branch). Then if you make any commits they will be
 
304
made on the other branch first. This creates an instant mirror of your work, or
 
305
facilitates lockstep development, where each developer is working together,
 
306
continuously integrating the changes of others.
 
307
 
 
308
However the checkout is still a first class branch in Bazaar terms, so that
 
309
you have the full history locally.  As you have a first class branch you can
 
310
also commit locally if you want, for instance due to the temporary loss af a
 
311
network connection. Use the --local option to commit to do this. All the local
 
312
commits will then be made on the master branch the next time you do a non-local
 
313
commit.
 
314
 
 
315
If you are using a checkout from a shared branch you will periodically want to
 
316
pull in all the changes made by others. This is done using the "update"
 
317
command. The changes need to be applied before any non-local commit, but
 
318
Bazaar will tell you if there are any changes and suggest that you use this
 
319
command when needed.
 
320
 
 
321
It is also possible to create a "lightweight" checkout by passing the
 
322
--lightweight flag to checkout. A lightweight checkout is even closer to an
 
323
SVN checkout in that it is not a first class branch, it mainly consists of the
 
324
working tree. This means that any history operations must query the master
 
325
branch, which could be slow if a network connection is involved. Also, as you
 
326
don't have a local branch, then you cannot commit locally.
 
327
 
 
328
Lightweight checkouts work best when you have fast reliable access to the
 
329
master branch. This means that if the master branch is on the same disk or LAN
 
330
a lightweight checkout will be faster than a heavyweight one for any commands
 
331
that modify the revision history (as only one copy branch needs to be updated).
 
332
Heavyweight checkouts will generally be faster for any command that uses the
 
333
history but does not change it, but if the master branch is on the same disk
 
334
then there wont be a noticeable difference.
 
335
 
 
336
Another possible use for a checkout is to use it with a treeless repository
 
337
containing your branches, where you maintain only one working tree by
 
338
switching the master branch that the checkout points to when you want to 
 
339
work on a different branch.
 
340
 
 
341
Obviously to commit on a checkout you need to be able to write to the master
 
342
branch. This means that the master branch must be accessible over a writeable
 
343
protocol , such as sftp://, and that you have write permissions at the other
 
344
end. Checkouts also work on the local file system, so that all that matters is
 
345
file permissions.
 
346
 
 
347
You can change the master of a checkout by using the "bind" command (see "help
 
348
bind"). This will change the location that the commits are sent to. The bind
 
349
command can also be used to turn a branch into a heavy checkout. If you
 
350
would like to convert your heavy checkout into a normal branch so that every
 
351
commit is local, you can use the "unbind" command.
 
352
 
 
353
Related commands::
 
354
 
 
355
  checkout    Create a checkout. Pass --lightweight to get a lightweight
 
356
              checkout
 
357
  update      Pull any changes in the master branch in to your checkout
 
358
  commit      Make a commit that is sent to the master branch. If you have
 
359
              a heavy checkout then the --local option will commit to the 
 
360
              checkout without sending the commit to the master
 
361
  bind        Change the master branch that the commits in the checkout will
 
362
              be sent to
 
363
  unbind      Turn a heavy checkout into a standalone branch so that any
 
364
              commits are only made locally
 
365
"""
 
366
 
 
367
_repositories = \
 
368
"""Repositories
 
369
 
 
370
Repositories in Bazaar are where committed information is stored. There is
 
371
a repository associated with every branch.
 
372
 
 
373
Repositories are a form of database. Bzr will usually maintain this for
 
374
good performance automatically, but in some situations (e.g. when doing
 
375
very many commits in a short time period) you may want to ask bzr to 
 
376
optimise the database indices. This can be done by the 'bzr pack' command.
 
377
 
 
378
By default just running 'bzr init' will create a repository within the new
 
379
branch but it is possible to create a shared repository which allows multiple
 
380
branches to share their information in the same location. When a new branch is
 
381
created it will first look to see if there is a containing shared repository it
 
382
can use.
 
383
 
 
384
When two branches of the same project share a repository, there is
 
385
generally a large space saving. For some operations (e.g. branching
 
386
within the repository) this translates in to a large time saving.
 
387
 
 
388
To create a shared repository use the init-repository command (or the alias
 
389
init-repo). This command takes the location of the repository to create. This
 
390
means that 'bzr init-repository repo' will create a directory named 'repo',
 
391
which contains a shared repository. Any new branches that are created in this
 
392
directory will then use it for storage.
 
393
 
 
394
It is a good idea to create a repository whenever you might create more
 
395
than one branch of a project. This is true for both working areas where you
 
396
are doing the development, and any server areas that you use for hosting
 
397
projects. In the latter case, it is common to want branches without working
 
398
trees. Since the files in the branch will not be edited directly there is no
 
399
need to use up disk space for a working tree. To create a repository in which
 
400
the branches will not have working trees pass the '--no-trees' option to
 
401
'init-repository'.
 
402
 
 
403
Related commands::
 
404
 
 
405
  init-repository   Create a shared repository. Use --no-trees to create one
 
406
                    in which new branches won't get a working tree.
 
407
"""
 
408
 
 
409
 
 
410
_working_trees = \
 
411
"""Working Trees
 
412
 
 
413
A working tree is the contents of a branch placed on disk so that you can
 
414
see the files and edit them. The working tree is where you make changes to a
 
415
branch, and when you commit the current state of the working tree is the
 
416
snapshot that is recorded in the commit.
 
417
 
 
418
When you push a branch to a remote system, a working tree will not be
 
419
created. If one is already present the files will not be updated. The
 
420
branch information will be updated and the working tree will be marked
 
421
as out-of-date. Updating a working tree remotely is difficult, as there
 
422
may be uncommitted changes or the update may cause content conflicts that are
 
423
difficult to deal with remotely.
 
424
 
 
425
If you have a branch with no working tree you can use the 'checkout' command
 
426
to create a working tree. If you run 'bzr checkout .' from the branch it will
 
427
create the working tree. If the branch is updated remotely, you can update the
 
428
working tree by running 'bzr update' in that directory.
 
429
 
 
430
If you have a branch with a working tree that you do not want the 'remove-tree'
 
431
command will remove the tree if it is safe. This can be done to avoid the
 
432
warning about the remote working tree not being updated when pushing to the
 
433
branch. It can also be useful when working with a '--no-trees' repository
 
434
(see 'bzr help repositories').
 
435
 
 
436
If you want to have a working tree on a remote machine that you push to you
 
437
can either run 'bzr update' in the remote branch after each push, or use some
 
438
other method to update the tree during the push. There is an 'rspush' plugin
 
439
that will update the working tree using rsync as well as doing a push. There
 
440
is also a 'push-and-update' plugin that automates running 'bzr update' via SSH
 
441
after each push.
 
442
 
 
443
Useful commands::
 
444
 
 
445
  checkout     Create a working tree when a branch does not have one.
 
446
  remove-tree  Removes the working tree from a branch when it is safe to do so.
 
447
  update       When a working tree is out of sync with it's associated branch
 
448
               this will update the tree to match the branch.
 
449
"""
 
450
 
 
451
 
 
452
_branches = \
 
453
"""Branches
 
454
 
 
455
A branch consists of the state of a project, including all of its
 
456
history. All branches have a repository associated (which is where the
 
457
branch history is stored), but multiple branches may share the same
 
458
repository (a shared repository). Branches can be copied and merged.
 
459
 
 
460
Related commands::
 
461
 
 
462
  init    Make a directory into a versioned branch.
 
463
  branch  Create a new copy of a branch.
 
464
  merge   Perform a three-way merge.
 
465
"""
 
466
 
 
467
 
 
468
_standalone_trees = \
 
469
"""Standalone Trees
 
470
 
 
471
A standalone tree is a working tree with an associated repository. It
 
472
is an independently usable branch, with no dependencies on any other.
 
473
Creating a standalone tree (via bzr init) is the quickest way to put
 
474
an existing project under version control.
 
475
 
 
476
Related Commands::
 
477
 
 
478
  init    Make a directory into a versioned branch.
 
479
"""
 
480
 
 
481
 
 
482
_status_flags = \
 
483
"""Status Flags
 
484
 
 
485
Status flags are used to summarise changes to the working tree in a concise
 
486
manner.  They are in the form::
 
487
 
 
488
   xxx   <filename>
 
489
 
 
490
where the columns' meanings are as follows.
 
491
 
 
492
Column 1 - versioning/renames::
 
493
 
 
494
  + File versioned
 
495
  - File unversioned
 
496
  R File renamed
 
497
  ? File unknown
 
498
  C File has conflicts
 
499
  P Entry for a pending merge (not a file)
 
500
 
 
501
Column 2 - contents::
 
502
 
 
503
  N File created
 
504
  D File deleted
 
505
  K File kind changed
 
506
  M File modified
 
507
 
 
508
Column 3 - execute::
 
509
 
 
510
  * The execute bit was changed
 
511
"""
 
512
 
 
513
 
 
514
_env_variables = \
 
515
"""Environment Variables
 
516
 
 
517
================ =================================================================
 
518
BZRPATH          Path where bzr is to look for shell plugin external commands.
 
519
BZR_EMAIL        E-Mail address of the user. Overrides EMAIL.
 
520
EMAIL            E-Mail address of the user.
 
521
BZR_EDITOR       Editor for editing commit messages. Overrides EDITOR.
 
522
EDITOR           Editor for editing commit messages.
 
523
BZR_PLUGIN_PATH  Paths where bzr should look for plugins.
 
524
BZR_HOME         Directory holding .bazaar config dir. Overrides HOME.
 
525
BZR_HOME (Win32) Directory holding bazaar config dir. Overrides APPDATA and HOME.
 
526
BZR_REMOTE_PATH  Full name of remote 'bzr' command (for bzr+ssh:// URLs).
 
527
================ =================================================================
 
528
"""
 
529
 
 
530
 
 
531
_files = \
 
532
r"""Files
 
533
 
 
534
:On Linux:   ~/.bazaar/bazaar.conf
 
535
:On Windows: C:\\Documents and Settings\\username\\Application Data\\bazaar\\2.0\\bazaar.conf
 
536
 
 
537
Contains the user's default configuration. The section ``[DEFAULT]`` is
 
538
used to define general configuration that will be applied everywhere.
 
539
The section ``[ALIASES]`` can be used to create command aliases for
 
540
commonly used options.
 
541
 
 
542
A typical config file might look something like::
 
543
 
 
544
  [DEFAULT]
 
545
  email=John Doe <jdoe@isp.com>
 
546
 
 
547
  [ALIASES]
 
548
  commit = commit --strict
 
549
  log10 = log --short -r -10..-1
 
550
"""
 
551
 
 
552
 
 
553
# Register help topics
 
554
topic_registry.register("revisionspec", _help_on_revisionspec,
 
555
                        "Explain how to use --revision")
 
556
topic_registry.register('basic', _basic_help, "Basic commands", SECT_HIDDEN)
 
557
topic_registry.register('topics', _help_on_topics, "Topics list", SECT_HIDDEN)
 
558
def get_format_topic(topic):
 
559
    from bzrlib import bzrdir
 
560
    return "Storage Formats\n\n" + bzrdir.format_registry.help_topic(topic)
 
561
topic_registry.register('formats', get_format_topic, 'Directory formats')
 
562
topic_registry.register('standard-options', _standard_options,
 
563
                        'Options that can be used with any command')
 
564
topic_registry.register('global-options', _global_options,
 
565
                    'Options that control how Bazaar runs')
 
566
topic_registry.register('urlspec', _help_on_transport,
 
567
                        "Supported transport protocols")
 
568
topic_registry.register('status-flags', _status_flags,
 
569
                        "Help on status flags")
 
570
def get_bugs_topic(topic):
 
571
    from bzrlib import bugtracker
 
572
    return "Bug Trackers\n\n" + bugtracker.tracker_registry.help_topic(topic)
 
573
topic_registry.register('bugs', get_bugs_topic, 'Bug tracker support')
 
574
topic_registry.register('env-variables', _env_variables,
 
575
                        'Environment variable names and values')
 
576
topic_registry.register('files', _files,
 
577
                        'Information on configuration and log files')
 
578
 
 
579
 
 
580
# Register concept topics.
 
581
# Note that we might choose to remove these from the online help in the
 
582
# future or implement them via loading content from files. In the meantime,
 
583
# please keep them concise.
 
584
topic_registry.register('branches', _branches,
 
585
                        'Information on what a branch is', SECT_CONCEPT)
 
586
topic_registry.register('checkouts', _checkouts,
 
587
                        'Information on what a checkout is', SECT_CONCEPT)
 
588
topic_registry.register('repositories', _repositories,
 
589
                        'Basic information on shared repositories.',
 
590
                        SECT_CONCEPT)
 
591
topic_registry.register('standalone-trees', _standalone_trees,
 
592
                        'Information on what a standalone tree is',
 
593
                        SECT_CONCEPT)
 
594
topic_registry.register('working-trees', _working_trees,
 
595
                        'Information on working trees', SECT_CONCEPT)
 
596
 
 
597
 
 
598
class HelpTopicIndex(object):
 
599
    """A index for bzr help that returns topics."""
 
600
 
 
601
    def __init__(self):
 
602
        self.prefix = ''
 
603
 
 
604
    def get_topics(self, topic):
 
605
        """Search for topic in the HelpTopicRegistry.
 
606
 
 
607
        :param topic: A topic to search for. None is treated as 'basic'.
 
608
        :return: A list which is either empty or contains a single
 
609
            RegisteredTopic entry.
 
610
        """
 
611
        if topic is None:
 
612
            topic = 'basic'
 
613
        if topic in topic_registry:
 
614
            return [RegisteredTopic(topic)]
 
615
        else:
 
616
            return []
 
617
 
 
618
 
 
619
class RegisteredTopic(object):
 
620
    """A help topic which has been registered in the HelpTopicRegistry.
 
621
 
 
622
    These topics consist of nothing more than the name of the topic - all
 
623
    data is retrieved on demand from the registry.
 
624
    """
 
625
 
 
626
    def __init__(self, topic):
 
627
        """Constructor.
 
628
 
 
629
        :param topic: The name of the topic that this represents.
 
630
        """
 
631
        self.topic = topic
 
632
 
 
633
    def get_help_text(self, additional_see_also=None, plain=True):
 
634
        """Return a string with the help for this topic.
 
635
 
 
636
        :param additional_see_also: Additional help topics to be
 
637
            cross-referenced.
 
638
        :param plain: if False, raw help (reStructuredText) is
 
639
            returned instead of plain text.
 
640
        """
 
641
        result = topic_registry.get_detail(self.topic)
 
642
        # there is code duplicated here and in bzrlib/plugin.py's 
 
643
        # matching Topic code. This should probably be factored in
 
644
        # to a helper function and a common base class.
 
645
        if additional_see_also is not None:
 
646
            see_also = sorted(set(additional_see_also))
 
647
        else:
 
648
            see_also = None
 
649
        if see_also:
 
650
            result += '\n:See also: '
 
651
            result += ', '.join(see_also)
 
652
            result += '\n'
 
653
        if plain:
 
654
            result = help_as_plain_text(result)
 
655
        return result
 
656
 
 
657
    def get_help_topic(self):
 
658
        """Return the help topic this can be found under."""
 
659
        return self.topic
 
660
 
 
661
 
 
662
def help_as_plain_text(text):
 
663
    """Minimal converter of reStructuredText to plain text."""
 
664
    lines = text.splitlines()
 
665
    result = []
 
666
    for line in lines:
 
667
        if line.startswith(':'):
 
668
            line = line[1:]
 
669
        elif line.endswith('::'):
 
670
            line = line[:-1]
 
671
        result.append(line)
 
672
    return "\n".join(result) + "\n"