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