Ejemplo n.º 1
0
def _CreateFlagItem(flag,
                    docstring_info,
                    spec,
                    required=False,
                    flag_string=None):
    """Returns a string describing a flag using docstring and FullArgSpec info.

    Args:
      flag: The name of the flag.
      docstring_info: A docstrings.DocstringInfo namedtuple with information about
        the containing function's docstring.
      spec: An instance of fire.inspectutils.FullArgSpec, containing type and
       default information about the arguments to a callable.
      required: Whether the flag is required.
      flag_string: If provided, use this string for the flag, rather than
        constructing one from the flag name.
    Returns:
      A string to be used in constructing the help screen for the function.
    """
    # pylint: disable=g-bad-todo
    # TODO(MichaelCG8): Get type and default information from docstrings if it is
    # not available in FullArgSpec. This will require updating
    # fire.docstrings.parser().

    # The help string is indented, so calculate the maximum permitted length
    # before indentation to avoid exceeding the maximum line length.
    max_str_length = LINE_LENGTH - SECTION_INDENTATION - SUBSECTION_INDENTATION

    description = _GetArgDescription(flag, docstring_info)

    if not flag_string:
        flag_string_template = "--{flag_name}={flag_name_upper}"
        flag_string = flag_string_template.format(
            flag_name=flag, flag_name_upper=formatting.Underline(flag.upper()))
    if required:
        flag_string += " (required)"

    arg_type = _GetArgType(flag, spec)
    arg_default = _GetArgDefault(flag, spec)

    # We need to handle the case where there is a default of None, but otherwise
    # the argument has another type.
    if arg_default == "None":
        arg_type = "Optional[{}]".format(arg_type)

    arg_type = "Type: {}".format(arg_type) if arg_type else ""
    available_space = max_str_length - len(arg_type)
    arg_type = formatting.EllipsisTruncate(arg_type, available_space,
                                           max_str_length)

    arg_default = "Default: {}".format(arg_default) if arg_default else ""
    available_space = max_str_length - len(arg_default)
    arg_default = formatting.EllipsisTruncate(arg_default, available_space,
                                              max_str_length)

    description = "\n".join(part
                            for part in (arg_type, arg_default, description)
                            if part)

    return _CreateItem(flag_string, description, indent=SUBSECTION_INDENTATION)
Ejemplo n.º 2
0
def _CreateFlagItem(flag, docstring_info, spec, required=False):
    """Returns a string describing a flag using docstring and FullArgSpec info.

  Args:
    flag: The name of the flag.
    docstring_info: A docstrings.DocstringInfo namedtuple with information about
      the containing function's docstring.
    spec: An instance of fire.inspectutils.FullArgSpec, containing type and
     default information about the arguments to a callable.
    required: Whether the flag is required.
  Returns:
    A string to be used in constructing the help screen for the function.
  """
    # TODO(MichaelCG8): In future it would be good to be able to get type and
    #  default information out of docstrings if it is not available in
    #  FullArgSpec. This would require updating fire.docstrings.parser() and a
    #  decision would need to be made about which takes priority if the docstrings
    #  and function definition disagree.

    # The help string is indented, so calculate the maximum permitted length
    # before indentation to avoid exceeding the maximum line length.
    max_str_length = LINE_LENGTH - SECTION_INDENTATION - SUBSECTION_INDENTATION

    description = _GetArgDescription(flag, docstring_info)

    flag_string_template = '--{flag_name}={flag_name_upper}'
    flag_string = flag_string_template.format(
        flag_name=flag, flag_name_upper=formatting.Underline(flag.upper()))
    if required:
        flag_string += ' (required)'

    arg_type = _GetArgType(flag, spec)
    arg_default = _GetArgDefault(flag, spec)

    # We need to handle the case where there is a default
    # of None, but otherwise the argument has another type.
    if arg_default == 'None':
        arg_type = 'Optional[{}]'.format(arg_type)

    arg_type = 'Type: {}'.format(arg_type) if arg_type else ''
    available_space = max_str_length - len(arg_type)
    arg_type = \
      formatting.EllipsisTruncate(arg_type, available_space, max_str_length)

    arg_default = 'Default: {}'.format(arg_default) if arg_default else ''
    available_space = max_str_length - len(arg_default)
    arg_default = \
      formatting.EllipsisTruncate(arg_default, available_space, max_str_length)

    description = '\n'.join(part
                            for part in (arg_type, arg_default, description)
                            if part)

    return _CreateItem(flag_string, description, indent=SUBSECTION_INDENTATION)
