X-Git-Url: http://git.onelab.eu/?p=sfa.git;a=blobdiff_plain;f=clientbin%2FgetNodes.py;h=69abf484d5f410a955d74063a7754b1f5e5bc23d;hp=71d17f02c2e209fc7fc6cd7d99a3b31c085ad80b;hb=HEAD;hpb=a0b08c3177b6273ad22f3882cd62495743ed404c diff --git a/clientbin/getNodes.py b/clientbin/getNodes.py index 71d17f02..69abf484 100644 --- a/clientbin/getNodes.py +++ b/clientbin/getNodes.py @@ -1,82 +1,92 @@ -#!/usr/bin/python +#!/usr/bin/env python3 import sys import os from optparse import OptionParser from pprint import pprint -from types import StringTypes def create_parser(): command = sys.argv[0] argv = sys.argv[1:] usage = "%(command)s [options]" % locals() 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("-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") + parser = OptionParser(usage=usage, description=description) + parser.add_option("-i", "--infile", dest="infile", default=None, + help="input rspec file") + 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 + 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, StringTypes): + 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() - except Exception, e: + try: + main() + except Exception as e: raise - print e + print(e)