ConfigParser and multiple option names

  • Thread starter Florian Lindner
  • Start date
F

Florian Lindner

Hello,
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"

(only the last one is read in)

I wonder what the best way to work around this.

I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"

What character would be best to work on various operating systems? (of what
names may a path consist is the question)

What do you think? Any better ideas?

Thanks,

Florian
 
A

Alexis Roda

Florian Lindner escribió:
I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"

RCS uses , in filenames
What do you think? Any better ideas?

A bit ugly, but probably safer and simpler than adding arbitrary separators:

[section]
dir_1=/home/florian
dir_2=/home/john
dir_3=/home/whoever


a s(a|i)mple implementation to give you the idea, it has some bugs:

import ConfigParser
import re

class ConfigParserWithLists(ConfigParser.ConfigParser):
def setlist(self, section, option, value) :
for i, v in enumerate(value) :
self.set(section, '%s_%i' % (option, i + 1), v)

def getlist(self, section, option) :
res = []
m = re.compile('^' + option + '_\d+$').match
for oo in self.options(section) :
if m(oo) :
res.append(self.get(section, oo))
return res




HTH
 
F

Florian Lindner

Alexis said:
Florian Lindner escribió:

RCS uses , in filenames

A kommata (,) is a valid character in path names. Ok, you can use quotes.
What do you think? Any better ideas?

A bit ugly, but probably safer and simpler than adding arbitrary
separators:

[section]
dir_1=/home/florian
dir_2=/home/john
dir_3=/home/whoever

I tend to use seperators, because I think it's more common to users. (the
PATH environment variable e.g.)
a s(a|i)mple implementation to give you the idea, it has some bugs:
[...]

Thanks for your input!

Florian
 
L

Larry Bates

Florian said:
Hello,
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"

(only the last one is read in)

I wonder what the best way to work around this.

I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"

What character would be best to work on various operating systems? (of what
names may a path consist is the question)

What do you think? Any better ideas?

Thanks,

Florian

I would either use (not tested):

[directories]
dir_01="/home/florian"
dir_02="/home/john"
dir_03="/home/whoever"

and in my program do:

INI=ConfigParser.ConfigParser()
INI.read(inifilepath)

section="directories"
dirs=[x for x in INI.options(section) if x.startswith('dir_')]

or more easily:

[init]
dirs=/home/florian,/home/john,home/whoever

and in program do:

section="init"
dirs=INI.get(section, 'dirs',).split(',')

Which one to use depends on how many dirs you would be supporting.
If it is a lot, I like the first version. If a few, the second
seems better.

-Larry Bates
 
E

Edward Elliott

Florian said:
I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"

What character would be best to work on various operating systems? (of
what names may a path consist is the question)

I don't think there are any universally-disallowed chars for path names.
But I also haven't seen anyone mention os.pathsep yet. Seems like the best
choice for a separator char. If you need to run on multiple platforms,
your choices seem to be:

1. pick a legal-on-some-systems-but-uncommon-on-all char for dir names as a
separator (iirc osx uses : in paths so ; might have to do)
2. use os.pathsep to write platform-specific config files
3. do it another way
 
A

alisonken1

Florian said:
Hello,
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"
<snip>

Another option would be to switch to the XMLParser library and use an
XML file for the configuration.

That way, you can build using the same name:

#==== config_file.xml ====
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE options>
<options>
<directories>
<path>"/home/florian"</path>
<path>"/home/john"</path>
<path>"/home/whoever"</path>
</directories>
{other options}
</options>
#==== config_file.xml ====

There are also several tutorials
<a
href="http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/pyxml-akara">
Uche Ogbuji's Akara site
</a>

has some excellent XML tutorials which include python XML processing.
 
B

Benji York

Florian said:
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"

I generally do this:

dirs =
/home/florian
/home/john
/home/whoever

....and then use str.split() in my program.
 
A

alisonken1

Benji York wrote:
I generally do this:

dirs =
/home/florian
/home/john
/home/whoever

...and then use str.split() in my program.

The only problem with this would be if you plan on updating the config
file later in the program - I don't think ConfigParser would write the
new config file with these options setup this way.
 
C

Caleb Hattingh

I have had this same problem before, and what I ended up doing was
writing my own far more limited config parser that would create lists
for repeated named assignments.

Who is the maintainer of ConfigParser? Perhaps a keyword option can
be added so that this kind of behaviour can be added at creation.

Caleb
 
J

jorge.vargas

that will break horribly in windows, remenber it install all it's crap
in c:\Program Files
 
I

Ivan Vinogradov

Another option is to use a dedicated section and simply omit values
for options:

[dirs]
/path/1:
/long/path/2:
/etc:

Then get options for section dirs.
This approach precludes using ':' or '=' in paths though.
 
F

Florian Lindner

that will break horribly in windows, remenber it install all it's crap
in c:\Program Files

Why should this break? If you split at the \n character?

Florian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,213
Latest member
DonnellTol

Latest Threads

Top