[771] | 1 | #!/usr/bin/env python |
---|
[726] | 2 | |
---|
[733] | 3 | """ |
---|
[778] | 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 |
---|
[733] | 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 |
---|
[778] | 11 | the YAML configuration; e.g., they cannot contain dots.) |
---|
[733] | 12 | |
---|
| 13 | Examples: |
---|
| 14 | |
---|
| 15 | invirt-getconf db.uri |
---|
| 16 | invirt-getconf authn.0.type |
---|
| 17 | """ |
---|
| 18 | |
---|
[816] | 19 | from invirt.config import default_src_path, default_cache_path, load |
---|
[789] | 20 | from sys import argv, exit, stderr, stdout |
---|
[733] | 21 | from optparse import OptionParser |
---|
[726] | 22 | |
---|
[771] | 23 | class invirt_exception(Exception): pass |
---|
[733] | 24 | |
---|
[771] | 25 | def 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', |
---|
[816] | 30 | default = default_src_path, |
---|
[771] | 31 | help = 'the source YAML configuration file to read from') |
---|
| 32 | parser.add_option('-c', '--cache', |
---|
[816] | 33 | default = default_cache_path, |
---|
[771] | 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') |
---|
[787] | 38 | parser.add_option('-l', '--ls', |
---|
| 39 | action = 'store_true', |
---|
| 40 | help = 'list node\'s children') |
---|
[771] | 41 | opts, args = parser.parse_args() |
---|
[726] | 42 | |
---|
[788] | 43 | if len(args) > 1: |
---|
| 44 | raise invirt_exception(__doc__.strip()) |
---|
| 45 | elif args and args[0]: |
---|
| 46 | components = args[0].split('.') |
---|
| 47 | else: |
---|
| 48 | components = [] |
---|
[726] | 49 | |
---|
[771] | 50 | conf = load(opts.src, opts.cache, opts.refresh) |
---|
| 51 | for i, component in enumerate(components): |
---|
| 52 | progress = '.'.join(components[:i]) |
---|
[789] | 53 | if type(conf) not in (dict, list): |
---|
[733] | 54 | raise invirt_exception( |
---|
[771] | 55 | '%s: node has no children (atomic datum)' % progress) |
---|
| 56 | if type(conf) == list: |
---|
| 57 | try: component = int(component) |
---|
[733] | 58 | except: raise invirt_exception( |
---|
[771] | 59 | '%s: node a list; integer path component required, ' |
---|
| 60 | 'but got "%s"' % (progress, component)) |
---|
| 61 | try: conf = conf[component] |
---|
[733] | 62 | except KeyError: raise invirt_exception( |
---|
[771] | 63 | '%s: key "%s" not found' % (progress, component)) |
---|
| 64 | except IndexError: raise invirt_exception( |
---|
| 65 | '%s: index %s out of range' % (progress, component)) |
---|
[788] | 66 | |
---|
[787] | 67 | if opts.ls: |
---|
[789] | 68 | if type(conf) not in (dict, list): |
---|
[787] | 69 | raise invirt_exception( |
---|
[802] | 70 | '%s: node has no children (atomic datum)' |
---|
| 71 | % '.'.join(components)) |
---|
[787] | 72 | if type(conf) == list: |
---|
| 73 | for i in xrange(len(conf)): |
---|
| 74 | print i |
---|
| 75 | else: |
---|
| 76 | for k in conf.iterkeys(): |
---|
| 77 | print k |
---|
| 78 | else: |
---|
[789] | 79 | if type(conf) not in (dict, list): |
---|
| 80 | print conf |
---|
| 81 | else: |
---|
| 82 | import yaml |
---|
[816] | 83 | try: dumper = yaml.CSafeDumper |
---|
| 84 | except: dumper = yaml.SafeDumper |
---|
[796] | 85 | yaml.dump(conf, stdout, |
---|
[816] | 86 | Dumper = dumper, default_flow_style = False) |
---|
[778] | 87 | except invirt_exception, ex: |
---|
[733] | 88 | print >> stderr, ex |
---|
| 89 | return 1 |
---|
| 90 | |
---|
[771] | 91 | if __name__ == '__main__': |
---|
| 92 | exit(main(argv)) |
---|
[733] | 93 | |
---|
[726] | 94 | # vim:et:sw=4:ts=4 |
---|