docs: def format_item(format_spec, item, defaults=None):
      """ Format an item according to the given output format.
          The format can be gioven as either an interpolation string,
          or a Tempita template (which has to start with "E{lb}E{lb}"),
  
          @param format_spec: The output format.
          @param item: The object, which is automatically wrapped for interpolation.
          @param defaults: Optional default values.
      """
      template_engine = getattr(format_spec, "__engine__", None)
  
      # TODO: Make differences between engines transparent
      if template_engine == "tempita" or (not template_engine and format_spec.startswith("{{")):
          # Set item, or field names for column titles
          namespace = dict(headers=not bool(item))
          if item:
              namespace["d"] = item
          else:
              namespace["d"] = Bunch()
              for name in engine.FieldDefinition.FIELDS:
                  namespace["d"][name] = name.upper()
  
              # Justify headers to width of a formatted value
              namespace.update((name[4:], lambda x, m=method: str(x).rjust(len(str(m(0)))))
                  for name, method in globals().items()
                  if name.startswith("fmt_")
              )
  
          return expand_template(format_spec, namespace)
      else:
          # Interpolation
          format_spec = getattr(format_spec, "fmt", format_spec)
  
          if item is None:
              # For headers, ensure we only have string formats
              format_spec = re.sub(
                  r"(\([_.a-zA-Z0-9]+\)[-#+0 ]?[0-9]*?)[.0-9]*[diouxXeEfFgG]",
                  lambda m: m.group(1) + 's', format_spec)
  
          return format_spec % OutputMapping(item, defaults)