Exemple #1
0
def parseInput(line):
    line = line.replace("\t", " ")
    cmd = line.split(" ")[0]
    data = line.replace(cmd,"").strip()
    if (cmd == 'add'):
        # Syntax: "add id,description,footprint,quantity"
        dict = parserecord.lineToDict(data)
        database.append(dict)
        print dict['id'], " has been added"
        writeToFile(database)
        return    
    if (cmd == 'remove'):
        # Syntax: "remove id"
        # Set the database equal to the database except IDs that should be
        # removed
        database[:] = [d for d in database if d.get('id') != data]
        print id, " has been removed"
        writeToFile(database)
        return
    if (cmd == 'list'):
        # Syntax: "list key value"
        # Get key and value
        key = data.split(" ")[0]
        value = data.replace(key,"").strip()
        if (key == "all"):
            for item in database:
                print parserecord.dictToLine(item)
        else:
            for item in [d for d in database if d.get(key) == value]:
                print parserecord.dictToLine(item)
        return
            
    # Check for invalid commands
    print "Invalid command!"
Exemple #2
0
def writeToFile(db):
    global args
    f = open(args.datafile, 'w')
    for entry in db:
        f.write(parserecord.dictToLine(entry))
        f.write("\n")
    f.close()