Changed Rspec --> RSpec throughout.
[sfa.git] / sfa / client / getNodes.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5 from optparse import OptionParser
6 from pprint import pprint
7 from types import StringTypes
8
9 from sfa.util.rspec import RSpec
10
11 def create_parser():
12     command = sys.argv[0]
13     argv = sys.argv[1:]
14     usage = "%(command)s [options]" % locals()
15     description = """getNodes will open a rspec file and print all key/values, or filter results based on a given key or set of keys."""
16     parser = OptionParser(usage=usage,description=description)
17     parser.add_option("-i", "--infile", dest="infile", default=None,  help = "input rspec file")
18     parser.add_option("-t", "--tag", dest="tag", default=None,  help = "filter rspec for this tag")
19     parser.add_option("-a", "--attribute", dest="attribute", default=None,  help = "comma separated list of attributes to display")
20     parser.add_option("-r", "--recursive", dest="print_children", default=False,  action="store_true", help = "print the tag's child nodes")
21
22     return parser    
23
24
25 def print_dict(rdict, options, counter=1):
26     print_children = options.print_children
27     attributes = []
28     if options.attribute: 
29         attributes = options.attribute.split(',') 
30     lists = []
31     tab = "    "
32     
33     if not isinstance(rdict, dict):
34         raise "%s not a dict" % rdict 
35     for (key, value) in rdict.items():
36         if isinstance(value, StringTypes):
37             if (attributes and key in attributes) or not attributes:
38                 print tab * counter + "%s: %s" % (key, value)
39         elif isinstance(value, list):
40             for listitem in value:
41                 if isinstance(listitem, dict):
42                     lists.append((key, listitem))
43         elif isinstance(value, dict):
44             lists.append((key, value)) 
45     
46     if counter == 1 or print_children: 
47         for (key, listitem) in lists:
48             if isinstance(listitem, dict):
49                 print tab * (counter - 1) + key
50                 print_dict(listitem, options, counter+1)
51     elif not attributes or (attributes and 'children' in attributes):
52         keys = set([key for (key, listitem) in lists])
53         if keys: print tab * (counter) + "(children: %s)" % (",".join(keys))    
54         
55
56 def main():
57     parser = create_parser(); 
58     (options, args) = parser.parse_args()
59     if not options.infile:
60         print "RSpec file not specified"
61         return 
62         
63     rspec = RSpec()
64     try:
65         rspec.parseFile(options.infile)
66     except:
67         print "Error reading rspec file"
68
69     if options.tag:
70         tag_name = options.tag
71         rspec_dicts = rspec.getDictsByTagName(tag_name)
72         rspec_dict = {tag_name: rspec_dicts}
73     else:
74         rspec_dict = rspec.toDict()  
75   
76     print_dict(rspec_dict, options)
77
78     return
79
80 if __name__ == '__main__':
81     try: main()
82     except Exception, e:
83         raise
84         print e