from typing import OrderedDict


def parse_vizzy_file(tracefile: str) -> OrderedDict:
    """Iterate through trace file lines and parse data into an ordered dictionary

    Converts the nanoseconds to seconds and adds to seconds field to get absolute time of traced
    event in seconds (as a float)

    Args:
      tracefile: Path to vizzy trace file

    Returns:
      Ordered dictionary of parsed data
    """

    dataset = OrderedDict()
    with open(tracefile, 'r') as _file:
        first_line = True
        starttime = 0
        for line in _file.readlines():
            line = line.strip()
            elements = line.split(',')

            seconds = float(elements[0])
            nanoseconds = float(elements[1]) / 1000000.0
            abstime = seconds+nanoseconds

            function = elements[2]
            address = int(elements[3], 16)
            size = None
            if elements[4]:
                size = int(elements[4])

            reltime = 0
            if first_line:
                starttime = abstime
                first_line = False
            else:
                reltime = abstime-starttime

            dataset[reltime] = dict(
                function=function,
                address=address,
                size=size,
            )

    return dataset