6156427bda6916c0c35f8832a111e0be8e861b81
[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
20 class invirt_exception(Exception): pass
21
22 def main(argv):
23     try:
24         parser = OptionParser(usage = '%prog [options] key',
25                 description = __doc__.strip().split('\n\n')[0])
26         parser.add_option('-r', '--refresh',
27                 action = 'store_true',
28                 help = 'force the cache to be regenerated')
29         parser.add_option('-l', '--ls',
30                 action = 'store_true',
31                 help = 'list node\'s children')
32         opts, args = parser.parse_args()
33
34         if len(args) > 1:
35             raise invirt_exception(__doc__.strip())
36         elif args and args[0]:
37             components = args[0].split('.')
38 import argparse
39 import sys
40 import yaml
41
42 import invirt
43
44         else:
45             components = []
46
47         conf = config.load(opts.refresh)
48         for i, component in enumerate(components):
49             progress = '.'.join(components[:i])
50             if type(conf) not in (dict, list):
51                 raise invirt_exception(
52                         '%s: node has no children (atomic datum)' % progress)
53             if type(conf) == list:
54                 try: component = int(component)
55                 except: raise invirt_exception(
56                         '%s: node a list; integer path component required, '
57                         'but got "%s"' % (progress, component))
58             try: conf = conf[component]
59             except KeyError: raise invirt_exception(
60                     '%s: key "%s" not found' % (progress, component))
61             except IndexError: raise invirt_exception(
62                     '%s: index %s out of range' % (progress, component))
63
64         if opts.ls:
65             if type(conf) not in (dict, list):
66                 raise invirt_exception(
67                         '%s: node has no children (atomic datum)'
68                         % '.'.join(components))
69             if type(conf) == list:
70                 for i in xrange(len(conf)):
71                     print i
72             else:
73                 for k in conf.iterkeys():
74                     print k
75         else:
76             if type(conf) not in (dict, list):
77                 print conf
78             else:
79                 import yaml
80                 yaml.dump(conf, stdout,
81                           Dumper=yaml.CSafeDumper, default_flow_style=False)
82     except invirt_exception, ex:
83         print >> stderr, ex
84         return 1
85
86 if __name__ == '__main__':
87     exit(main(argv))