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