| OLD | NEW |
| 1 # -*- coding: utf-8 -*- |
| 1 # Copyright 2012 Google Inc. All Rights Reserved. | 2 # Copyright 2012 Google Inc. All Rights Reserved. |
| 2 # | 3 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
| 6 # | 7 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 9 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 14 # limitations under the License. |
| 15 """Module defining help types and providers for gsutil commands.""" |
| 14 | 16 |
| 17 from __future__ import absolute_import |
| 18 |
| 19 import collections |
| 15 from gslib.exception import CommandException | 20 from gslib.exception import CommandException |
| 16 | 21 |
| 17 class HelpType(object): | 22 ALL_HELP_TYPES = ['command_help', 'additional_help'] |
| 18 COMMAND_HELP = 'command_help' | |
| 19 ADDITIONAL_HELP = 'additional_help' | |
| 20 | |
| 21 ALL_HELP_TYPES = [HelpType.COMMAND_HELP, HelpType.ADDITIONAL_HELP] | |
| 22 | |
| 23 # help_spec key constants. | |
| 24 HELP_NAME = 'help_name' | |
| 25 HELP_NAME_ALIASES = 'help_name_aliases' | |
| 26 HELP_TYPE = 'help_type' | |
| 27 HELP_ONE_LINE_SUMMARY = 'help_one_line_summary' | |
| 28 HELP_TEXT = 'help_text' | |
| 29 SUBCOMMAND_HELP_TEXT = 'subcommand_help_text' | |
| 30 | 23 |
| 31 # Constants enforced by SanityCheck | 24 # Constants enforced by SanityCheck |
| 32 MAX_HELP_NAME_LEN = 15 | 25 MAX_HELP_NAME_LEN = 15 |
| 33 MIN_ONE_LINE_SUMMARY_LEN = 10 | 26 MIN_ONE_LINE_SUMMARY_LEN = 10 |
| 34 MAX_ONE_LINE_SUMMARY_LEN = 80 - MAX_HELP_NAME_LEN | 27 MAX_ONE_LINE_SUMMARY_LEN = 80 - MAX_HELP_NAME_LEN |
| 35 | 28 |
| 36 REQUIRED_SPEC_KEYS = [HELP_NAME, HELP_NAME_ALIASES, HELP_TYPE, | |
| 37 HELP_ONE_LINE_SUMMARY, HELP_TEXT] | |
| 38 | |
| 39 DESCRIPTION_PREFIX = """ | 29 DESCRIPTION_PREFIX = """ |
| 40 <B>DESCRIPTION</B>""" | 30 <B>DESCRIPTION</B>""" |
| 41 | 31 |
| 42 SYNOPSIS_PREFIX = """ | 32 SYNOPSIS_PREFIX = """ |
| 43 <B>SYNOPSIS</B>""" | 33 <B>SYNOPSIS</B>""" |
| 44 | 34 |
| 35 |
| 45 class HelpProvider(object): | 36 class HelpProvider(object): |
| 46 """Interface for providing help.""" | 37 """Interface for providing help.""" |
| 47 | 38 |
| 48 # Each subclass must define the following map. | 39 # Each subclass of HelpProvider define a property named 'help_spec' that is |
| 49 help_spec = { | 40 # an instance of the following class. |
| 50 # Name of command or auxiliary help info for which this help applies. | 41 HelpSpec = collections.namedtuple('HelpSpec', [ |
| 51 HELP_NAME : None, | 42 # Name of command or auxiliary help info for which this help applies. |
| 52 # List of help name aliases. | 43 'help_name', |
| 53 HELP_NAME_ALIASES : None, | 44 # List of help name aliases. |
| 54 # HelpType. | 45 'help_name_aliases', |
| 55 HELP_TYPE : None, | 46 # Type of help. |
| 56 # One line summary of this help. | 47 'help_type', |
| 57 HELP_ONE_LINE_SUMMARY : None, | 48 # One line summary of this help. |
| 58 # The full help text. | 49 'help_one_line_summary', |
| 59 HELP_TEXT : None, | 50 # The full help text. |
| 60 } | 51 'help_text', |
| 52 # Help text for subcommands of the command's help being specified. |
| 53 'subcommand_help_text', |
| 54 ]) |
| 55 |
| 56 # Each subclass must override this with an instance of HelpSpec. |
| 57 help_spec = None |
| 58 |
| 61 | 59 |
| 62 # This is a static helper instead of a class method because the help loader | 60 # This is a static helper instead of a class method because the help loader |
| 63 # (gslib.commands.help._LoadHelpMaps()) operates on classes not instances. | 61 # (gslib.commands.help._LoadHelpMaps()) operates on classes not instances. |
| 64 def SanityCheck(help_provider, help_name_map): | 62 def SanityCheck(help_provider, help_name_map): |
| 65 """Helper for checking that a HelpProvider has minimally adequate content.""" | 63 """Helper for checking that a HelpProvider has minimally adequate content.""" |
| 66 for k in REQUIRED_SPEC_KEYS: | |
| 67 if k not in help_provider.help_spec or help_provider.help_spec[k] is None: | |
| 68 raise CommandException('"%s" help implementation is missing %s ' | |
| 69 'specification' % (help_provider.help_name, k)) | |
| 70 # Sanity check the content. | 64 # Sanity check the content. |
| 71 assert (len(help_provider.help_spec[HELP_NAME]) > 1 | 65 assert (len(help_provider.help_spec.help_name) > 1 |
| 72 and len(help_provider.help_spec[HELP_NAME]) < MAX_HELP_NAME_LEN) | 66 and len(help_provider.help_spec.help_name) < MAX_HELP_NAME_LEN) |
| 73 for hna in help_provider.help_spec[HELP_NAME_ALIASES]: | 67 for hna in help_provider.help_spec.help_name_aliases: |
| 74 assert len(hna) > 0 | 68 assert hna |
| 75 one_line_summary_len = len(help_provider.help_spec[HELP_ONE_LINE_SUMMARY]) | 69 one_line_summary_len = len(help_provider.help_spec.help_one_line_summary) |
| 76 assert (one_line_summary_len > MIN_ONE_LINE_SUMMARY_LEN | 70 assert (one_line_summary_len > MIN_ONE_LINE_SUMMARY_LEN |
| 77 and one_line_summary_len < MAX_ONE_LINE_SUMMARY_LEN) | 71 and one_line_summary_len < MAX_ONE_LINE_SUMMARY_LEN) |
| 78 assert len(help_provider.help_spec[HELP_TEXT]) > 10 | 72 assert len(help_provider.help_spec.help_text) > 10 |
| 79 | 73 |
| 80 # Ensure there are no dupe help names or aliases across commands. | 74 # Ensure there are no dupe help names or aliases across commands. |
| 81 name_check_list = [help_provider.help_spec[HELP_NAME]] | 75 name_check_list = [help_provider.help_spec.help_name] |
| 82 name_check_list.extend(help_provider.help_spec[HELP_NAME_ALIASES]) | 76 name_check_list.extend(help_provider.help_spec.help_name_aliases) |
| 83 for name_or_alias in name_check_list: | 77 for name_or_alias in name_check_list: |
| 84 if help_name_map.has_key(name_or_alias): | 78 if help_name_map.has_key(name_or_alias): |
| 85 raise CommandException( | 79 raise CommandException( |
| 86 'Duplicate help name/alias "%s" found while loading help from %s. ' | 80 'Duplicate help name/alias "%s" found while loading help from %s. ' |
| 87 'That name/alias was already taken by %s' % (name_or_alias, | 81 'That name/alias was already taken by %s' % ( |
| 88 help_provider.__module__, help_name_map[name_or_alias].__module__)) | 82 name_or_alias, help_provider.__module__, |
| 89 | 83 help_name_map[name_or_alias].__module__)) |
| 84 |
| 85 |
| 90 def CreateHelpText(synopsis, description): | 86 def CreateHelpText(synopsis, description): |
| 91 """Helper for adding help text headers given synopsis and description.""" | 87 """Helper for adding help text headers given synopsis and description.""" |
| 92 return SYNOPSIS_PREFIX + synopsis + DESCRIPTION_PREFIX + description | 88 return SYNOPSIS_PREFIX + synopsis + DESCRIPTION_PREFIX + description |
| OLD | NEW |