source: trunk/packages/sipb-xen-base/files/usr/sbin/invirt-getconf @ 778

Last change on this file since 778 was 778, checked in by y_z, 16 years ago
  • moved more generic code into common package
  • silently fail if cache fails
  • load the configuration on module load
  • produce a struct-based representation of the configuration
  • allowing full exception messages for OSErrors (default behavior)
  • added some dependencies specs
  • Property svn:executable set to *
File size: 2.4 KB
RevLine 
[771]1#!/usr/bin/env python
[726]2
[733]3"""
[778]4invirt-getconf loads an invirt configuration file (either the original YAML
5source or the faster-to-load JSON cache) and prints the configuration option
6with the given name (key).  Keys are dot-separated paths into the YAML
[733]7configuration tree.  List indexes (0-based) are also treated as path
8components.
9
10(Due to this path language, certain restrictions are placed on the keys used in
[778]11the YAML configuration; e.g., they cannot contain dots.)
[733]12
13Examples:
14
15  invirt-getconf db.uri
16  invirt-getconf authn.0.type
17"""
18
[726]19from invirt.config import load
[733]20from sys import argv, exit, stderr
21from optparse import OptionParser
[726]22
[771]23class invirt_exception(Exception): pass
[733]24
[771]25def main(argv):
[733]26    try:
[771]27        parser = OptionParser(usage = '%prog [options] key',
28                description = __doc__.strip().split('\n\n')[0])
29        parser.add_option('-s', '--src',
30                default = '/etc/invirt/master.yaml',
31                help = 'the source YAML configuration file to read from')
32        parser.add_option('-c', '--cache',
33                default = '/var/lib/invirt/invirt.json',
34                help = 'path to the JSON cache')
35        parser.add_option('-r', '--refresh',
36                action = 'store_true',
37                help = 'force the cache to be regenerated')
38        opts, args = parser.parse_args()
[726]39
[733]40        try: [key] = args
[771]41        except: raise invirt_exception(__doc__.strip())
[726]42
[771]43        conf = load(opts.src, opts.cache, opts.refresh)
[733]44        components = key.split('.')
[771]45        for i, component in enumerate(components):
46            progress = '.'.join(components[:i])
47            if type(conf) not in [dict, list]:
[733]48                raise invirt_exception(
[771]49                        '%s: node has no children (atomic datum)' % progress)
50            if type(conf) == list:
51                try: component = int(component)
[733]52                except: raise invirt_exception(
[771]53                        '%s: node a list; integer path component required, '
54                        'but got "%s"' % (progress, component))
55            try: conf = conf[component]
[733]56            except KeyError: raise invirt_exception(
[771]57                    '%s: key "%s" not found' % (progress, component))
58            except IndexError: raise invirt_exception(
59                    '%s: index %s out of range' % (progress, component))
[733]60        print conf
[778]61    except invirt_exception, ex:
[733]62        print >> stderr, ex
63        return 1
64
[771]65if __name__ == '__main__':
66    exit(main(argv))
[733]67
[726]68# vim:et:sw=4:ts=4
Note: See TracBrowser for help on using the repository browser.