5 from optparse import OptionParser
6 from pprint import pprint
7 from types import StringTypes
9 from geni.util.rspec import Rspec
14 usage = "%(command)s [options]" % locals()
15 description = """getNodes will open a rspec file and print all key/values, or filter results based on a given key or set of keys."""
16 parser = OptionParser(usage=usage,description=description)
17 parser.add_option("-i", "--infile", dest="infile", default=None, help = "input rspec file")
18 parser.add_option("-t", "--tag", dest="tag", default=None, help = "filter rspec for this tag")
19 parser.add_option("-a", "--attribute", dest="attribute", default=None, help = "comma separated list of attributes to display")
20 parser.add_option("-r", "--recursive", dest="print_children", default=False, action="store_true", help = "print the tag's child nodes")
25 def print_dict(rdict, options, counter=1):
26 print_children = options.print_children
29 attributes = options.attribute.split(',')
33 if not isinstance(rdict, dict):
34 raise "%s not a dict" % rdict
35 for (key, value) in rdict.items():
36 if isinstance(value, StringTypes):
37 if (attributes and key in attributes) or not attributes:
38 print tab * counter + "%s: %s" % (key, value)
39 elif isinstance(value, list):
40 for listitem in value:
41 if isinstance(listitem, dict):
42 lists.append((key, listitem))
43 elif isinstance(value, dict):
44 lists.append((key, value))
46 if counter == 1 or print_children:
47 for (key, listitem) in lists:
48 if isinstance(listitem, dict):
49 print tab * (counter - 1) + key
50 print_dict(listitem, options, counter+1)
51 elif not attributes or (attributes and 'children' in attributes):
52 keys = set([key for (key, listitem) in lists])
53 if keys: print tab * (counter) + "(children: %s)" % (",".join(keys))
57 parser = create_parser();
58 (options, args) = parser.parse_args()
59 if not options.infile:
60 print "Rspec file not specified"
65 rspec.parseFile(options.infile)
67 print "Error reading rspec file"
70 tag_name = options.tag
71 rspec_dicts = rspec.getDictsByTagName(tag_name)
72 rspec_dict = {tag_name: rspec_dicts}
74 rspec_dict = rspec.toDict()
76 print_dict(rspec_dict, options)
80 if __name__ == '__main__':