Ejemplo n.º 3
0
def _CreateArgItem(arg, docstring_info, spec):
  """Returns a string describing a positional argument.

  Args:
    arg: The name of the positional argument.
    docstring_info: A docstrings.DocstringInfo namedtuple with information about
      the containing function's docstring.
    spec: An instance of fire.inspectutils.FullArgSpec, containing type and
     default information about the arguments to a callable.

  Returns:
    A string to be used in constructing the help screen for the function.
  """

  # The help string is indented, so calculate the maximum permitted length
  # before indentation to avoid exceeding the maximum line length.
  max_str_length = LINE_LENGTH - SECTION_INDENTATION - SUBSECTION_INDENTATION

  description = _GetArgDescription(arg, docstring_info)

  arg_string = formatting.BoldUnderline(arg.upper())

  arg_type = _GetArgType(arg, spec)
  arg_type = 'Type: {}'.format(arg_type) if arg_type else ''
  available_space = max_str_length - len(arg_type)
  arg_type = (
      formatting.EllipsisTruncate(arg_type, available_space, max_str_length))

  description = '\n'.join(part for part in (arg_type, description) if part)

  return _CreateItem(arg_string, description, indent=SUBSECTION_INDENTATION)
Ejemplo n.º 4
0
def GetStringTypeSummary(obj, available_space, line_length):
    """Returns a custom summary for string type objects.

  This function constructs a summary for string type objects by double quoting
  the string value. The double quoted string value will be potentially truncated
  with ellipsis depending on whether it has enough space available to show the
  full string value.

  Args:
    obj: The object to generate summary for.
    available_space: Number of character spaces available.
    line_length: The full width of the terminal, default is 80.

  Returns:
    A summary for the input object.
  """
    if len(obj) + len(TWO_DOUBLE_QUOTES) <= available_space:
        content = obj
    else:
        additional_len_needed = len(TWO_DOUBLE_QUOTES) + len(
            formatting.ELLIPSIS)
        if available_space < additional_len_needed:
            available_space = line_length
        content = formatting.EllipsisTruncate(
            obj, available_space - len(TWO_DOUBLE_QUOTES), line_length)
    return formatting.DoubleQuote(content)
Ejemplo n.º 5
0
def GetStringTypeDescription(obj, available_space, line_length):
    """Returns the predefined description for string obj.

  This function constructs a description for string type objects in the format
  of 'The string "<string_value>"'. <string_value> could be potentially
  truncated depending on whether it has enough space available to show the full
  string value.

  Args:
    obj: The object to generate description for.
    available_space: Number of character spaces available.
    line_length: The full width of the terminal, default if 80.

  Returns:
    A description for input object.
  """
    additional_len_needed = len(STRING_DESC_PREFIX) + len(
        TWO_DOUBLE_QUOTES) + len(formatting.ELLIPSIS)
    if available_space < additional_len_needed:
        available_space = line_length

    return STRING_DESC_PREFIX + formatting.DoubleQuote(
        formatting.EllipsisTruncate(
            obj, available_space - len(STRING_DESC_PREFIX) -
            len(TWO_DOUBLE_QUOTES), line_length))
Ejemplo n.º 6
0
 def test_ellipsis_truncate_not_enough_space(self):
   text = 'This is a string'
   truncated_text = formatting.EllipsisTruncate(
       text=text, available_space=2, line_length=LINE_LENGTH)
   self.assertEqual('This is a string', truncated_text)
Ejemplo n.º 7
0
 def test_ellipsis_truncate(self):
     text = "This is a string"
     truncated_text = formatting.EllipsisTruncate(text=text,
                                                  available_space=10,
                                                  line_length=LINE_LENGTH)
     self.assertEqual("This is...", truncated_text)