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.items():
37 if isinstance(value, str):
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])
55 print(tab * (counter) + "(children: %s)" % (",".join(keys)))
59 # this code probably is obsolete
60 # RSpec is not imported, it does not have a toDict() method anyway
61 # plus, getNodes.py is not exposed in packaging
64 parser = create_parser()
65 (options, args) = parser.parse_args()
66 if not options.infile:
67 print("RSpec file not specified")
72 rspec.parseFile(options.infile)
74 print("Error reading rspec file")
77 tag_name = options.tag
78 rspec_dicts = rspec.getDictsByTagName(tag_name)
79 rspec_dict = {tag_name: rspec_dicts}
81 rspec_dict = rspec.toDict()
83 print_dict(rspec_dict, options)
87 if __name__ == '__main__':
90 except Exception as e: