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