1 # -*- coding: utf-8 -*-
2 # Copyright 2012 Google Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Module defining help types and providers for gsutil commands."""
17 from __future__
import absolute_import
20 from gslib
.exception
import CommandException
22 ALL_HELP_TYPES
= ['command_help', 'additional_help']
24 # Constants enforced by SanityCheck
25 MAX_HELP_NAME_LEN
= 15
26 MIN_ONE_LINE_SUMMARY_LEN
= 10
27 MAX_ONE_LINE_SUMMARY_LEN
= 80 - MAX_HELP_NAME_LEN
29 DESCRIPTION_PREFIX
= """
36 class HelpProvider(object):
37 """Interface for providing help."""
39 # Each subclass of HelpProvider define a property named 'help_spec' that is
40 # an instance of the following class.
41 HelpSpec
= collections
.namedtuple('HelpSpec', [
42 # Name of command or auxiliary help info for which this help applies.
44 # List of help name aliases.
48 # One line summary of this help.
49 'help_one_line_summary',
52 # Help text for subcommands of the command's help being specified.
53 'subcommand_help_text',
56 # Each subclass must override this with an instance of HelpSpec.
60 # This is a static helper instead of a class method because the help loader
61 # (gslib.commands.help._LoadHelpMaps()) operates on classes not instances.
62 def SanityCheck(help_provider
, help_name_map
):
63 """Helper for checking that a HelpProvider has minimally adequate content."""
64 # Sanity check the content.
65 assert (len(help_provider
.help_spec
.help_name
) > 1
66 and len(help_provider
.help_spec
.help_name
) < MAX_HELP_NAME_LEN
)
67 for hna
in help_provider
.help_spec
.help_name_aliases
:
69 one_line_summary_len
= len(help_provider
.help_spec
.help_one_line_summary
)
70 assert (one_line_summary_len
> MIN_ONE_LINE_SUMMARY_LEN
71 and one_line_summary_len
< MAX_ONE_LINE_SUMMARY_LEN
)
72 assert len(help_provider
.help_spec
.help_text
) > 10
74 # Ensure there are no dupe help names or aliases across commands.
75 name_check_list
= [help_provider
.help_spec
.help_name
]
76 name_check_list
.extend(help_provider
.help_spec
.help_name_aliases
)
77 for name_or_alias
in name_check_list
:
78 if help_name_map
.has_key(name_or_alias
):
79 raise CommandException(
80 'Duplicate help name/alias "%s" found while loading help from %s. '
81 'That name/alias was already taken by %s' % (
82 name_or_alias
, help_provider
.__module
__,
83 help_name_map
[name_or_alias
].__module
__))
86 def CreateHelpText(synopsis
, description
):
87 """Helper for adding help text headers given synopsis and description."""
88 return SYNOPSIS_PREFIX
+ synopsis
+ DESCRIPTION_PREFIX
+ description