def generate_one_or_none(generator, enforce=True):
    """
    Return first item from a generator.

    Optionally check that nothing else was generated.

    Return None if nothing was generated.
    """
    result = None
    for item in generator:
        result = item
        break
    if enforce:
        for item in generator:
            raise RuntimeError('Generator "%s" had more than one item.' % generator.__name__)
    return result