5 from optparse import OptionParser
6 from pprint import pprint
8 from sfa.util.py23 import StringType
13 usage = "%(command)s [options]" % locals()
14 description = """getNodes will open a rspec file and print all key/values, or filter results based on a given key or set of keys."""
15 parser = OptionParser(usage=usage, description=description)
16 parser.add_option("-i", "--infile", dest="infile", default=None,
17 help = "input rspec file")
18 parser.add_option("-t", "--tag", dest="tag", default=None,
19 help = "filter rspec for this tag")
20 parser.add_option("-a", "--attribute", dest="attribute", default=None,
21 help = "comma separated list of attributes to display")
22 parser.add_option("-r", "--recursive", dest="print_children", default=False, action="store_true",
23 help = "print the tag's child nodes")
28 def print_dict(rdict, options, counter=1):
29 print_children = options.print_children
32 attributes = options.attribute.split(',')
36 if not isinstance(rdict, dict):
37 raise "%s not a dict" % rdict
38 for (key, value) in rdict.iteritems():
39 if isinstance(value, StringType):
40 if (attributes and key in attributes) or not attributes:
41 print tab * counter + "%s: %s" % (key, value)
42 elif isinstance(value, list):
43 for listitem in value:
44 if isinstance(listitem, dict):
45 lists.append((key, listitem))
46 elif isinstance(value, dict):
47 lists.append((key, value))
49 if counter == 1 or print_children:
50 for (key, listitem) in lists:
51 if isinstance(listitem, dict):
52 print tab * (counter - 1) + key
53 print_dict(listitem, options, counter+1)
54 elif not attributes or (attributes and 'children' in attributes):
55 keys = set([key for (key, listitem) in lists])
56 if keys: print tab * (counter) + "(children: %s)" % (",".join(keys))
60 # this code probably is obsolete
61 # RSpec is not imported, it does not have a toDict() method anyway
62 # plus, getNodes.py is not exposed in packaging
65 parser = create_parser();
66 (options, args) = parser.parse_args()
67 if not options.infile:
68 print "RSpec file not specified"
73 rspec.parseFile(options.infile)
75 print "Error reading rspec file"
78 tag_name = options.tag
79 rspec_dicts = rspec.getDictsByTagName(tag_name)
80 rspec_dict = {tag_name: rspec_dicts}
82 rspec_dict = rspec.toDict()
84 print_dict(rspec_dict, options)
88 if __name__ == '__main__':
90 except Exception as e: