Exemplo n.º 1
0
def _GetActionsGroupedByKind(component, verbose=False):
  """Gets lists of available actions, grouped by action kind."""
  groups = []
  commands = []
  values = []

  members = completion._Members(component, verbose)  # pylint: disable=protected-access
  for member_name, member in members:
    member_name = str(member_name)
    if value_types.IsGroup(member):
      groups.append((member_name, member))
    if value_types.IsCommand(member):
      commands.append((member_name, member))
    if value_types.IsValue(member):
      values.append((member_name, member))

  indexes = None
  if isinstance(component, (list, tuple)) and component:
    component_len = len(component)
    # WARNING: Note that indexes is a string, whereas the rest are lists.
    if component_len < 10:
      indexes = ', '.join(str(x) for x in range(component_len))
    else:
      indexes = '0..{max}'.format(max=component_len-1)

  return groups, commands, values, indexes
Exemplo n.º 2
0
def _GetActionsGroupedByKind(component, verbose=False):
  """Gets lists of available actions, grouped by action kind."""
  groups = ActionGroup(name='group', plural='groups')
  commands = ActionGroup(name='command', plural='commands')
  values = ActionGroup(name='value', plural='values')
  indexes = ActionGroup(name='index', plural='indexes')

  members = completion.VisibleMembers(component, verbose=verbose)
  for member_name, member in members:
    member_name = str(member_name)
    if value_types.IsGroup(member):
      groups.Add(name=member_name, member=member)
    if value_types.IsCommand(member):
      commands.Add(name=member_name, member=member)
    if value_types.IsValue(member):
      values.Add(name=member_name, member=member)

  if isinstance(component, (list, tuple)) and component:
    component_len = len(component)
    if component_len < 10:
      indexes.Add(name=', '.join(str(x) for x in range(component_len)))
    else:
      indexes.Add(name='0..{max}'.format(max=component_len-1))

  return [groups, commands, values, indexes]
Exemplo n.º 3
0
def UsageTextForObject(component, trace=None, verbose=False):
    """Returns help text for usage screen for objects.

  Construct help text for usage screen to inform the user about error occurred
  and correct syntax for invoking the object.

  Args:
    component: The component to determine the usage text for.
    trace: The Fire trace object containing all metadata of current execution.
    verbose: Whether to include private members in the usage text.
  Returns:
    String suitable for display in error screen.
  """
    output_template = """Usage: {current_command} <{possible_actions}>
{availability_lines}

For detailed information on this command, run:
{current_command} --help
"""
    if trace:
        command = trace.GetCommand()
    else:
        command = None

    if not command:
        command = ''

    groups = []
    commands = []
    values = []

    members = completion._Members(component, verbose)  # pylint: disable=protected-access
    for member_name, member in members:
        if value_types.IsGroup(member):
            groups.append(member_name)
        if value_types.IsCommand(member):
            commands.append(member_name)
        if value_types.IsValue(member):
            values.append(member_name)

    possible_actions = []
    availability_lines = []
    availability_lint_format = '{header:20s}{choices}'
    if groups:
        possible_actions.append('groups')
        groups_string = ' | '.join(groups)
        groups_text = availability_lint_format.format(
            header='available groups:', choices=groups_string)
        availability_lines.append(groups_text)
    if commands:
        possible_actions.append('commands')
        commands_string = ' | '.join(commands)
        commands_text = availability_lint_format.format(
            header='available commands:', choices=commands_string)
        availability_lines.append(commands_text)
    if values:
        possible_actions.append('values')
        values_string = ' | '.join(values)
        values_text = availability_lint_format.format(
            header='available values:', choices=values_string)
        availability_lines.append(values_text)
    possible_actions_string = '|'.join(possible_actions)
    availability_lines_string = '\n'.join(availability_lines)

    return output_template.format(current_command=command,
                                  possible_actions=possible_actions_string,
                                  availability_lines=availability_lines_string)
