Pyfig: A simple config file parser for python

v2.0

Introduction

Pyfig is a very small and simple configuration file parser written in the python scripting language. It is written in just above 200 lines, in one file (pyfig.py), with support for categories and comments. Here is a sample configuration file that Pyfig will parse:

[general]
player_name = toaster_phun
age/int = 19

[connection]
type=dsl
speed=medium

[bank]
balance/int = 3000
profit/float = .02
	

To parse the config file:

import pyfig

# init the config object
config = Pyfig("path/to/config")

print config.general.player_name
'toaster_phun'

print config.bank.balance
3000

print config.connection.type
'dsl'
	

README

This is the README that comes with Pyfig. It includes usage instructions and examples.

Pyfig: A simple config file parser for python ============================================= Information: * Project by Alec Henriksen * Website: http://pyfig.alecwh.com/ * Version: 2.0 (09/27/2009) * Development: http://bitbucket.org/alecwh/pyfig/ Features -------- * Easy to use (straightforward, simple) * Supports categories/sections in the config file * Supports comments * Very robust in terms of parsing * One file, no package Installation ------------ Simply drop pyfig.py inside your working directory, or somewhere in your python search path. Pyfig doesn't have any dependencies. Example and Usage ----------------- **The `EXAMPLE_CONFIG` file can be used to test/play with Pyfig.** First, you need to import Pyfig into your program: import pyfig Next, you need to create the Pyfig object. In order to do this, you need to pass the filepath of the config file you want to parse. I'll use the EXAMPLE_CONFIG included. config = pyfig.Pyfig("EXAMPLE_CONFIG") That's it. You've just created a Pyfig instance which contains all the data in EXAMPLE_CONFIG. Pyfig organizes config values by category, so in order to access `player_name` under category `general`, you would do this: print config.general.player_name 'toaster_phun' There is also an `age` value in that category. We can access that like this: print config.general.age 19 You can also iterate through categories! This is useful when you don't know what or how many config values exist in a given category. You iterate like this: for x, y in config.general: print "%s ---> %s" % (x, y) age ---> 19 player_name ---> toaster_phun The entire config object is also an iterable. It will return a tuple for each category; the name of the category and the actual object: for x, y in config: print "%s: %s" % (x, y) bank: <pyfig.ConfigCategory object at 0xb7d8692c> connection: <pyfig.ConfigCategory object at 0xb7d8990c> general: <pyfig.ConfigCategory object at 0xb7d867ec> Pyfig and Type Detection ------------------------ Pyfig does not automatically detect types. If you would like Pyfig to turn a value into something other than a string (the default type for values), it must be specified in the config file. By default, the following value, 19, will be a string: player_age = 19 If you want 19 to be an integer inside your config object, you must add this: player_age/int = 19 Or a float: player_age/float = 19 API Changes since 1.x --------------------- Pyfig has undergone a major API re-design since the 1.x series. To get a full documentation, see the `Example and Usage` section above. Config values are now stored as objects in category objects. You grab a value by: >>> print Pyfig.general.age 16 Config categories are also iterables. You can do this: >>> for x, y in Pyfig.general ... print x, y age 19 player_name toaster_phun Contribute, Contact ------------------- All development happens at http://bitbucket.org/alecwh/pyfig/. Branches and patches are always appreciated. To contact me, email me (alecwh{at}gmail[dot]com) or see my http://alecwh.com/

About

Pyfig has been around since August 2008, when I released the first version. Pyfig was initially a module I created myself for fun. I thought others might find it useful, so I've released it with periodic feature updates.

Copyright (C) 2007-08 Alec Henriksen

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Please see the GPL at gnu.org for a complete understanding of what this license means and how to abide by it.

Download

Download version 2.0 (latest) - 09/27/2009

Major API redesign, category support added, categories are now iterable, config object is now iterable, type customization added, docs update

Older versions:

I use Bitbucket for developing Pyfig. Find our code here and please improve it! We gladly merge community contributions!

Branches of Pyfig

Here is where I'll list some cool branches of Pyfig that people have sent me. I cannot guarantee these will work well, or even work! Here they are:

  1. By Francesco, adds support for comment_characters to be specified at the class initiation level. Also, he added support for writing to the config file from your python code. This is a branch from the 1.x series. Download!

Help/Bugs

If you need any assistance, or you found a bug, shoot me an email at alecwh{at}gmail[dot]com.

I would also love to hear about your app using Pyfig. Let me know!

Authors

Just me, Alec Henriksen (website)