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