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
18 from sfa.util.rspec import RecordSpec
24 usage = "%(command)s [options]" % locals()
25 description = """setRecord will edit a record (from stdin), modify its contents, then print the new record to stdout"""
26 parser = OptionParser(usage=usage,description=description)
27 parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
28 default=False, help = "print debug info")
33 def editDict(args, recordDict, options):
35 Takes the arg list, seperates into tag/value, creates a dict, then munges args.
37 # find out if its iterable.
41 modDict({vect.split("+=")[0]: returnVal(vect.split("+=")[1])},
46 replaceDict({vect.split("=")[0]: returnVal("=".join(vect.split("=")[1:]))},
49 if vect in recordDict:
52 raise TypeError, "Argument error: Records are updated with \n" \
53 "key=val1,val2,valN or\n" \
54 "key+=val1,val2,valN \n%s Unknown key/val" % vect
57 def replaceDict(newval, recordDict, options):
59 Replaces field in dict
61 # Check type of old field matches type of new field
62 for (key, val) in newval.iteritems():
65 def modDict(newval, recordDict, options):
67 Checks type of existing field, addends new field
69 for (key, val) in newval.iteritems():
70 if (type(recordDict[key]) == list):
71 if (type(val) == list):
72 recordDict[key] = recordDict[key] + val
74 recordDict[key].append(val)
75 elif type(val) == list:
76 val.append(recordDict[key])
79 recordDict[key] = [recordDict[key], val]
84 if given input has ",", then its assumed to be a list.
87 return list(arg.split(","))
92 parser = create_parser();
93 (options, args) = parser.parse_args()
95 record = RecordSpec(xml = sys.stdin.read())
98 editDict(args, record.dict["record"], options)
100 print "New Record:\n%s" % record.dict
103 record.parseDict(record.dict)
107 if __name__ == '__main__':