5 from optparse import OptionParser
6 from pprint import pprint
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")
26 def print_dict(rdict, options, counter=1):
27 print_children = options.print_children
30 attributes = options.attribute.split(',')
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))
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))
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
63 parser = create_parser();
64 (options, args) = parser.parse_args()
65 if not options.infile:
66 print "RSpec file not specified"
71 rspec.parseFile(options.infile)
73 print "Error reading rspec file"
76 tag_name = options.tag
77 rspec_dicts = rspec.getDictsByTagName(tag_name)
78 rspec_dict = {tag_name: rspec_dicts}
80 rspec_dict = rspec.toDict()
82 print_dict(rspec_dict, options)
86 if __name__ == '__main__':
88 except Exception as e: