import re


def get_requirements(path=None):
    """get a list of requirements and any dependency links associated.

    This function fascilitates git urls being in requirements.txt
    and installing them as normall just like pip install -r requirements.txt
    would but setup does not by default.

    :e.g requirements.txt\::

        ConfigArgParse
        git+https://github.com/DreamingRaven/python-ezdb.git#branch=master

    should result in

    :requirements\::

        [
            ConfigArgParse
            python-ezdb
        ]

    : dependenc links\::

        ["git+https://github.com/DreamingRaven/python-ezdb.git#egg=python-ezdb"]

    version can also be appended but that may break compatibility with
    pip install -r requirements.txt so we will not attempt that here but would
    look something like this:

    "git+https://github.com/DreamingRaven/python-ezdb.git#egg=python-ezdb-0.0.1"
    """
    dependency_links = []

    #  read in the requirements file
    path = path if path is not None else "./requirements.txt"
    with open(path, "r") as f:
        requirements = f.read().splitlines()

    # apply regex to find all desired groups like package name and url
    re_git_url = r"^\bgit.+/(.+)\.git"
    re_groups = list(map(lambda x: re.search(re_git_url, x), requirements))

    # iterate over regex and select package name group to insert over url
    for i, content in enumerate(re_groups):
        # re.search can return None so only if it returned something
        if(content):
            print(i, content, requirements[i])
            requirements[i] = content.group(1)
            dependency_links.append("{}#egg={}".format(content.group(0),
                                                       content.group(1)))

    return requirements, dependency_links