import re
from datetime import datetime
import pytz


def get_bcbio_timings(path):
    """Fetch timing information from a bcbio log file."""
    with open(path, 'r') as fp:
        steps = {}
        for line in fp:
            matches = re.search(r'^\[([^\]]+)\] ([^:]+: .*)', line)
            if not matches:
                continue

            tstamp = matches.group(1)
            msg = matches.group(2)

            if not msg.find('Timing: ') >= 0:
                continue

            when = datetime.strptime(tstamp, '%Y-%m-%dT%H:%MZ').replace(
                tzinfo=pytz.timezone('UTC'))
            step = msg.split(":")[-1].strip()
            steps[when] = step

        return steps