Ignore:
Timestamp:
Jul 27, 2008, 8:11:04 PM (16 years ago)
Author:
y_z
Message:

simple initial version of invirt-getconf simply reads & navigates yaml; fixed permissions

Location:
trunk/packages/sipb-xen-base/files/usr
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/sipb-xen-base/files/usr/sbin/invirt-getconf

    • Property svn:executable set to *
    r729 r733  
    11#!/usr/bin/env python2.5
    22
     3"""
     4invirt-getconf [-f FILE] KEY prints the configuration the option named KEY from
     5the invirt configuration file FILE.  Keys are dot-separated paths into the YAML
     6configuration tree.  List indexes (0-based) are also treated as path
     7components.
     8
     9(Due to this path language, certain restrictions are placed on the keys used in
     10the YAML configuration, e.g. they cannot contain dots.)
     11
     12Examples:
     13
     14  invirt-getconf db.uri
     15  invirt-getconf authn.0.type
     16"""
     17
    318from invirt.config import load
    4 from sys import argv, stderr
     19from sys import argv, exit, stderr
     20from optparse import OptionParser
     21
     22class invirt_exception( Exception ): pass
    523
    624def main( argv ):
    7     try: command, key = argv
    8     except: print >> stderr, 'invirt-getconf KEY'
    9     conf = load()
    10     for component in key.split('.')[:-1]:
    11         if component.isdigit(): component = int( component )
    12         conf = conf[ component ]
    13     print conf[key]
     25    try:
     26        parser = OptionParser()
     27        parser.add_option('-f', '--file', default = '/etc/invirt/master.yaml',
     28                help = 'the configuration file to read from')
     29        options, args = parser.parse_args()
    1430
    15 if __name__ == '__main__':
    16     main( argv )
     31        try: [key] = args
     32        except: raise invirt_exception( __doc__ )
     33
     34        conf = load()
     35        components = key.split('.')
     36        for i, component in enumerate( components ):
     37            progress = lambda: '.'.join( components[:i] )
     38            if type( conf ) not in [ dict, list ]:
     39                raise invirt_exception(
     40                        'prematurely arrived at an atomic datum in the tree:\n'
     41                        '%s has no children' % progress() )
     42            if type( conf ) == list:
     43                try: component = int( component )
     44                except: raise invirt_exception(
     45                        '%s is a list, requires an integral path component '
     46                        'but got "%s"' % ( progress(), component ) )
     47            try: conf = conf[ component ]
     48            except KeyError: raise invirt_exception(
     49                    '"%s" not in "%s"' % ( component, progress() ) )
     50        print conf
     51    except invirt_exception, ex:
     52        print >> stderr, ex
     53        return 1
     54
     55if __name__ == '__main__': exit( main( argv ) )
    1756
    1857# vim:et:sw=4:ts=4
  • trunk/packages/sipb-xen-base/files/usr/share/python-support/sipb-xen-base/invirt/config.py

    r726 r733  
    44default_path = '/etc/invirt/master.yaml'
    55
     6try:    default_loader = yaml.CSafeLoader
     7except: default_loader = yaml.SafeLoader
     8
    69def load( path = default_path ):
    710    with file( path ) as f:
    8         return yaml.load( f, yaml.CSafeLoader )
     11        return yaml.load( f, default_loader )
    912
    1013# vim:et:sw=4:ts=4
Note: See TracChangeset for help on using the changeset viewer.