HerbSpiral
HerbSpiral

Reputation: 193

How to write a parser for a configuration file in python

I have a non-standard configuration file, for which I want to write a python parser.

What is the best approach to writing a parser from scratch?

Example of a configuration file:

// Comment 
conf OPTION_NAME {
     (
       ( option1:"string"
         option2:"14"
       )
     )
}


// Comment2 
conf OPTION_NAME2 {
     (
       ( option1:"string2"
         option2:"15"
       )
     )
}

Upvotes: 1

Views: 2161

Answers (1)

FogleBird
FogleBird

Reputation: 76762

I would personally use PLY: http://www.dabeaz.com/ply/

Here's a simple example:

http://www.dabeaz.com/ply/example.html

Here's an example from one of my own projects:

https://github.com/fogleman/FeedNotifier/blob/master/filters.py

Alternatively, since the files look very simple, I might just use a handmade Finite State Machine to do the parsing.

Upvotes: 2

Related Questions