Exemplo n.º 4
0
def HelpTextForObject(component, info, trace=None, verbose=False):
    """Generates help text for python objects.

  Args:
    component: Current component to generate help text for.
    info: Info containing metadata of component.
    trace: FireTrace object that leads to current component.
    verbose: Whether to display help text in verbose mode.

  Returns:
    Formatted help text for display.
  """

    output_template = """NAME
    {current_command} - {command_summary}

SYNOPSIS
    {synopsis}

DESCRIPTION
    {command_description}
{detail_section}
"""

    current_command = GetCurrentCommand(trace)

    docstring_info = info['docstring_info']
    command_summary = docstring_info.summary if docstring_info.summary else ''
    if docstring_info.description:
        command_description = docstring_info.description
    else:
        command_description = ''

    groups = []
    commands = []
    values = []
    members = completion._Members(component, verbose)  # pylint: disable=protected-access
    for member_name, member in members:
        if value_types.IsGroup(member):
            groups.append((member_name, member))
        if value_types.IsCommand(member):
            commands.append((member_name, member))
        if value_types.IsValue(member):
            values.append((member_name, member))

    possible_actions = []
    # TODO(joejoevictor): Add global flags to here. Also, if it's a callable,
    # there will be additional flags.
    possible_flags = ''
    detail_section_string = ''
    item_template = """
        {name}
            {command_summary}
"""

    if groups:
        # TODO(joejoevictor): Add missing GROUPS section handling
        possible_actions.append('GROUP')
    if commands:
        possible_actions.append('COMMAND')
        commands_str_template = """
COMMANDS
    COMMAND is one of the followings:
{items}
"""
        command_item_strings = []
        for command_name, command in commands:
            command_docstring_info = docstrings.parse(
                inspectutils.Info(command)['docstring'])
            command_item_strings.append(
                item_template.format(
                    name=command_name,
                    command_summary=command_docstring_info.summary))
        detail_section_string += commands_str_template.format(
            items=('\n'.join(command_item_strings)).rstrip('\n'))

    if values:
        possible_actions.append('VALUES')
        values_str_template = """
VALUES
    VALUE is one of the followings:
{items}
"""
        value_item_strings = []
        for value_name, value in values:
            del value
            init_docstring_info = docstrings.parse(
                inspectutils.Info(component.__class__.__init__)['docstring'])
            for arg_info in init_docstring_info.args:
                if arg_info.name == value_name:
                    value_item_strings.append(
                        item_template.format(
                            name=value_name,
                            command_summary=arg_info.description))
        detail_section_string += values_str_template.format(
            items=('\n'.join(value_item_strings)).rstrip('\n'))

    possible_actions_string = ' ' + (' | '.join(possible_actions))

    synopsis_template = '{current_command}{possible_actions}{possible_flags}'
    synopsis_string = synopsis_template.format(
        current_command=current_command,
        possible_actions=possible_actions_string,
        possible_flags=possible_flags)

    return output_template.format(current_command=current_command,
                                  command_summary=command_summary,
                                  synopsis=synopsis_string,
                                  command_description=command_description,
                                  detail_section=detail_section_string)
Exemplo n.º 5
0
def UsageTextForObject(component, trace=None, verbose=False):
    """Returns the usage text for the error screen for an object.

  Constructs the usage text for the error screen to inform the user about how
  to use the current component.

  Args:
    component: The component to determine the usage text for.
    trace: The Fire trace object containing all metadata of current execution.
    verbose: Whether to include private members in the usage text.
  Returns:
    String suitable for display in error screen.
  """
    output_template = """Usage: {current_command}{possible_actions}
{availability_lines}
For detailed information on this command, run:
  {current_command} --help"""
    if trace:
        command = trace.GetCommand()
    else:
        command = None

    if not command:
        command = ''

    groups = []
    commands = []
    values = []

    members = completion._Members(component, verbose)  # pylint: disable=protected-access
    for member_name, member in members:
        member_name = str(member_name)
        if value_types.IsGroup(member):
            groups.append(member_name)
        if value_types.IsCommand(member):
            commands.append(member_name)
        if value_types.IsValue(member):
            values.append(member_name)

    possible_actions = []
    availability_lines = []
    if groups:
        possible_actions.append('group')
        groups_text = _CreateAvailabilityLine(header='available groups:',
                                              items=groups)
        availability_lines.append(groups_text)
    if commands:
        possible_actions.append('command')
        commands_text = _CreateAvailabilityLine(header='available commands:',
                                                items=commands)
        availability_lines.append(commands_text)
    if values:
        possible_actions.append('value')
        values_text = _CreateAvailabilityLine(header='available values:',
                                              items=values)
        availability_lines.append(values_text)

    if possible_actions:
        possible_actions_string = ' <{actions}>'.format(
            actions='|'.join(possible_actions))
    else:
        possible_actions_string = ''

    availability_lines_string = ''.join(availability_lines)

    return output_template.format(current_command=command,
                                  possible_actions=possible_actions_string,
                                  availability_lines=availability_lines_string)
