def addDept(dept): if XMLConnection.isDepartment(departments, dept): return 1 else: departments.append(dept) XMLConnection.writeXML(departments, people) return 1 return 0
def handleMessage(msg): print "Got Message ["+msg+"]" #UPD: Updates a record, using the updateID method above, and for now prints out the change if msg.startswith("UPD: "): #uhits = uhits+1 msg = msg.replace("UPD: ", "").strip() parts = msg.split("|") ID = parts.pop(0) uperson = updateID(ID, parts) print uperson.getFirstName(), uperson.getLastName(), uperson.getPhoneNumber() XMLConnection.writeXML(departments, people) return "OK" #Prints out the department list, nothing fancy elif msg.startswith("DEPTLIST"): for dept in departments: print dept #if either the QRY or AQRY commands are sent to the server, this takes care of them. #the QRY or AQRY tag is removed, newline characters chopped, and if the command is #AQRY then a flag is set, so that we know to return the ID with the result. elif msg.startswith("QRY: " or "AQRY: "): aqry = 0 if msg.startswith("AQRY: "): msg = msg.replace("QRY: ", "").strip() aqry = 1 else: msg = msg.replace("QRY: ", "").strip() results = queryPipes(msg.split("|")) retMsg = "" for person in results: if aqry == 1: retMsg = retMsg + person.getID() + "|" + person.getAll("|") + "\n" else: retMsg = retMsg + person.getAll("|") + "\n" return retMsg elif msg.startswith("GETFILE\n"): msg.replace("GETFILE\n", "") f = open("wow.xml", "w") for line in msg: f.write(line) #Create a person with given attributes, and add them to our list above - then write #the xml document. elif msg.startswith("ADD: "): msg = msg.replace("ADD: ", "").strip() parts = msg.split("|") addDept(parts[6]) people.append(Person(len(people), parts)) XMLConnection.writeXML(departments, people) return "OK" #Adds a department to our list, and then updates the xml elif msg.startswith("ADDDEPT: "): msg = msg.replace("ADDDEPT: ", "").strip() departments.append(msg) XMLConnection.writeXML(departments, people) elif msg.startswith("VER"): return serverVersion elif msg.startswith("SETVER: "): msg.replace("SETVER: ", "").strip() serverVersion = msg Log.writeLog("Updated to version:", msg) else: return 0