updated document
[sfa.git] / cmdline / 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 from geni.util.rspec import Rspec
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,  help = "input rspec file")
17     parser.add_option("-t", "--tag", dest="tag", default=None,  help = "filter rspec for this tag")
18     parser.add_option("-a", "--attribute", dest="attribute", default=None,  help = "comma separated list of attributes to display")
19     parser.add_option("-r", "--recursive", dest="print_children", default=False,  action="store_true", help = "print the tag's child nodes")
20
21     return parser    
22
23
24 def print_dict(rdict, options, counter=1):
25     print_children = options.print_children
26     attributes = []
27     if options.attribute: 
28         attributes = options.attribute.split(',') 
29     lists = []
30     tab = "    "
31     
32     if not isinstance(rdict, dict):
33         raise "%s not a dict" % rdict 
34     for (key, value) in rdict.items():
35         if isinstance(value, StringTypes):
36             if (attributes and key in attributes) or not attributes:
37                 print tab * counter + "%s: %s" % (key, value)
38         elif isinstance(value, list):
39             for listitem in value:
40                 if isinstance(listitem, dict):
41                     lists.append((key, listitem))
42         elif isinstance(value, dict):
43             lists.append((key, value)) 
44     
45     if counter == 1 or print_children: 
46         for (key, listitem) in lists:
47             if isinstance(listitem, dict):
48                 print tab * (counter - 1) + key
49                 print_dict(listitem, options, counter+1)
50     elif not attributes or (attributes and 'children' in attributes):
51         keys = set([key for (key, listitem) in lists])
52         if keys: print tab * (counter) + "(children: %s)" % (",".join(keys))    
53         
54
55 def main():
56     parser = create_parser(); 
57     (options, args) = parser.parse_args()
58     if not options.infile:
59         print "Rspec file not specified"
60         return 
61         
62     rspec = Rspec()
63     try:
64         rspec.parseFile(options.infile)
65     except:
66         print "Error reading rspec file"
67
68     if options.tag:
69         tag_name = options.tag
70         rspec_dicts = rspec.getDictsByTagName(tag_name)
71         rspec_dict = {tag_name: rspec_dicts}
72     else:
73         rspec_dict = rspec.toDict()  
74   
75     print_dict(rspec_dict, options)
76
77     return
78
79 if __name__ == '__main__':
80     try: main()
81     except Exception, e:
82         raise
83         print e