X-Git-Url: http://git.onelab.eu/?p=sfa.git;a=blobdiff_plain;f=clientbin%2FgetNodes.py;h=69abf484d5f410a955d74063a7754b1f5e5bc23d;hp=ec2e4a2d90852f9a1fead98290ba9500fd73d914;hb=HEAD;hpb=30d9951e075d93127c3909dcb41be09b420b3525 diff --git a/clientbin/getNodes.py b/clientbin/getNodes.py index ec2e4a2d..69abf484 100644 --- a/clientbin/getNodes.py +++ b/clientbin/getNodes.py @@ -1,12 +1,10 @@ -#!/usr/bin/python +#!/usr/bin/env python3 import sys 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:] @@ -14,79 +12,81 @@ 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 - for (key, value) in rdict.iteritems(): - if isinstance(value, StringType): + raise "%s not a dict" % rdict + for (key, value) in rdict.items(): + if isinstance(value, str): if (attributes and key in attributes) or not attributes: - print tab * counter + "%s: %s" % (key, value) + print(tab * counter + "%s: %s" % (key, value)) elif isinstance(value, list): for listitem in value: 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(tab * (counter - 1) + key) + 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 + print("RSpec file not specified") + return rspec = RSpec() try: rspec.parseFile(options.infile) except: - print "Error reading rspec file" + print("Error reading rspec file") if options.tag: tag_name = options.tag 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 + print(e)