Python block parsing utility?

I have a file that runs something like:

databaseCons = {
    main = {
        database = "readable_name",
        hostname = "hostdb1.serv.com",
        instances= {
            slaves = {
                conns = "8"
            }
        }
        maxconns = "5",
        user = "user",
        pass = "pass"
    }
}

      

So what I would like to do is parse this into dict

sub < dict

s, something like:

{'main': {'database': 'readable_name', 'hostname': 'hostdb1.serv.com', 'maxconns': '5', 'instances': {'slave': {'maxCount': '8'}}, 'user': 'user', 'pass': 'pass'}}

I think it makes sense ... but please feel free to edit this if it doesn't. Basically, I want the equivalent:

conns = '8'

slave = dict()
slave['maxCount'] = conns

instances = dict()
instances['slave'] = slave

database = 'readable_name'
hostname = 'hostdb1.serv.com'
maxconns = '5'
user = 'user'
pas = 'pass'
main = dict()
main['database'] = database
main['hostname'] = hostname
main['instances'] = instances
main['maxconns'] = maxconns
main['user'] = user
main['pass'] = pas

databaseCons = dict()
databaseCons['main'] = main

      

Are there any modules that can handle this kind of parsing? Even what I suggested above looks messy .. there must be a better way I imagine.

+3


source to share


1 answer


Below is a pyparsing parser for your config file:



from pyparsing import *

def to_dict(t):
    return {k:v for k,v in t}

series = Forward()
struct = Suppress('{') + series + Suppress('}')
value = quotedString.setParseAction(removeQuotes) | struct
token = Word(alphanums)
assignment = Group(token + Suppress('=') + value + Suppress(Optional(",")))
series << ZeroOrMore(assignment).setParseAction(to_dict)
language = series + stringEnd

def config_file_to_dict(filename):
    return language.parseFile(filename)[0]

if __name__=="__main__":
    from pprint import pprint
    pprint(config_file_to_dict('config.txt'))

      

+2


source







All Articles