10

A configuration file generally needs section headers for each section. In rsyncd config files a global section need not explicitly have a section header. Example of an rsyncd.conf file:

[rsyncd.conf]

# GLOBAL OPTIONS

path            = /data/ftp
pid file        = /var/run/rsyncdpid.pid
syslog facility = local3
uid             = rsync
gid             = rsync
read only       = true
use chroot      = true

# MODULE OPTIONS
[mod1]
...

How to parse such config files using python ConfigParser? Doing the following gives an erorr:

>>> import ConfigParser
>>> cp = ConfigParser.ConfigParser()
>>> cp.read("rsyncd.conf")

# Error: ConfigParser.MissingSectionHeaderError: File contains no section headers.
Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99

2 Answers2

18

I use itertools.chain (Python 3):

import configparser, itertools
cfg = configparser.ConfigParser()
filename = 'foo.ini'
with open(filename) as fp:
  cfg.read_file(itertools.chain(['[global]'], fp), source=filename)
print(cfg.items('global'))

(source=filename results in better error messages, especially if you read from multiple config files.)

johnLate
  • 464
  • 3
  • 8
2

Alex Martelli provided a solution for using ConfigParser to parse similar files (which are section less files). His solution is a file-like wrapper that will automatically insert a dummy section.

You can apply the above solution to parsing rsyncd config files.

>>> class FakeGlobalSectionHead(object):
...     def __init__(self, fp):
...         self.fp = fp
...         self.sechead = '[global]\n'
...     def readline(self):
...         if self.sechead:
...             try: return self.sechead
...             finally: self.sechead = None
...         else: return self.fp.readline()
...
>>> cp = ConfigParser()
>>> cp.readfp(FakeGlobalSectionHead(open('rsyncd.conf')))
>>> print(cp.items('global'))
[('path', '/data/ftp'), ('pid file', '/var/run/rsyncdpid.pid'), ...]
Community
  • 1
  • 1
seikichi
  • 1,251
  • 13
  • 15
  • With the deprecation of `configparser#readfp` this answer, whilst excellent, is likely to stop working in future versions of Python. The `itertools#chain` answer is probably a better solution for newer Python versions. – JeroenHoek May 14 '19 at 13:59
  • 1
    readfp was replace by read_file: https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.read_file – dosas Feb 08 '22 at 10:48