renaming the toplevel geni/ package into sfa/
[sfa.git] / sfa / client / setRecord.py
1 #!/usr/bin/python
2
3 """
4 Updates record objects
5
6
7 faiyaza at cs dot princeton dot edu
8 Copyright (c) 2009 Board of Trustees, Princeton University
9
10 $Id$
11 $HeadURL$
12 """
13
14 import sys
15 import os
16 from optparse import OptionParser
17 from pprint import pprint
18
19 from sfa.util.rspec import RecordSpec
20
21
22 def create_parser():
23     command = sys.argv[0]
24     argv = sys.argv[1:]
25     usage = "%(command)s [options]" % locals()
26     description = """setRecord will edit a record (from stdin), modify its contents, then print the new record to stdout"""
27     parser = OptionParser(usage=usage,description=description)
28     parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
29         default=False,  help = "print debug info")
30    
31     return parser    
32
33
34 def editDict(args, recordDict, options):
35     """
36     Takes the arg list, seperates into tag/value, creates a dict, then munges args.
37     """
38     # find out if its iterable.
39     for vect in args:
40         if vect.count("+="):
41             # append value
42             modDict({vect.split("+=")[0]: returnVal(vect.split("+=")[1])},
43                          recordDict, options) 
44  
45         elif vect.count("="):
46             # reassign value
47             replaceDict({vect.split("=")[0]: returnVal(vect.split("=")[1])},
48                          recordDict, options) 
49         else:
50             raise TypeError, "Argument error: Records are updated with \n" \
51                             "key=val1,val2,valN or\n" \
52                             "key+=val1,val2,valN \n%s Unknown key/val" % vect
53
54
55 def replaceDict(newval, recordDict, options):
56     """
57     Replaces field in dict
58     """
59     # Check type of old field matches type of new field
60     for (key, val) in newval.iteritems():
61         recordDict[key] = val
62
63 def modDict(newval, recordDict, options):
64     """
65     Checks type of existing field, addends new field
66     """
67     for (key, val) in newval.iteritems():
68         if (type(recordDict[key]) == list):
69             if (type(val) == list):
70                 recordDict[key] = recordDict[key] + val
71             else:
72                 recordDict[key].append(val)
73         elif type(val) == list:
74             val.append(recordDict[key])
75             recordDict[key] = val
76         else:
77             recordDict[key] = [recordDict[key], val]
78
79
80 def returnVal(arg):
81     """
82     if given input has ",", then its assumed to be a list.
83     """
84     if arg.count(","):
85         return list(arg.split(","))
86     else:
87         return arg
88
89 def main():
90     parser = create_parser(); 
91     (options, args) = parser.parse_args()
92
93     record = RecordSpec(xml = sys.stdin.read())
94
95     if args:
96         editDict(args, record.dict["record"], options)
97     if options.DEBUG:
98         print "New Record:\n%s" % record.dict
99         record.pprint()
100
101     record.parseDict(record.dict)
102     s = record.toxml()
103     sys.stdout.write(s)
104
105 if __name__ == '__main__':
106     try: main()
107     except Exception, e:
108         print e