from typing import Dict
from typing import List
from typing import Optional


def get_dict_from_cu_inst_result_file(cu_inst_result_file: str) -> Dict[str, List[Dict[str, Optional[str]]]]:
    """Parses the information contained in cu_inst_result_file into a dictionary of dictionaries,
    ordered by dependency type and returns the dictionary.
    :param cu_inst_result_file: Path (string) to cu_inst_result file
    :return: parsed dictionary"""
    res_dict: Dict[str, List[Dict[str, Optional[str]]]] = {'RAW': [], 'WAR': [], 'WAW': []}
    try:
        with open(cu_inst_result_file) as f:
            for line in f:
                line = line.replace("\n", "")
                line_split = line.split(" ")
                dep_type = line_split[0]
                target_type = line_split[2]
                if target_type == "line:":
                    target_function = None
                    target_line = line_split[3]
                    target_var = line_split[5]
                elif target_type == "function:":
                    target_function = line_split[3]
                    target_line = line_split[5]
                    target_var = line_split[7]
                else:
                    raise ValueError("Unknown type: ", target_type)
                cur_line_dict = {'function': target_function, 'line': target_line.replace(",", ""), 'var': target_var}
                res_dict[dep_type].append(cur_line_dict)
    except FileNotFoundError as ex:
        raise ex
    return res_dict