7 faiyaza at cs dot princeton dot edu
8 Copyright (c) 2009 Board of Trustees, Princeton University
15 from optparse import OptionParser
16 from pprint import pprint
21 usage = "%(command)s [options]" % locals()
22 description = """setRecord will edit a record (from stdin), modify its contents, then print the new record to stdout"""
23 parser = OptionParser(usage=usage,description=description)
24 parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
25 default=False, help = "print debug info")
30 def editDict(args, recordDict, options):
32 Takes the arg list, seperates into tag/value, creates a dict, then munges args.
34 # find out if its iterable.
38 modDict({vect.split("+=")[0]: returnVal(vect.split("+=")[1])},
43 replaceDict({vect.split("=")[0]: returnVal("=".join(vect.split("=")[1:]))},
46 if vect in recordDict:
49 raise TypeError, "Argument error: Records are updated with \n" \
50 "key=val1,val2,valN or\n" \
51 "key+=val1,val2,valN \n%s Unknown key/val" % vect
54 def replaceDict(newval, recordDict, options):
56 Replaces field in dict
58 # Check type of old field matches type of new field
59 for (key, val) in newval.iteritems():
62 def modDict(newval, recordDict, options):
64 Checks type of existing field, addends new field
66 for (key, val) in newval.iteritems():
67 if (type(recordDict[key]) == list):
68 if (type(val) == list):
69 recordDict[key] = recordDict[key] + val
71 recordDict[key].append(val)
72 elif type(val) == list:
73 val.append(recordDict[key])
76 recordDict[key] = [recordDict[key], val]
81 if given input has ",", then its assumed to be a list.
84 return list(arg.split(","))
89 parser = create_parser();
90 (options, args) = parser.parse_args()
92 record = RecordSpec(xml = sys.stdin.read())
95 editDict(args, record.dict["record"], options)
97 print "New Record:\n%s" % record.dict
100 record.parseDict(record.dict)
104 if __name__ == '__main__':