Basic ConfigObj =============== Here we'll look at using ConfigObj to parse a user's configuration file and make some comparisons to python's ConfigParser.SafeConfigParser. .. ' .. module:: ConfigParser .. autosummary:: :toctree: api SafeConfigParser .. module:: configobj .. autosummary:: :toctree: api ConfigObj :: # python standard library from StringIO import StringIO import ConfigParser from ConfigParser import SafeConfigParser # third party from configobj import ConfigObj import configobj the ConfigObj object inherits from the configobj.Section class which itself extends python's `dict`. .. ' .. uml:: dict <|-- Section Section <|-- ConfigObj So most retrievals will look like you're using a dictionary of dictionaries. .. ' Like `ConfigParser` it focuses on the `ini` format which has the basic for of:: [section] option = value I'll be passing StringIO in to the constructor for ConfigObj, but in the real-world this would probably be a filename or opened file. .. ' Root Options ------------ Unlike python's ConfigParser, ConfigObj lets you put values in the configuration with no section header. .. ' :: sample = ["name = John Bigboote"] config = ConfigObj(sample) print config['name'] :: John Bigboote If we try this with SafeConfigParser: :: safeconfigparser = SafeConfigParser() sample = StringIO('name = John Bigboote') try: safeconfigparser.readfp(sample) except ConfigParser.MissingSectionHeaderError as error: print error :: File contains no section headers. file: , line: 1 'name = John Bigboote' Comma-Separated Lists --------------------- ConfigObj also supports comma-separated lists by default. :: sample = ["diseases = ebola, syphillis, cooties"] config = ConfigObj(sample) print config['diseases'] :: ['ebola', 'syphillis', 'cooties'] What if your value has a comma? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: sample = ["quote = What's this, then?"] config = ConfigObj(sample) print config['quote'] :: ["What's this", 'then?'] Our quote's comma caused config parser to split it in two. In this case you can set the `list_values` parameter to False .. ' :: config = ConfigObj(sample, list_values=False) print config['quote'] :: What's this, then? .. note:: This parameter has to be set in the constructor, changing the objects 'list_values' attribute won't work. .. ' :: config = ConfigObj(sample) config.list_values = False print config['quote'] :: ["What's this", 'then?'] This makes it difficult to have cases where you have lists and non-lists in the same configuration. The better way around this is to use quotation marks to identify strings. :: text = """ quote = "What's this, then?" strings = a, b, c strings_quote = a, b, c, "What's this, then?" """.splitlines() config = ConfigObj(text) for key, value in config.iteritems(): print "{0}: {1}".format(key, value) :: quote: What's this, then? strings: ['a', 'b', 'c'] strings_quote: ['a', 'b', 'c', "What's this, then?"] Option-Value Separators ----------------------- The formatting of the values, options and comments are also slightly different. ConfigParser allows both `