[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 |
---|
[1836] | 16 | invirt-getconf hosts.0.ip |
---|
[733] | 17 | """ |
---|
| 18 | |
---|
[1420] | 19 | from invirt import config |
---|
[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('-r', '--refresh', |
---|
| 30 | action = 'store_true', |
---|
| 31 | help = 'force the cache to be regenerated') |
---|
[787] | 32 | parser.add_option('-l', '--ls', |
---|
| 33 | action = 'store_true', |
---|
| 34 | help = 'list node\'s children') |
---|
[771] | 35 | opts, args = parser.parse_args() |
---|
[726] | 36 | |
---|
[788] | 37 | if len(args) > 1: |
---|
| 38 | raise invirt_exception(__doc__.strip()) |
---|
| 39 | elif args and args[0]: |
---|
| 40 | components = args[0].split('.') |
---|
| 41 | else: |
---|
| 42 | components = [] |
---|
[726] | 43 | |
---|
[1420] | 44 | conf = config.load(opts.refresh) |
---|
[771] | 45 | for i, component in enumerate(components): |
---|
| 46 | progress = '.'.join(components[:i]) |
---|
[789] | 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)) |
---|
[788] | 60 | |
---|
[787] | 61 | if opts.ls: |
---|
[789] | 62 | if type(conf) not in (dict, list): |
---|
[787] | 63 | raise invirt_exception( |
---|
[802] | 64 | '%s: node has no children (atomic datum)' |
---|
| 65 | % '.'.join(components)) |
---|
[787] | 66 | if type(conf) == list: |
---|
| 67 | for i in xrange(len(conf)): |
---|
| 68 | print i |
---|
| 69 | else: |
---|
| 70 | for k in conf.iterkeys(): |
---|
| 71 | print k |
---|
| 72 | else: |
---|
[789] | 73 | if type(conf) not in (dict, list): |
---|
| 74 | print conf |
---|
| 75 | else: |
---|
| 76 | import yaml |
---|
[796] | 77 | yaml.dump(conf, stdout, |
---|
| 78 | Dumper=yaml.CSafeDumper, default_flow_style=False) |
---|
[778] | 79 | except invirt_exception, ex: |
---|
[733] | 80 | print >> stderr, ex |
---|
| 81 | return 1 |
---|
| 82 | |
---|
[771] | 83 | if __name__ == '__main__': |
---|
| 84 | exit(main(argv)) |
---|
[733] | 85 | |
---|
[726] | 86 | # vim:et:sw=4:ts=4 |
---|