One of the major task of rallyclient is a command line interface.
This patch implements base for it.
- rallyclient.exc - the main module for exceptions, which include custom
exceptions(not implemented yet) and exceptions from common code;
- rallyclient.shell - base shell, which is independent from version of API
- rallyclient.utils - custom rallyclient's utils
- rallyclient._i18n - setup for oslo.i18n
- rallyclient.v1.* - versioned module for Rally API V1
- rallyclient.v1.shell - base shell for Rally API V1
bp api-rally-python-client
Change-Id: I910b311908013207aac04ae3b2f24405b58f69c9
67 lines
2.7 KiB
Python
67 lines
2.7 KiB
Python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import argparse
|
|
|
|
|
|
class HelpFormatter(argparse.HelpFormatter):
|
|
INDENT_BEFORE_ARGUMENTS = 6
|
|
MAX_WIDTH_ARGUMENTS = 32
|
|
|
|
def add_arguments(self, actions):
|
|
for action in filter(lambda x: not x.option_strings, actions):
|
|
for choice in action.choices:
|
|
length = len(choice) + self.INDENT_BEFORE_ARGUMENTS
|
|
if (self.MAX_WIDTH_ARGUMENTS >= length and
|
|
length > self._max_help_position):
|
|
self._max_help_position = length
|
|
super(HelpFormatter, self).add_arguments(actions)
|
|
|
|
def start_section(self, heading):
|
|
# Title-case the headings
|
|
heading = '%s%s' % (heading[0].upper(), heading[1:])
|
|
super(HelpFormatter, self).start_section(heading)
|
|
|
|
|
|
def define_command(subparsers, command, callback, cmd_mapper):
|
|
"""Define a command in the subparsers collection.
|
|
|
|
:param subparsers: subparsers collection where the command will go
|
|
:param command: command name
|
|
:param callback: function that will be used to process the command
|
|
"""
|
|
desc = callback.__doc__ or ''
|
|
help = desc.strip().split('\n')[0]
|
|
arguments = getattr(callback, 'arguments', [])
|
|
|
|
subparser = subparsers.add_parser(command, help=help,
|
|
description=desc,
|
|
add_help=False,
|
|
formatter_class=HelpFormatter)
|
|
subparser.add_argument('-h', '--help', action='help',
|
|
help=argparse.SUPPRESS)
|
|
cmd_mapper[command] = subparser
|
|
for (args, kwargs) in arguments:
|
|
subparser.add_argument(*args, **kwargs)
|
|
subparser.set_defaults(func=callback)
|
|
|
|
|
|
def define_commands_from_module(subparsers, command_module, cmd_mapper):
|
|
"""Find all methods beginning with 'do_' in a module, and add them
|
|
as commands into a subparsers collection.
|
|
"""
|
|
for method_name in (a for a in dir(command_module) if a.startswith('do_')):
|
|
# Commands should be hypen-separated instead of underscores.
|
|
command = method_name[3:].replace('_', '-')
|
|
callback = getattr(command_module, method_name)
|
|
define_command(subparsers, command, callback, cmd_mapper)
|