from typing import Optional
from typing import IO


def openfile(spec: Optional[str]) -> Optional[IO[str]]:
    """Open file helper.

    Args:
        spec: file/mode spec; for example:

            * ``file`` uses mode ``w``
            * ``file+`` uses mode ``a``
            *  ``file+r`` uses mode ``r``

    Returns:
        File object, or ``None`` if spec is ``None``.
    """
    if spec is None:
        return None
    else:
        parts = spec.split('+', 1)
        name = parts[0]
        mode = parts[1] if len(parts) > 1 else 'w'
        mode = mode or 'a'
        return open(name, mode=mode, buffering=1)