import re


def read_webrequest_in(filestream):
    """    
    Read a webrequest file and put data into a dictionary.
   
    Parameters
    ----------
    filestream : file object
        A file object that contains an open data file.
    
    Returns
    -------
    data : dictionary
        A dictionary holding data found in file. 

    Notes
    -----
    data = {"column names": [], "requests": []}
               
    The "requests" key in the data dictionary contains a list of dictionaries containing
    the requests information found in the requests file. For example:
    
    requests[0] = {"data type": str, "site number": str, "start date": str, "end date": str, "parameters": list of str}       
    """
    data_file = filestream.readlines()

    patterns = {
        "column_names": "(#)(.+)", 
        "dv_iv_row": "(dv|iv|)\t([0-9]+)\t([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})\t([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})\t(.+)", # match site only at the beginning of a line
        "site_row": "(site)\t([0-9]+)" 
    }  

    # initialize a dictionary to hold all the data 
    data = {
        "column names": [],
        "requests": [],
    } 

    for line in data_file: 
        match_column_names = re.search(pattern = patterns["column_names"], string = line)
        match_dv_iv_row = re.search(pattern = patterns["dv_iv_row"], string = line)
        match_site_row = re.search(pattern = patterns["site_row"], string = line)  
        
        if match_column_names:
            data["column names"] = match_column_names.group(2).strip().split("\t")
            
        if match_dv_iv_row:
            data["requests"].append({
                "data type": match_dv_iv_row.group(1),
                "site number": match_dv_iv_row.group(2),
                "start date": match_dv_iv_row.group(3),
                "end date": match_dv_iv_row.group(4),
                "parameters": match_dv_iv_row.group(5).strip().split("\t")                
            })

        if match_site_row:
            data["requests"].append({
                "data type": match_site_row.group(1),
                "site number": match_site_row.group(2),
                "start date": "",
                "end date": "",
                "parameters": ""                
            })

    return data