import importlib


def parse(podcast):
    """Parse a podcast source, edit the podcast's attribute, and return a list
    of the podcast's episode.

    Parameters
    ----------
    podcast : :class:`Podcast`
        The podcast to parse.

    Returns
    -------
    list of :class:`Episode`
        A list of the podcast's episodes.
    """
    if "." in podcast.parser:
        raise ValueError(
            "{} is not a valid parser name.".format(podcast.parser))

    try:
        parser = importlib.import_module(".".join((__name__, podcast.parser)))
    except ModuleNotFoundError:
        raise ValueError(
            "{} is not a valid parser name.".format(podcast.parser))

    episodes = parser.parse(podcast)

    for episode in episodes:
        episode.podcast = podcast

    return sorted(episodes, key=lambda e: e.pubdate)