X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=clientbin%2FgetNodes.py;h=89689a5b8a020d4f2a0c3eb65ba0e23566e18250;hb=0a9902d2a55a0a9ac03601345c4284293669012b;hp=d1c1623f15a1646d8e04153958556672f2108d4d;hpb=faa98a764e6a311400ac54933fca910c8bdd1cf2;p=sfa.git diff --git a/clientbin/getNodes.py b/clientbin/getNodes.py index d1c1623f..89689a5b 100644 --- a/clientbin/getNodes.py +++ b/clientbin/getNodes.py @@ -5,6 +5,9 @@ import os from optparse import OptionParser from pprint import pprint +from sfa.util.py23 import StringType + + def create_parser(): command = sys.argv[0] argv = sys.argv[1:] @@ -12,29 +15,29 @@ 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") + help="input rspec file") parser.add_option("-t", "--tag", dest="tag", default=None, - help = "filter rspec for this tag") + help="filter rspec for this tag") parser.add_option("-a", "--attribute", dest="attribute", default=None, - help = "comma separated list of attributes to display") + 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") + help="print the tag's child nodes") - return parser + return parser def print_dict(rdict, options, counter=1): print_children = options.print_children attributes = [] - if options.attribute: - attributes = options.attribute.split(',') + if options.attribute: + attributes = options.attribute.split(',') lists = [] tab = " " - + if not isinstance(rdict, dict): - raise "%s not a dict" % rdict + raise "%s not a dict" % rdict for (key, value) in rdict.iteritems(): - if isinstance(value, StringTypes): + if isinstance(value, StringType): if (attributes and key in attributes) or not attributes: print tab * counter + "%s: %s" % (key, value) elif isinstance(value, list): @@ -42,29 +45,30 @@ def print_dict(rdict, options, counter=1): if isinstance(listitem, dict): lists.append((key, listitem)) elif isinstance(value, dict): - lists.append((key, value)) - - if counter == 1 or print_children: + lists.append((key, value)) + + if counter == 1 or print_children: for (key, listitem) in lists: if isinstance(listitem, dict): print tab * (counter - 1) + key - print_dict(listitem, options, counter+1) + 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)) - + if keys: + print tab * (counter) + "(children: %s)" % (",".join(keys)) + # # this code probably is obsolete # RSpec is not imported, it does not have a toDict() method anyway # plus, getNodes.py is not exposed in packaging -# +# def main(): - parser = create_parser(); + parser = create_parser() (options, args) = parser.parse_args() if not options.infile: print "RSpec file not specified" - return + return rspec = RSpec() try: @@ -77,14 +81,15 @@ def main(): rspec_dicts = rspec.getDictsByTagName(tag_name) rspec_dict = {tag_name: rspec_dicts} else: - rspec_dict = rspec.toDict() - + rspec_dict = rspec.toDict() + print_dict(rspec_dict, options) return if __name__ == '__main__': - try: main() + try: + main() except Exception as e: raise print e