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