5 from optparse import OptionParser
6 from pprint import pprint
8 from sfa.util.py23 import StringType
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,
18 help="input rspec file")
19 parser.add_option("-t", "--tag", dest="tag", default=None,
20 help="filter rspec for this tag")
21 parser.add_option("-a", "--attribute", dest="attribute", default=None,
22 help="comma separated list of attributes to display")
23 parser.add_option("-r", "--recursive", dest="print_children", default=False, action="store_true",
24 help="print the tag's child nodes")
29 def print_dict(rdict, options, counter=1):
30 print_children = options.print_children
33 attributes = options.attribute.split(',')
37 if not isinstance(rdict, dict):
38 raise "%s not a dict" % rdict
39 for (key, value) in rdict.iteritems():
40 if isinstance(value, StringType):
41 if (attributes and key in attributes) or not attributes:
42 print tab * counter + "%s: %s" % (key, value)
43 elif isinstance(value, list):
44 for listitem in value:
45 if isinstance(listitem, dict):
46 lists.append((key, listitem))
47 elif isinstance(value, dict):
48 lists.append((key, value))
50 if counter == 1 or print_children:
51 for (key, listitem) in lists:
52 if isinstance(listitem, dict):
53 print tab * (counter - 1) + key
54 print_dict(listitem, options, counter + 1)
55 elif not attributes or (attributes and 'children' in attributes):
56 keys = set([key for (key, listitem) in lists])
58 print tab * (counter) + "(children: %s)" % (",".join(keys))
62 # this code probably is obsolete
63 # RSpec is not imported, it does not have a toDict() method anyway
64 # plus, getNodes.py is not exposed in packaging
67 parser = create_parser()
68 (options, args) = parser.parse_args()
69 if not options.infile:
70 print "RSpec file not specified"
75 rspec.parseFile(options.infile)
77 print "Error reading rspec file"
80 tag_name = options.tag
81 rspec_dicts = rspec.getDictsByTagName(tag_name)
82 rspec_dict = {tag_name: rspec_dicts}
84 rspec_dict = rspec.toDict()
86 print_dict(rspec_dict, options)
90 if __name__ == '__main__':
93 except Exception as e: