From 069a47b668e7efcae19adc5bec6396c144c6cb61 Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Thu, 18 Jun 2009 00:05:15 +0000 Subject: [PATCH] added -a option. now users can specify a comma separated list of attributes to display --- cmdline/getNodes.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/cmdline/getNodes.py b/cmdline/getNodes.py index 4c0df226..df252a5e 100644 --- a/cmdline/getNodes.py +++ b/cmdline/getNodes.py @@ -14,21 +14,27 @@ def create_parser(): description = """getNodes will open a rspec file and print all key/values, or filter results based on a given key or set of keys.""" parser = OptionParser(usage=usage,description=description) parser.add_option("-i", "--infile", dest="infile", default=None, help = "input rspec file") - parser.add_option("-f", "--filter", dest="filter", default=None, help = "filter rspec for this tag") + parser.add_option("-t", "--tag", dest="tag", default=None, help = "filter rspec for this tag") + parser.add_option("-a", "--attribute", dest="attribute", default=None, help = "comma separated list of attributes to display") parser.add_option("-r", "--recursive", dest="print_children", default=False, action="store_true", help = "print the tag's child nodes") - + return parser -print -def print_dict(rdict, print_children, counter=1): +def print_dict(rdict, options, counter=1): + print_children = options.print_children + attributes = [] + if options.attribute: + attributes = options.attribute.split(',') lists = [] tab = " " + if not isinstance(rdict, dict): raise "%s not a dict" % rdict for (key, value) in rdict.items(): if isinstance(value, StringTypes): - print tab * counter + "%s: %s" % (key, value) + if (attributes and key in attributes) or not attributes: + print tab * counter + "%s: %s" % (key, value) elif isinstance(value, list): for listitem in value: if isinstance(listitem, dict): @@ -40,8 +46,8 @@ def print_dict(rdict, print_children, counter=1): for (key, listitem) in lists: if isinstance(listitem, dict): print tab * (counter - 1) + key - print_dict(listitem, print_children, counter+1) - else: + print_dict(listitem, options, counter+1) + elif not attributes or (attributes and 'children' in attributes): keys = set([key for (key, listitem) in lists]) if keys: print tab * (counter) + "(children: %s)" % (",".join(keys)) @@ -49,7 +55,6 @@ def print_dict(rdict, print_children, counter=1): def main(): parser = create_parser(); (options, args) = parser.parse_args() - if not options.infile: print "Rspec file not specified" return @@ -60,14 +65,14 @@ def main(): except: print "Error reading rspec file" - if options.filter: - filter_name = options.filter - rspec_dicts = rspec.getDictsByTagName(options.filter) - rspec_dict = {filter_name: rspec_dicts} + if options.tag: + tag_name = options.tag + rspec_dicts = rspec.getDictsByTagName(tag_name) + rspec_dict = {tag_name: rspec_dicts} else: - rspec_dict = rspec.toDict() - - print_dict(rspec_dict, options.print_children) + rspec_dict = rspec.toDict() + + print_dict(rspec_dict, options) return -- 2.43.0