Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New lines with ConfigParser?

Tags:

python

I have a config file using configParser:

<br>
[ section one ]<br>
one = Y,Z,X <br><br>
[EG 2]<br>
ias = X,Y,Z<br>

My program works fine reading and processing these values.

However some of the sections are going to be quite large. I need a config file that will allow the values to be on a new line, like this:

[EG SECTION]<br>
EG=<br>
item 1 <br>
item 2 <br>
item 3<br>
etc...

In my code I have a simple function that takes a delimiter (or separator) of the values using string.split() obviously now set to comma. I have tried the escape string of \n which does not work.

Does anyone know if this is possible with python's config parser?
http://docs.python.org/library/configparser.html

# We need to extract data from the config 
def getFromConfig(currentTeam, section, value, delimeter):
    cp = ConfigParser.ConfigParser()
    fileName = getFileName(currentTeam)
    cp.read(fileName)
    try:
        returnedString = cp.get(section, value)
    except: # The config file could be corrupted
        print( "Error reading " + fileName + " configuration file." )
        sys.exit(1) #Stop us from crashing later
    if delimeter != "": # We may not need to split
        returnedList = returnedString.split(delimeter)
    return returnedList

I would use for this:

taskStrings = list(getFromConfig(teamName, "Y","Z",","))
like image 729
JP29 Avatar asked Jul 09 '12 16:07

JP29


People also ask

What is the use of config parser in configparser?

The configparser module has ConfigParser class. It is responsible for parsing a list of configuration files, and managing the parsed database. Return all the configuration section names.

How do I write to a file in configparser?

write (file_object) - This method takes as input file-like instance and writes configuration contents to that file. Our code for the first example starts by creating an instance of ConfigParser with default parameters. It then goes on to create 5 different sections.

How to implement mapping interface in configparser?

In case of configparser , the mapping interface implementation is using the parser ['section'] ['option'] notation. parser ['section'] in particular returns a proxy for the section’s data in the parser. This means that the values are not copied but they are taken from the original parser on demand.

How does configparser handle section proxies?

This means that the values are not copied but they are taken from the original parser on demand. What’s even more important is that when values are changed on a section proxy, they are actually mutated in the original parser. configparser objects behave as close to actual dictionaries as possible.


2 Answers

The ConfigParser _read() method's docstring says:

Continuations are represented by an embedded newline then leading whitespace.

Or alternatively (as the version in Python 3 puts it):

Values can span multiple lines, as long as they are indented deeper than the first line of the value.

This feature provides a means to split values up and "continue" them across multiple lines. For example, say you had a config file named 'test.ini' which contained:

[EG SECTION]<br>
EG=<br>
  item 1<br>
  item 2<br>
  item 3<br>

You could read the value of EG in the EG SECTION into a list with code like this:

try:
    import ConfigParser as configparser
except ImportError:  # Python 3
    import configparser

cp = configparser.ConfigParser()
cp.read('test.ini')

eg = cp.get('EG SECTION', 'EG')
print(repr(eg))  # -> '\nitem 1\nitem 2\nitem 3'

cleaned = [item for item in eg.strip().split('\n')]
print(cleaned)  # -> ['item 1', 'item 2', 'item 3']
like image 166
martineau Avatar answered Oct 10 '22 21:10

martineau


It seems possible. In my own config file, for example, I have a list object with tuples:

[root]
path: /
redirectlist: [ ( r'^magic', '/file' ),
    ( r'^avplay', '/file' ),
    ( r'^IPTV', '/file' ),
    ( r'^box', '/file' ),
    ( r'^QAM', '/qam' ),
    ( r'.*opentv.*', '/qam' ),
    ( r'.+', '/file' ) ]

and I do:

redirectstr = _configdict.get('root', 'redirectlist')
redirects = eval(redirectstr)

note that I am actually eval'ing that line, which may cause security breaches if used in the wild.

like image 2
Andre Blum Avatar answered Oct 10 '22 21:10

Andre Blum