#!/usr/bin/python import collections import io import platform pyver = platform.python_version_tuple()[:2] try: import ConfigParser except ImportError: import configparser as ConfigParser class FakeDictlist(dict): def __setitem__(self, key, value): try: self[key] except KeyError: super(FakeDictlist, self).__setitem__(key, value) else: newvalue = '{old}, {new}'.format(old=self[key], new=value) super(FakeDictlist, self).__setitem__(key, newvalue) testconfig = """ [mysqld] replicate-do-db="test1" replicate-do-db="test2" replicate-do-db="test3" """ for dtype in [FakeDictlist, dict, collections.OrderedDict]: print("Testing with %s" % dtype) if pyver == ('2', '7'): cp = ConfigParser.RawConfigParser(dict_type=dtype, allow_no_value=True) cp.readfp(io.BytesIO(testconfig)) else: cp = ConfigParser.RawConfigParser(dict_type=dtype, strict=False, allow_no_value=True) cp.read_string(testconfig) print(cp.sections()) print(cp.options('mysqld')) print(cp.get('mysqld', 'replicate-do-db')) # with open('test.cnf', 'w') as fh: # cp.write(fh) print("")