from typing import Sequence
import codecs


def _read_notebook(path: str, encoding: str) -> Sequence[str]:
    """
    Read a source notebook into a list of lines, removing trailing newlines.

    :param path:      the path to the source notebook
    :param encoding:  the encoding to use to read the notebook

    :return: the list of lines
    """
    buf = []
    with codecs.open(path, mode="r", encoding=encoding) as f:
        for line in f.readlines():
            # Don't use rstrip(), because we want to keep any trailing white
            # space, except for the trailing newline.
            if len(line) == 0:
                buf.append(line)
                continue
            if line[-1] != "\n":
                buf.append(line)
                continue
            buf.append(line[:-1])

    return buf