import inspect
import re


def get_caller(stack_index=2, root_dir=None):
  """Get caller's file and line number information.
  Args:
    stack_index: a stack_index of 2 will provide the caller of the
      function calling this function. Notice that stack_index of 2
      or more will fail if called from global scope.
    root_dir: the root dir prefixed to the file name. The root_dir
      will be trimmed from the file name in the output.
  Returns:
    Call site info in a dictionary with these fields:
      "file": (string) file path
      "lineno": (int) line number
  """
  caller = inspect.getframeinfo(inspect.stack()[stack_index][0])

  # Trim the filenames for readability.
  filename = caller.filename
  if root_dir is not None:
    filename = re.sub('^' + root_dir + '/', '', filename)
  return {"file": filename, "lineno": caller.lineno}