将文本文件内容解析为Python对象,并将对象写回可解析的文本文件中

时间:2013-03-27 14:12:11

标签: python file parsing text configuration

我需要解析配置文件文件,以便能够添加/删除/修改软件配置,这是由多个格式的文本块定义的:

portsbuild {
        path = /jails/portsbuild;
        allow.mount;
        mount.devfs;
        host.hostname = portsbuild.home;
        ip4.addr = 192.168.0.200;
        interface = nfe0;
        exec.start = "/bin/sh /etc/rc";
        exec.stop = "/bin/sh /etc/rc.shutdown";
}

这些块是相当重复的,到目前为止,只有变量的值才会发生变化。

我尝试过使用re模块,但我最终得到的代码太过臃肿和复杂。然后我尝试了iscpy模块,代码非常简单(一行将整个文件转换成一个方便的字典),但解析后的数据并不完全是应该的:

>>> conf = iscpy.ParseISCString(open('/etc/jail.conf', 'r').read())
>>> conf
{'portsbuild': {'allow.mount': True, 'interface = nfe0': True, 'exec.start = "/bin/sh /etc/rc"': True, 'ip4.addr': '= 192.168.0.200', 'exec.stop': '= "/bin/sh /etc/rc.shutdown"', 'exec.stop = "/bin/sh /etc/rc.shutdown"': True, 'ip4.addr = 192.168.0.200': True, 'path': '= /jails/portsbuild', 'interface': '= nfe0', 'path = /jails/portsbuild': True, 'mount.devfs': True, 'host.hostname': '= portsbuild.home', 'host.hostname = portsbuild.home': True, 'exec.start': '= "/bin/sh /etc/rc"'}}

我也试过pyparsing,但它似乎只适用于一种方式。所以,我想知道是否有一些其他模块或方法来解析该文件使用干净,易于理解的代码片段,可以在两种方式下使用,用于在修改python对象后进行读取和写入?

1 个答案:

答案 0 :(得分:2)

欧芹救援!它似乎是最容易编写自定义解析器的模块,并且结果使用python对象,这些对象很容易转换回相同,精确格式的配置文件。样品溶液下方稍大一点:

from parsley import makeGrammar, unwrapGrammar
from collections import OrderedDict

configFileGrammer = r"""

file = block+:bs end -> OrderedDict(bs)

block = ws name:b ws '{' ws members:m ws '}' ws -> (b, OrderedDict(m))

members = (setting:first (ws setting)*:rest ws -> [first] + rest) | -> []

setting = pair | flag

pair = ws name:k ws '=' ws name:v ws ';' ws -> (k, v)

flag = ws name:k ws ';' ws -> (k, True)

name = <(~('='|';'|'{') anything)+>:n -> n.strip()

"""


testSource = r"""
    portsbuild {
            path = /jails/portsbuild;
            allow.mount;
            mount.devfs;
            host.hostname = portsbuild.home;
            ip4.addr = 192.168.0.200;
            interface = nfe0;
            exec.start = "/bin/sh /etc/rc";
            exec.stop = "/bin/sh /etc/rc.shutdown";
    }

    web01 {
            path = /jails/web01;
            allow.mount;
            mount.devfs;
            host.hostname = web02.site.com;
            ip4.addr = 10.0.0.1;
            interface = eth0;
            exec.start = "/bin/sh /etc/rc";
    }

    db01 {
            path = /jails/db01;
            mount.devfs;
            host.hostname = db01.home;
            ip4.addr = 192.168.6.66;
            interface = if0;
            exec.start = "/bin/mysql";
    }


"""
ConfigFile = makeGrammar(configFileGrammer, globals(), name="ConfigFile")

config = unwrapGrammar(ConfigFile)(testSource).apply('file')[0]
for block in config:
    print "%s {" % block
    for key in config[block]:
        if config[block][key] == True:
            print "\t%s;" % key
        else:
            print "\t%s = %s;" % (key, config[block][key])
    print "}"