import yaml
from io import StringIO
import json


def load_metadata(path):
  """Return the contents of a YAML or JSON path."""
  metadata = {}
  if path:
    with open(path, 'r') as stream:
      content = stream.read()
    try:
      doc = yaml.safe_load(StringIO(content))
    except (TypeError, ValueError, UnicodeEncodeError):
      doc = json.load(StringIO(content))
    metadata.update(doc)
        
  return metadata