ANN : ConfigObj 3.0.0 - Simple config file parsing

Discussion in 'Python' started by Fuzzyman, May 26, 2004.

  1. Fuzzyman

    Fuzzyman Guest

    There have been a couple of config file 'systems' announced recently,
    that focus on building more powerful and complex configuration files.
    ConfigObj is a module to enable you to much more *simply* access
    config files.

    This is version 3, which is a big overhaul. It extends ConfigObj to
    reading config files with sections and various other simplifications.
    I find ConfigObj extremely easy to use and use it for reading config
    files and data persistence......

    http://www.voidspace.org.uk/atlantibots/pythonutils.html#configobj

    (also the home of caseless and listparse modules)

    INTRODUCTION and EXTRACT from docs

    ConfigObj

    ConfigObj allows you to read, modify and create config files in python
    with basically single line commands.
    If you give it a filename it will read the config automatically and
    you can access or change the values by treating it like a dictionary.
    ConfigObj 3 is a major upgrade to ConfigObj - it will now read and
    write config files with sections (like windows INI files) as well as
    various other improvements and simplifications.


    ConfigObj has the following advantages over other configuration
    systems :
    Ease of use
    Multiple (list) values for keywords
    Easy to create, modify *and* write files
    Easy to hand edit/create the config files
    comments are preserved
    quoting is optional
    will understand various different keyword/value dividers

    Because the programmers interface is the same as the Python
    dictionary, ConfigObj is also extremely useful for data persistence.
    The fact that it can store lists and all the files it creates are
    easily 'human readable' is a big plus for this. Most functionality can
    be achieved with dictionary like syntax.

    So it implements a system that is easy for your users to use and easy
    for you to examine the files that it creates.
    Feedback on this module, including the documentation, is very much
    welcomed. Questions, criticism, comments, bug reports and suggestions
    all welcomed.

    ConfigObj needs python 2.2+

    ######################################################################

    THE BASICS

    USAGE :
    from configobj import ConfigObj, pref_dict, exceptionlist
    config = ConfigObj(infile=[], indict = {}, **keywargs)

    You create a basic configobj by giving it a filename and any options
    you want to set. The options can either be in the form of keyword
    arguments or as a dictionary. A useful default dictionary is available
    to modify - but we'll look more at the options later.

    config = ConfigObj(filename)

    ConfigObj then reads the file and parses the values from it.
    Values are always read in as strings - or lists of strings.
    The file can either be a straightforward config file with just
    keywords and values, or it can be divided into sections. Each section
    then has it's own set of keywords. See the paragraph on config files
    to see the difference.

    You can then access the keywords in the same way as you access a
    dictionary :
    value1 = config['keyword1']
    value2 = config['keyword2']
    ..
    ..

    You can even use the same method to change the values, and write it
    out using the write method :
    config['keyword1'] = value1
    config['keyword2'] = value2
    ..
    ..
    config.write()

    ConfigObj inherits most of it's methods from the built in type
    dictionary - so most of the things you can do with dictionaries you
    can do with a configobj.

    Keywords in ConfigObj are case insensitive. This is done using a class
    called caseless. If you ever need a case insensitive dictionary or
    list you can use these !

    This means that :
    print config['FISH']
    is the same as :
    print config['fish']

    It also means that the case of the keywords in your config files
    doesn't matter. ConfigObj will try and preserve the case you used when
    it writes files out though.

    If the config file has sections in it, then each section will be a
    dictionary of keywords and values.
    print config['section1']
    {'keyword1': 'value 1', 'keyword2': 'value 2', 'keyword3': 'value 3'}

    You can create an empty new section with :
    config['section 2'] = None
    or you can just pass in a dictionary :
    config['section 2'] = {'keyword1': 'value 1', 'keyword2': 'value 2',
    'keyword3': 'value 3'}

    You access values in a section with :
    value1 = config['section 1']['keyword1']
    value2 = config['section 1']['keyword2']

    You can also use ConfigObj to just read in *some* values from a file,
    and then just update those values in the file.
    This is done using a 'configspec' when you read a file and the
    'writein' method to write it out again. But we'll see more about those
    later.

    You can even create a completely empty configobj from scratch :
    config = ConfigObj()
    config.filename = filename
    config['keyword1'] = value
    config['keyword2'] = value
    ..
    ..
    configobj.write()

    It can then be read back in with :
    config = ConfigObj(filename)


    Easy hey !!
    A blank configobj created with
    config = ConfigObj()
    will be a flatfile by default. The same applies if you specify a
    filename which doesn't yet exist. To make it a configobj with sections
    you need to specify :
    config = ConfigObj(flatfile=False)

    The last thing I'll mention when covering the basics is list values in
    config files.
    Values are always strings - if you want integers, or anything else,
    you can do the conversion yourself !
    If a line in a config file is a list -
    'keyword' = [value1, value2, value3]
    Then it will be read in and turned into a list (using the listparse
    module).
    If you pass in a list to a configobj then it will be written out as a
    list. This includes nested lists !! (lists of lists !! - unless you
    turn recursivelist off - see the options section).

    This means :
    config['keyword'] = [ value1, [value2, value3], value4]
    is perfectly valid.

    Regards,


    Fuzzy

    http://www.voidspace.org.uk
     
    Fuzzyman, May 26, 2004
    #1
    1. Advertising

  2. Fuzzyman

    Wilk Guest

    (Fuzzyman) writes:

    > There have been a couple of config file 'systems' announced recently,
    > that focus on building more powerful and complex configuration files.
    > ConfigObj is a module to enable you to much more *simply* access
    > config files.


    What do you thing about yaml for config file ? it's very pythonic with
    indentation :

    import yaml
    print yaml.load("""
    rub1:
    - one
    - two
    rub2:
    - three
    - four
    """).next()

    {'rub1': ['one', 'two'], 'rub2': ['three', 'four']}

    it can also work on the other side :

    print yaml.dump({'rub1': ['one', 'two'], 'rub2': ['three', 'four']})
    ---
    rub1:
    - one
    - two
    rub2:
    - three
    - four

    --
    Wilk - http://flibuste.net
     
    Wilk, May 26, 2004
    #2
    1. Advertising

  3. Fuzzyman

    Fuzzyman Guest

    Wilk <> wrote in message news:<>...
    > (Fuzzyman) writes:
    >
    > > There have been a couple of config file 'systems' announced recently,
    > > that focus on building more powerful and complex configuration files.
    > > ConfigObj is a module to enable you to much more *simply* access
    > > config files.

    >
    > What do you thing about yaml for config file ? it's very pythonic with
    > indentation :
    >
    > import yaml
    > print yaml.load("""
    > rub1:
    > - one
    > - two
    > rub2:
    > - three
    > - four
    > """).next()
    >
    > {'rub1': ['one', 'two'], 'rub2': ['three', 'four']}
    >
    > it can also work on the other side :
    >
    > print yaml.dump({'rub1': ['one', 'two'], 'rub2': ['three', 'four']})
    > ---
    > rub1:
    > - one
    > - two
    > rub2:
    > - three
    > - four


    I'm sure YAML is fine - I think ConfigObj is still probably 'simpler'.
    A basic config file can be created with :
    config = ConfigObj(filename)
    config['keyword1] = value1
    config['list keyword] = [value2, value3, value4]
    ..
    ..

    config.write()

    You can then read it back with :
    config = ConfigObj(filename)

    Lot's of extra options of course. The created config files look like
    config files that most people will be used to creating :

    "keyword1" = "value1"
    "keyword2" = "value2"
    ..
    ..
    ..

    Regards,


    Fuzzy

    http://www.voidspace.org.uk/atlantibots/pythonutils.html
     
    Fuzzyman, May 27, 2004
    #3
  4. Fuzzyman

    David Fraser Guest

    Fuzzyman wrote:
    > There have been a couple of config file 'systems' announced recently,
    > that focus on building more powerful and complex configuration files.
    > ConfigObj is a module to enable you to much more *simply* access
    > config files.
    >
    > This is version 3, which is a big overhaul. It extends ConfigObj to
    > reading config files with sections and various other simplifications.
    > I find ConfigObj extremely easy to use and use it for reading config
    > files and data persistence......
    >
    > http://www.voidspace.org.uk/atlantibots/pythonutils.html#configobj
    >
    > (also the home of caseless and listparse modules)
    >
    > INTRODUCTION and EXTRACT from docs
    >
    > ConfigObj
    >
    > ConfigObj allows you to read, modify and create config files in python
    > with basically single line commands.
    > If you give it a filename it will read the config automatically and
    > you can access or change the values by treating it like a dictionary.
    > ConfigObj 3 is a major upgrade to ConfigObj - it will now read and
    > write config files with sections (like windows INI files) as well as
    > various other improvements and simplifications.
    >
    >
    > ConfigObj has the following advantages over other configuration
    > systems :
    > Ease of use
    > Multiple (list) values for keywords
    > Easy to create, modify *and* write files
    > Easy to hand edit/create the config files
    > comments are preserved
    > quoting is optional
    > will understand various different keyword/value dividers
    >
    > Because the programmers interface is the same as the Python
    > dictionary, ConfigObj is also extremely useful for data persistence.
    > The fact that it can store lists and all the files it creates are
    > easily 'human readable' is a big plus for this. Most functionality can
    > be achieved with dictionary like syntax.
    >
    > So it implements a system that is easy for your users to use and easy
    > for you to examine the files that it creates.
    > Feedback on this module, including the documentation, is very much
    > welcomed. Questions, criticism, comments, bug reports and suggestions
    > all welcomed.
    >


    Just as an aside, we've just written a config system for our app that I
    think is quite interesting and am planning to release when I get time.

    Some of the goals are the same as ConfigObject: easy for either a human
    or a computer to read / edit the files.
    Some are different. Particularly it handles hierarchical config tries in
    a nice pythonic syntax. For example you can specify things like this:

    app1.title = "Title of app"
    app1.font.face = "Arial"

    Or like this:

    app1:
    title = "Title of app"
    font.face = "Arial"

    You can also copy bits of the tree if you like:

    app1:
    title = "Title of app"
    subtitle = "Red"
    font:
    face = "Arial"
    size = 16
    style = "normal"

    app2:
    title = "Other title"
    subtitle = app1.subtitle
    font = app1.font:
    size = 18

    The module will automatically remember where an element is defined and
    save changed values to the same location. It will also save new values
    to an appropriate location (e.g. app2.newvalue would appear in the app2
    section).

    I think the hierarchical aspect of configuration is important ; it lets
    you do things like Mozilla's generic prefs interface etc. It also gives
    plugins / customizations to an app an easy way to add their own preferences.

    If anyone is interested in this module, post a reply...

    David
     
    David Fraser, May 28, 2004
    #4
  5. David Fraser <> writes:

    > Just as an aside, we've just written a config system for our app that
    > I think is quite interesting and am planning to release when I get
    > time.
    >
    > Some of the goals are the same as ConfigObject: easy for either a
    > human or a computer to read / edit the files.
    > Some are different. Particularly it handles hierarchical config tries
    > in a nice pythonic syntax. For example you can specify things like
    > this:
    >
    > app1.title = "Title of app"
    > app1.font.face = "Arial"
    >
    > Or like this:
    >
    > app1:
    > title = "Title of app"
    > font.face = "Arial"

    [...]

    > The module will automatically remember where an element is defined and
    > save changed values to the same location. It will also save new values
    > to an appropriate location (e.g. app2.newvalue would appear in the
    > app2 section).
    >
    > I think the hierarchical aspect of configuration is important ; it
    > lets you do things like Mozilla's generic prefs interface etc. It also
    > gives plugins / customizations to an app an easy way to add their own
    > preferences.
    >
    > If anyone is interested in this module, post a reply...


    Looks interesting, if you ask me.

    Thomas
     
    Thomas Heller, May 28, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    322
  2. Fuzzyman
    Replies:
    0
    Views:
    339
    Fuzzyman
    Mar 9, 2005
  3. Fuzzyman

    ANN: ConfigObj 4.0.0 Beta 4

    Fuzzyman, Sep 8, 2005, in forum: Python
    Replies:
    0
    Views:
    278
    Fuzzyman
    Sep 8, 2005
  4. Fuzzyman
    Replies:
    0
    Views:
    287
    Fuzzyman
    Oct 18, 2005
  5. Fuzzyman
    Replies:
    0
    Views:
    304
    Fuzzyman
    Jan 31, 2006
Loading...

Share This Page