Exemplo n.º 6
0
def HelpTextForObject(component, info, trace=None, verbose=False):
    """Generates help text for python objects.

  Args:
    component: Current component to generate help text for.
    info: Info containing metadata of component.
    trace: FireTrace object that leads to current component.
    verbose: Whether to display help text in verbose mode.

  Returns:
    Formatted help text for display.
  """
    current_command = GetCurrentCommand(trace)
    current_command_without_separator = GetCurrentCommand(
        trace, include_separators=False)
    docstring_info = info['docstring_info']
    command_summary = docstring_info.summary if docstring_info.summary else ''
    command_description = GetDescriptionSectionText(docstring_info.summary,
                                                    docstring_info.description)
    groups = []
    commands = []
    values = []
    members = completion._Members(component, verbose)  # pylint: disable=protected-access
    for member_name, member in members:
        if value_types.IsGroup(member):
            groups.append((member_name, member))
        if value_types.IsCommand(member):
            commands.append((member_name, member))
        if value_types.IsValue(member):
            values.append((member_name, member))

    usage_details_sections = []
    possible_actions = []
    # TODO(joejoevictor): Add global flags to here. Also, if it's a callable,
    # there will be additional flags.
    possible_flags = ''

    if groups:
        possible_actions.append('GROUP')
        usage_details_section = GroupUsageDetailsSection(groups)
        usage_details_sections.append(usage_details_section)
    if commands:
        possible_actions.append('COMMAND')
        usage_details_section = CommandUsageDetailsSection(commands)
        usage_details_sections.append(usage_details_section)
    if values:
        possible_actions.append('VALUE')
        usage_details_section = ValuesUsageDetailsSection(component, values)
        usage_details_sections.append(usage_details_section)

    possible_actions_string = ' | '.join(
        formatting.Underline(action) for action in possible_actions)

    synopsis_template = '{current_command} {possible_actions}{possible_flags}'
    synopsis_string = synopsis_template.format(
        current_command=current_command,
        possible_actions=possible_actions_string,
        possible_flags=possible_flags)

    description_sections = []
    if command_description:
        description_sections.append(('DESCRIPTION', command_description))

    name_line = '{current_command} - {command_summary}'.format(
        current_command=current_command_without_separator,
        command_summary=command_summary)
    output_sections = [
        ('NAME', name_line),
        ('SYNOPSIS', synopsis_string),
    ] + description_sections + usage_details_sections

    return '\n\n'.join(
        _CreateOutputSection(name, content)
        for name, content in output_sections)
Exemplo n.º 7
0
def HelpTextForObject(component, info, trace=None, verbose=False):
  """Generates help text for python objects.

  Args:
    component: Current component to generate help text for.
    info: Info containing metadata of component.
    trace: FireTrace object that leads to current component.
    verbose: Whether to display help text in verbose mode.

  Returns:
    Formatted help text for display.
  """
  current_command = GetCurrentCommand(trace)

  docstring_info = info['docstring_info']
  command_summary = docstring_info.summary if docstring_info.summary else ''
  command_description = GetDescriptionSectionText(docstring_info.summary,
                                                  docstring_info.description)
  groups = []
  commands = []
  values = []
  members = completion._Members(component, verbose)  # pylint: disable=protected-access
  for member_name, member in members:
    if value_types.IsGroup(member):
      groups.append((member_name, member))
    if value_types.IsCommand(member):
      commands.append((member_name, member))
    if value_types.IsValue(member):
      values.append((member_name, member))

  usage_details_sections = []
  possible_actions = []
  # TODO(joejoevictor): Add global flags to here. Also, if it's a callable,
  # there will be additional flags.
  possible_flags = ''

  if groups:
    # TODO(joejoevictor): Add missing GROUPS section handling
    possible_actions.append('GROUP')
  if commands:
    possible_actions.append('COMMAND')
    command_item_strings = []
    for command_name, command in commands:
      command_info = inspectutils.Info(command)
      command_item = command_name
      if 'docstring_info' in command_info:
        command_docstring_info = command_info['docstring_info']
        if command_docstring_info and command_docstring_info.summary:
          command_item = _CreateItem(command_name,
                                     command_docstring_info.summary)

      command_item_strings.append(command_item)
    usage_details_sections.append(
        ('COMMANDS', _NewChoicesSection('COMMAND', command_item_strings)))

  if values:
    possible_actions.append('VALUE')
    value_item_strings = []
    for value_name, value in values:
      del value
      init_info = inspectutils.Info(component.__class__.__init__)
      value_item = value_name
      if 'docstring_info' in init_info:
        init_docstring_info = init_info['docstring_info']
        for arg_info in init_docstring_info.args:
          if arg_info.name == value_name:
            value_item = _CreateItem(value_name, arg_info.description)
      value_item_strings.append(value_item)
    usage_details_sections.append(
        ('VALUES', _NewChoicesSection('VALUE', value_item_strings)))

  possible_actions_string = ' | '.join(
      formatting.Underline(action) for action in possible_actions)

  synopsis_template = '{current_command} {possible_actions}{possible_flags}'
  synopsis_string = synopsis_template.format(
      current_command=current_command,
      possible_actions=possible_actions_string,
      possible_flags=possible_flags)

  description_sections = []
  if command_description:
    description_sections.append(('DESCRIPTION', command_description))

  name_line = '{current_command} - {command_summary}'.format(
      current_command=current_command,
      command_summary=command_summary)
  output_sections = [
      ('NAME', name_line),
      ('SYNOPSIS', synopsis_string),
  ] + description_sections + usage_details_sections

  return '\n\n'.join(
      _CreateOutputSection(name, content)
      for name, content in output_sections
  )