Switch from optparse to argparse
[invirt/packages/invirt-base.git] / scripts / invirt-getconf
1 #!/usr/bin/env python3
2
3 """
4 invirt-getconf loads an invirt configuration file (either the original YAML
5 source or the faster-to-load JSON cache) and prints the configuration option
6 with the given name (key).  Keys are dot-separated paths into the YAML
7 configuration tree.  List indexes (0-based) are also treated as path
8 components.
9
10 (Due to this path language, certain restrictions are placed on the keys used in
11 the YAML configuration; e.g., they cannot contain dots.)
12
13 Examples:
14
15   invirt-getconf db.uri
16   invirt-getconf hosts.0.ip
17 """
18
19 import argparse
20 import sys
21 import yaml
22
23 import invirt
24
25
26 class InvirtException(Exception):
27     pass
28
29
30 def main():
31     parser = argparse.ArgumentParser(description='Get values from invirt configuration file')
32     parser.add_argument('-r', '--refresh', action='store_true', help='Force regenerate the cache')
33     parser.add_argument('-l', '--ls', action='store_true', help='List children of node')
34     parser.add_argument('path', nargs='?', default='', help='Path of value to get')
35
36     args = parser.parse_args()
37
38     components = args.path.split('.')
39
40         else:
41             components = []
42
43         conf = config.load(opts.refresh)
44         for i, component in enumerate(components):
45             progress = '.'.join(components[:i])
46             if type(conf) not in (dict, list):
47                 raise invirt_exception(
48                         '%s: node has no children (atomic datum)' % progress)
49             if type(conf) == list:
50                 try: component = int(component)
51                 except: raise invirt_exception(
52                         '%s: node a list; integer path component required, '
53                         'but got "%s"' % (progress, component))
54             try: conf = conf[component]
55             except KeyError: raise invirt_exception(
56                     '%s: key "%s" not found' % (progress, component))
57             except IndexError: raise invirt_exception(
58                     '%s: index %s out of range' % (progress, component))
59
60         if opts.ls:
61             if type(conf) not in (dict, list):
62                 raise invirt_exception(
63                         '%s: node has no children (atomic datum)'
64                         % '.'.join(components))
65             if type(conf) == list:
66                 for i in xrange(len(conf)):
67                     print i
68             else:
69                 for k in conf.iterkeys():
70                     print k
71         else:
72             if type(conf) not in (dict, list):
73                 print conf
74             else:
75                 import yaml
76                 yaml.dump(conf, stdout,
77                           Dumper=yaml.CSafeDumper, default_flow_style=False)
78     except invirt_exception, ex:
79         print >> stderr, ex
80         return 1
81
82 if __name__ == '__main__':
83     exit(main(argv))