view configuration/config.py @ 5:7910b0ef0bab

stub configuration providers
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 24 Mar 2012 22:25:24 -0700
parents 92e1b2dd60c8
children dce954a3831f
line wrap: on
line source

#!/usr/bin/env python

"""
multi-level unified configuration
"""

import sys
import optparse

# imports for contigent configuration providers
try:
    import json
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        json = None
try:
    import yaml
except ImportError:
    yaml = None

configuration_providers = []
if json:
    class JSON(object):
        extensions = ['json']
    configuration_providers.append(JSON)

if yaml:
    class YAML(object):
        extensions = ['yml']
        def read(self, filename):
            pass
    configuration_providers.append(YAML)

__all__ = ['Configuration']

class Configuration(object):
    options = {}

    def __init__(self):
        self.config = {}

    def check(self):
        """check validity of configuration"""

    def __call__(self, *args):
        """add items to configuration and check it"""

    def add(self, config):
        """update configuration: not undoable"""
        self.config.update(config)
        # TODO: option to extend; augment lists/dicts

def main(args=sys.argv[:]):

    # parse command line options
    usage = '%prog [options]'
    class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
        """description formatter for console script entry point"""
        def format_description(self, description):
            if description:
                return description.strip() + '\n'
            else:
                return ''
    parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
    options, args = parser.parse_args(args)

if __name__ == '__main__':
  main()