5 from optparse import OptionParser
6 from pprint import pprint
7 from types import StringTypes
8 from geni.util.rspec import Rspec
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")
24 def print_dict(rdict, options, counter=1):
25 print_children = options.print_children
28 attributes = options.attribute.split(',')
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))
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))
56 parser = create_parser();
57 (options, args) = parser.parse_args()
58 if not options.infile:
59 print "Rspec file not specified"
64 rspec.parseFile(options.infile)
66 print "Error reading rspec file"
69 tag_name = options.tag
70 rspec_dicts = rspec.getDictsByTagName(tag_name)
71 rspec_dict = {tag_name: rspec_dicts}
73 rspec_dict = rspec.toDict()
75 print_dict(rspec_dict, options)
79 if __name__ == '__main__':