Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python config parser that supports section inheritance?

Tags:

python

config

ini

I'm looking for an ini style config parser in Python that supports section inheritance similar to what Zend_Config_Ini does in PHP.

Does such a module exist or will I need to roll my own?

like image 273
Maascamp Avatar asked Jun 29 '11 22:06

Maascamp


People also ask

Is Configparser part of Python?

ConfigParser is a Python class which implements a basic configuration language for Python programs. It provides a structure similar to Microsoft Windows INI files.

What is .ini file in Python?

An INI file is a configuration file for computer software that consists of a text-based content with a structure and syntax comprising key–value pairs for properties, and sections that organize the properties.

How parse config file in Python?

Read and parse one configuration file, given as a file object. Read configuration from a given string. Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.

What is config () in Python?

A Python configuration file is a pure Python file that populates a configuration object. This configuration object is a Config instance.


2 Answers

Python's ConfigParser can load multiple files. Files read later on can override settings from the first file.

For example, my application has database settings in its internal default configuration file:

[database]
server = 127.0.0.1
port = 1234
...

I override these on a different server with a "environment.ini" file containing the same section but different values:

[database]
server = 192.168.0.12
port = 2345
...

In Python:

import os
from ConfigParser import ConfigParser
dbconf = ConfigParser()
dbconf.readfp(open('default.ini'))
if os.path.exists('environment.ini'):
    dbconf.readfp(open('environment.ini'))
dbconf.get('database', 'server') # Returns 192.168.0.12
like image 109
tuomur Avatar answered Oct 24 '22 01:10

tuomur


Here's what I used. extended_get method is what you need - it supports hierarchical sections.

import re
import io
import ConfigParser

class ZendConfigParser(ConfigParser.ConfigParser):
    def extended_get(self, section, key):
        if self.has_option(section, key):
            return self.get(section, key)
        else:
            orig_section, parent_section = self._get_orig_section(section)
            if orig_section != None:
                if self.has_option(orig_section,key):
                    return self.get(orig_section,key)
                else:
                    return self.extended_get(parent_section,key)
            else:
                return None



    def _get_orig_section(self, zend_section):
        orig_section = None
        parent_section = None
        for section in self.sections():
            if re.search(r'^[ \t]*' + zend_section + '\\b', section) != None:
                orig_section = section
                #look for a parent section
                match = re.match(r'\w+[ \t]*:[ \t]*(\w+)$', section)
                if match != None:
                    parent_section = match.groups()[0]
                break

        return (orig_section, parent_section)

config = ZendConfigParser()
config.read(file)
print(config.extended_get('production', 'database.params.host'))
like image 21
xvga Avatar answered Oct 23 '22 23:10

xvga