a little nicer wrt pep8
[sfa.git] / clientbin / getNodes.py
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 from optparse import OptionParser
6 from pprint import pprint
7
8 def create_parser():
9     command = sys.argv[0]
10     argv = sys.argv[1:]
11     usage = "%(command)s [options]" % locals()
12     description = """getNodes will open a rspec file and print all key/values, or filter results based on a given key or set of keys."""
13     parser = OptionParser(usage=usage, description=description)
14     parser.add_option("-i", "--infile", dest="infile", default=None,
15                       help="input rspec file")
16     parser.add_option("-t", "--tag", dest="tag", default=None,
17                       help="filter rspec for this tag")
18     parser.add_option("-a", "--attribute", dest="attribute", default=None,
19                       help="comma separated list of attributes to display")
20     parser.add_option("-r", "--recursive", dest="print_children", default=False,  action="store_true",
21                       help="print the tag's child nodes")
22
23     return parser
24
25
26 def print_dict(rdict, options, counter=1):
27     print_children = options.print_children
28     attributes = []
29     if options.attribute:
30         attributes = options.attribute.split(',')
31     lists = []
32     tab = "    "
33
34     if not isinstance(rdict, dict):
35         raise "%s not a dict" % rdict
36     for (key, value) in rdict.items():
37         if isinstance(value, str):
38             if (attributes and key in attributes) or not attributes:
39                 print(tab * counter + "%s: %s" % (key, value))
40         elif isinstance(value, list):
41             for listitem in value:
42                 if isinstance(listitem, dict):
43                     lists.append((key, listitem))
44         elif isinstance(value, dict):
45             lists.append((key, value))
46
47     if counter == 1 or print_children:
48         for (key, listitem) in lists:
49             if isinstance(listitem, dict):
50                 print(tab * (counter - 1) + key)
51                 print_dict(listitem, options, counter + 1)
52     elif not attributes or (attributes and 'children' in attributes):
53         keys = set([key for (key, listitem) in lists])
54         if keys:
55             print(tab * (counter) + "(children: %s)" % (",".join(keys)))
56
57
58 #
59 # this code probably is obsolete
60 # RSpec is not imported, it does not have a toDict() method anyway
61 # plus, getNodes.py is not exposed in packaging
62 #
63 def main():
64     parser = create_parser()
65     (options, args) = parser.parse_args()
66     if not options.infile:
67         print("RSpec file not specified")
68         return
69
70     rspec = RSpec()
71     try:
72         rspec.parseFile(options.infile)
73     except:
74         print("Error reading rspec file")
75
76     if options.tag:
77         tag_name = options.tag
78         rspec_dicts = rspec.getDictsByTagName(tag_name)
79         rspec_dict = {tag_name: rspec_dicts}
80     else:
81         rspec_dict = rspec.toDict()
82
83     print_dict(rspec_dict, options)
84
85     return
86
87 if __name__ == '__main__':
88     try:
89         main()
90     except Exception as e:
91         raise
92         print(e)