import warnings


def parse_sacct(sacct_str):
    """Convert output of ``sacct -p`` into a dictionary.

    Parses the output of ``sacct -p`` and return a dictionary with the full (raw)
    contents.

    Args:
        sacct_str (str): stdout of an invocation of ``sacct -p``

    Returns:
        dict: Keyed by Slurm Job ID and whose values are dicts containing
        key-value pairs corresponding to the Slurm quantities returned
        by ``sacct -p``.
    """
    result = {}
    cols = []
    for lineno, line in enumerate(sacct_str.splitlines()):
        fields = line.split('|')
        if lineno == 0:
            cols = [x.lower() for x in fields]
        else:
            record = {}
            jobidraw = fields[0]
            if jobidraw in result:
                warnings.warn("Duplicate raw jobid '%s' found" % jobidraw)
            for col, key in enumerate(cols):
                if key:
                    record[key] = fields[col]
            result[jobidraw] = record
    return result