import re
import ast


def extract_version(path):
    """
    Reads the file at the specified path and returns the version contained in it.

    This is meant for reading the __init__.py file inside a package, and so it
    expects a version field like:

    __version__ = '1.0.0'

    :param path: path to the Python file
    :return: the version inside the file
    """

    # Regular expression for the version
    _version_re = re.compile(r'__version__\s+=\s+(.*)')

    with open(path + '__init__.py', 'r', encoding='utf-8') as f:
        version = f.read()

    if version:
        version = _version_re.search(version)
        if version:
            version = version.group(1)
            version = str(ast.literal_eval(version.rstrip()))
            extracted = version
        else:
            extracted = None
    else:
        extracted = None

    return extracted