def help_command(group_name, command_name): """Print help on a command or group and exit. This will print help on a command, or an error if the command does not exist, and then exit with exit code 2. """ try: # Get the command and information on its parameters. cargs = None cls = get_command(group_name, command_name) command_text = cls.get_signature() paragraphs = [] if cls.__doc__: wrapper = textwrap.TextWrapper() paragraphs = [] for para in cls.__doc__.split("\n\n"): if para: paragraphs.append(wrapper.fill(para.strip())) print command_text, "\n\n", "\n\n".join(paragraphs) except KeyError as exc: msg = _ERR_COMMAND_MISSING % (command_name, group_name) PARSER.print_error(msg + '({0})'.format(exc)) PARSER.exit(2)
def load_services(self, options, config): """Load services into each protocol server. :param options: The options for the commands that shall be created. :param config: The configuration for the commands that shall be created. """ _LOGGER.info("Loading Services.") find_commands(config) for group_name in get_groups(): for command_name in get_commands(group_name): command = get_command(group_name, command_name) if hasattr(command, "execute"): _LOGGER.debug( "Registering %s.", command.group_name + '.' + \ command.command_name ) if self.__mysql_server: cmd = command() cmd.setup_server(self.__mysql_server, options, config) self.__mysql_server.register_command(cmd) if self.__rpc_server: cmd = command() cmd.setup_server(self.__rpc_server, options, config) self.__rpc_server.register_command(cmd)
def create_command(group_name, command_name, options, args, config): """Create command object. """ options = None try: # Fetch command class and create the command instance. command = get_command(group_name, command_name)() # Set up options for command command.add_options(PARSER) # Parse arguments options, args = PARSER.parse_args(args, options) # Create a protocol client for dispatching the command and set # up the client-side information for the command. Inside a # shell, this only have to be done once, but here we need to # set up the client-side of the command each time we call the # program. client = find_client() command.setup_client(client, options, config) return command, args except KeyError: PARSER.error( "Command (%s %s) was not found." % (group_name, command_name, ) )
def create_command(group_name, command_name, options, args, config): """Create command object. """ options = None try: # Fetch command class and create the command instance. command = get_command(group_name, command_name)() # Set up options for command command.add_options(PARSER) # Parse arguments options, args = PARSER.parse_args(args, options) # Create a protocol client for dispatching the command and set # up the client-side information for the command. Inside a # shell, this only have to be done once, but here we need to # set up the client-side of the command each time we call the # program. client = find_client() command.setup_client(client, options, config) return command, args except KeyError: PARSER.error("Command (%s %s) was not found." % ( group_name, command_name, ))
def show_commands(): """List the possible commands and their descriptions. """ commands = [] max_name_size = 0 for group_name in get_groups(): for command_name in get_commands(group_name): cls = get_command(group_name, command_name) doc_text = "" if cls.__doc__ and cls.__doc__.find(".") != -1: doc_text = cls.__doc__[0:cls.__doc__.find(".") + 1] elif cls.__doc__: doc_text = cls.__doc__ doc_text = [text.strip(" ") for text in doc_text.split("\n")] commands.append((group_name, command_name, " ".join(doc_text))) name_size = len(group_name) + len(command_name) if name_size > max_name_size: max_name_size = name_size # Format each description and print the result. wrapper = textwrap.TextWrapper(subsequent_indent=(" " * (max_name_size + 3))) for group_name, command_name, help_text in commands: padding_size = max_name_size - len(group_name) - len(command_name) padding_size = 0 if padding_size < 0 else padding_size padding_text = "".rjust(padding_size, " ") help_text = wrapper.fill(help_text) text = (group_name, command_name, padding_text, help_text) print " ".join(text)
def error_usage_text(group_name, command_name): """Print Usage information upon error while invoking the command. :param group_name: The Group of the command (sharding, server etc). :param command_name: The command name. """ cls = get_command(group_name, command_name) command_text = cls.get_signature() print "Usage: ", command_text, "\n" PARSER.error("Wrong number of parameters were provided " "for command '%s %s'." % ( group_name, command_name, ))
def error_usage_text(group_name, command_name): """Print Usage information upon error while invoking the command. :param group_name: The Group of the command (sharding, server etc). :param command_name: The command name. """ cls = get_command(group_name, command_name) command_text = cls.get_signature() print "Usage: ", command_text, "\n" PARSER.error( "Wrong number of parameters were provided " "for command '%s %s'." % ( group_name, command_name, ) )
def help_group(group_name): """Print help on a command group and exit. This will print a list of the commands available in the group, or an error message if the group is not available, and then exit with exit code 2. """ indent = " " * 4 try: commands = get_commands(group_name) print print "Commands available in group '%s' are:" % (group_name,) for cmdname in commands: print indent + get_command(group_name, cmdname).get_signature() except KeyError: PARSER.print_error(_ERR_GROUP_MISSING % (group_name, )) PARSER.exit(2)
def help_group(group_name): """Print help on a command group and exit. This will print a list of the commands available in the group, or an error message if the group is not available, and then exit with exit code 2. """ indent = " " * 4 try: commands = get_commands(group_name) print print "Commands available in group '%s' are:" % (group_name, ) for cmdname in commands: print indent + get_command(group_name, cmdname).get_signature() except KeyError: PARSER.print_error(_ERR_GROUP_MISSING % (group_name, )) PARSER.exit(2)
def _handle_show_create_procedure(self, showproc_match): """Handle SHOW CREATE PROCEDURE We send the information from the command as text to the client in a one row/one column result. :param showproc_match: Result of re.match """ group, command = showproc_match.groups() try: command_class = get_command(group, command) except KeyError: # group and/or command does not exists self.send_error( errorcode.ER_UNKNOWN_PROCEDURE, "Fabric command {0}.{1} does not exists".format(group,command), "22000" ) return self.send_packet(self.column_count_packet(1)) self.send_packet(self.column_packet( 'fabric_help', type_=str, table='fabric_help' )) self.send_packet(self.eof_packet()) command_text = command_class.get_signature() paragraphs = [] if command_class.__doc__: wrapper = TextWrapper() paragraphs = [] for para in command_class.__doc__.split("\n\n"): if para: paragraphs.append(wrapper.fill(para.strip())) help_text = command_text + "\n\n" + "\n\n".join(paragraphs) _LOGGER.debug(help_text) self.send_packet(self.row_packet(help_text)) self.send_packet(self.eof_packet())
def show_commands(): """List the possible commands and their descriptions. """ commands = [] max_name_size = 0 for group_name in get_groups(): for command_name in get_commands(group_name): cls = get_command(group_name, command_name) doc_text = "" if cls.__doc__ and cls.__doc__.find(".") != -1: doc_text = cls.__doc__[0: cls.__doc__.find(".") + 1] elif cls.__doc__: doc_text = cls.__doc__ doc_text = [text.strip(" ") for text in doc_text.split("\n")] commands.append( (group_name, command_name, " ".join(doc_text)) ) name_size = len(group_name) + len(command_name) if name_size > max_name_size: max_name_size = name_size # Format each description and print the result. wrapper = textwrap.TextWrapper( subsequent_indent=(" " * (max_name_size + 3))) for group_name, command_name, help_text in commands: padding_size = max_name_size - len(group_name) - len(command_name) padding_size = 0 if padding_size < 0 else padding_size padding_text = "".rjust(padding_size, " ") help_text = wrapper.fill(help_text) text = (group_name, command_name, padding_text, help_text) print " ".join(text)