7 faiyaza at cs dot princeton dot edu
8 Copyright (c) 2009 Board of Trustees, Princeton University
17 from optparse import OptionParser
18 from pprint import pprint
20 from sfa.util.rspec import RecordSpec
26 usage = "%(command)s [options]" % locals()
27 description = """setRecord will edit a record (from stdin), modify its contents, then print the new record to stdout"""
28 parser = OptionParser(usage=usage,description=description)
29 parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
30 default=False, help = "print debug info")
35 def editDict(args, recordDict, options):
37 Takes the arg list, seperates into tag/value, creates a dict, then munges args.
39 # find out if its iterable.
43 modDict({vect.split("+=")[0]: returnVal(vect.split("+=")[1])},
48 replaceDict({vect.split("=")[0]: returnVal("=".join(vect.split("=")[1:]))},
51 if vect in recordDict:
54 raise TypeError, "Argument error: Records are updated with \n" \
55 "key=val1,val2,valN or\n" \
56 "key+=val1,val2,valN \n%s Unknown key/val" % vect
59 def replaceDict(newval, recordDict, options):
61 Replaces field in dict
63 # Check type of old field matches type of new field
64 for (key, val) in newval.iteritems():
67 def modDict(newval, recordDict, options):
69 Checks type of existing field, addends new field
71 for (key, val) in newval.iteritems():
72 if (type(recordDict[key]) == list):
73 if (type(val) == list):
74 recordDict[key] = recordDict[key] + val
76 recordDict[key].append(val)
77 elif type(val) == list:
78 val.append(recordDict[key])
81 recordDict[key] = [recordDict[key], val]
86 if given input has ",", then its assumed to be a list.
89 return list(arg.split(","))
94 parser = create_parser();
95 (options, args) = parser.parse_args()
97 record = RecordSpec(xml = sys.stdin.read())
100 editDict(args, record.dict["record"], options)
102 print "New Record:\n%s" % record.dict
105 record.parseDict(record.dict)
109 if __name__ == '__main__':