1

I have a config file which is mainly used in shell scripts, and therefore has the following format:

# Database parameters (MySQL only for now)
DBHOST=localhost
DATABASE=stuff
DBUSER=mypkguser
DBPASS=zbxhsxhg

# Storage locations
STUFFDIR=/var/mypkg/stuff
GIZMODIR=/var/mypkg/gizmo

Now I need to read its values from a Python (2.6) script. I would like not to reinvent the wheel and parse it with descriptor.readlines() and looking for equal signs and skipping lines beginning with '#' and dealing with quoted values and blah blah blah boring. I tried using ConfigParser but it doesn't like files that don't have section headers. Do I have any options or will I have to do the boring thing?

Oh, by the way, wrapping a shell script around the Python script is not an option. It has to run within Apache.

JCCyC
  • 16,140
  • 11
  • 48
  • 75
  • is this a duplicate for [this question](http://stackoverflow.com/questions/2885190/using-pythons-configparser-to-read-a-file-without-section-name)? – zenpoy Mar 13 '12 at 14:49
  • Akk. Yes. It is an exact duplicate. Sorry. – JCCyC Mar 13 '12 at 14:59

1 Answers1

3

I'm not aware of such a module, but as a quick and dirty hack - just add the [section] before the file-content and you can use ConfigParser as intended!

from io import StringIO

filename = 'ham.egg'
vfile = StringIO(u'[Pseudo-Sectio]\n%s'  % open(filename).read())
Don Question
  • 11,227
  • 5
  • 36
  • 54