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