from ps_fields import *
import mongodb

def parsePsOutput(output):
    data = output.split('\n')
    data = list(filter(bool, [line.split() for line in data]))
    header = data[0]
    procs = []

    for line in data[1:]:
        proc = {}
        for i in range(len(header)):
            field = field_defs[header[i]]
            value = line[i]
            if i == len(header):
                # combine command arguments together to one string
                value = ' '.join(line[i:])

            value = field['dtype'](value)
            if 'mult' in field:
                # mult if applicable
                value *= field['mult']

            proc[header[i]] = value

        procs.append(proc)
    return procs

def parseFile(fname):
    f = open(fname, 'r')
    data = f.read()
    f.close()
    return parsePsOutput(data)
