import csv
from pathlib import Path


def visit_simulation(sim_path):
    """
    Visit driving_log of a given simulation and extract steering angle and images.
    :param sim_path:
    :return:
    """
    csv_file = sim_path / "driving_log.csv"

    with open(csv_file) as f:
        driving_log = [{k: v for k, v in row.items()}
                       for row in csv.DictReader(f, skipinitialspace=True)]
    print("\nReading simulation:\t", driving_log[0]["sim_name"])
    print(">> Row read:\t", len(driving_log))

    # Extract and normalize images paths -------------------------------------------------------------------------------
    images_path = [Path(sim_path, "IMG", d["center"].rsplit("\\", 1)[1]) for d in driving_log]
    print(">> Images processed:\t", len(images_path))
    return driving_log, images_path