Example #1
0
    def print(self, format='python', indent=4, **kwargs):
        """ print
        This method prints out our nested Config.  It primarily leans on our
        defined ConfigType classes to do so.

        format: the format/ConfigType to use to display the data
        indent: Since most/all config parsers understand indent, it was
            left here for readability and a default value.
        kwargs: Used to pass through other arguments to the ConfigType
            pretty_print methods for use with config parser pretty print
            options.
        """
        data = self.as_dict()

        if format == 'python':
            pp = pprint.PrettyPrinter(indent=indent, **kwargs)
            pp.pprint(data)
        else:
            type_obj = ConfigType.load_module(format)
            type_obj.pretty_print(data, indent=indent, **kwargs)
Example #2
0
def load_configuration(locations):
    """ load_configuration
    Takes an array of tuples in the form of (type, location, path)

    type: a defined ConfigType class, like 'json', 'sqlalchemy' or 'yaml'
    location: This is a pointer to where to find the data.  Each ConfigType can
       specify available locations, such as files, raw strings, database
       connection information, etc.

       You can specify a dictionary for precision, but the parser will try to
       guess your meaning if you simply pass a string, by trying all available
       options until one works.

       For example, with json type, location could be any of:
           {'file': '/etc/myconfig.json'}
           '/etc/myconfig.json'}
           {'string': '{"some_data": 15, "another_thing": "some_string"}'}
           '{"some_data": 15, "another_thing": "some_string"}'

    path: optional config path-prefix

    Returns: The root Config() object that is the base of the configuration,
    which can be traverssed with attribute notation.

    Example: given 'json' type that looks like:
        {
            "a": {
                "b": {
                    "c": {
                        "d": 15
                    }
                }
            }
        }

    passing ('json', '/path/to/above/config.json') will return an object where
    a.b.c.d = 15

    config = load_configuration([
            ('json', {'file': '/path/to/above/config.json'})
    ])

    config.a.b.c.d == 15

    if the last tuple elementi representing 'path' is 'h.i.j',
    the value is accessible at h.i.j.a.b.c.d
    """
    config = Config()

    for loc in locations:
        try:
            _type = loc[0]
            _location = loc[1]
        except IndexError:
            raise SystemExit('One of "config type" or "location" not '
                             'specified')

        try:
            _path = loc[2]
        except IndexError:
            _path = None

        type_class = ConfigType.load_module(_type)
        type_config = type_class.load(_location)

        config.update_config(type_config, _path)

    return config