import re
import io


def get_version(filename):
    """
    Trivial parser to extract a __version__ variable from a source file.

    :param str filename: the file to extract __version__ from
    :returns str: the version string for the package
    """
    version_re = re.compile(r'(\d\.\d(\.\d+)?)')
    with io.open(filename, 'r') as source:
        for line_num, line in enumerate(source):
            if line.startswith('__version__'):
                match = version_re.search(line)
                if not match:
                    raise Exception(
                        'Invalid __version__ string found on '
                        'line %d of %s' % (line_num + 1, filename))
                return match.group(1)
    raise Exception('No __version__ line found in %s' % filename)