Repository: google/python-fire
File: fire/core.py (Python)

Code before:
  elif component_trace.show_trace:
    print('Fire trace:\n{trace}'.format(trace=component_trace))
    return component_trace
  elif component_trace.show_help:
    result = component_trace.GetResult()
    print(
        helputils.HelpString(result, component_trace, component_trace.verbose))
    return None
  else:
    _PrintResult(component_trace, verbose=component_trace.verbose)
    result = component_trace.GetResult()
    return result


def CompletionScript(name, component):
  """Returns the text of the Bash completion script for a Fire CLI."""
  return completion.Script(name, component)


class FireError(Exception):
  """Exception used by Fire when a Fire command cannot be executed.

  These exceptions are not raised by the Fire function, but rather are caught
  and added to the FireTrace.
  """


class FireExit(SystemExit):
  """An exception raised by Fire to the client in the case of a FireError.

  Contains the trace of the Fire program.

  If not caught, then a FireExit will cause the program to exit with a non-zero
  status code.
  """

  def __init__(self, status, data):
    super(FireExit, self).__init__(status)
    self.trace = data


def _PrintResult(component_trace, verbose=False):
  """Prints the result of the Fire call to stdout in a human readable way."""
  # TODO: Design human readable deserializable serialization method
  # and move serialization to it's own module.
  result = component_trace.GetResult()

  if isinstance(result, (list, set, types.GeneratorType)):
    for i in result:
      print(_OneLineResult(i))
  elif inspect.isgeneratorfunction(result):

Review comment (suggestion):
Should FireError be private?
They are stored in the FireTraces, which are now accessible outside of Fire via the FireExit.

Code after:
    raise FireExit(0, component_trace)
  elif component_trace.show_trace:
    print('Fire trace:\n{trace}'.format(trace=component_trace))
    raise FireExit(0, component_trace)
  elif component_trace.show_help:
    result = component_trace.GetResult()
    print(
        helputils.HelpString(result, component_trace, component_trace.verbose))
    raise FireExit(0, component_trace)
  else:
    _PrintResult(component_trace, verbose=component_trace.verbose)
    result = component_trace.GetResult()
    return result


def CompletionScript(name, component):
  """Returns the text of the Bash completion script for a Fire CLI."""
  return completion.Script(name, component)


class FireError(Exception):
  """Exception used by Fire when a Fire command cannot be executed.

  These exceptions are not raised by the Fire function, but rather are caught
  and added to the FireTrace.
  """


class FireExit(SystemExit):
  """An exception raised by Fire to the client in the case of a FireError.

  The trace of the Fire program is available on the `trace` property.

  This exception inherits from SystemExit, so clients may explicitly catch it
  with `except SystemExit` or `except FireExit`. If not caught, this exception
  will cause the client program to exit without a stacktrace.
  """

  def __init__(self, code, component_trace):
    """Constructs a FireExit exception.

    Args:
      code: (int) Exit code for the Fire CLI.
      component_trace: (FireTrace) The trace for the Fire command.
    """
    super(FireExit, self).__init__(code)
    self.trace = component_trace


def _PrintResult(component_trace, verbose=False):
  """Prints the result of the Fire call to stdout in a human readable way."""