如何使用pyparsing解析F5 bigip.conf

时间:2013-04-08 21:17:55

标签: python pyparsing f5

我想弄清楚如何使用这个漂亮的lib来解析BigIP配置文件... 语法应该是这样的:

stanza :: name { content }
name   :: several words, might contain alphas nums dot dash underscore or slash
content:: stanza OR ZeroOrMore(printable characters)

为了使事情稍微复杂一点,例外:

如果名称以“rule”开头,则内容不能是“stanza”

我从这开始:

from pyparsing import *
def parse(config):
    def BNF():
        """
        Example:
        ...
        ltm virtual /Common/vdi.uis.test.com_80_vs {
            destination /Common/1.2.3.4:80
            http-class {
                /Common/http2https
            }
            ip-protocol tcp
            mask 255.255.255.255
            profiles {
                /Common/http { }
                /Common/tcp { }
            }
            vlans-disabled
        }
        ...
        """        
        lcb, rcb, slash, dot, underscore, dash = [c for c in '{}/._-']
        name_word = Word(alphas + nums + dot + underscore + slash + dash)
        name = OneOrMore(name_word).setResultsName("name")
        stanza = Forward()
        content = OneOrMore(stanza | ZeroOrMore(OneOrMore(Word(printables)))).setResultsName("content")
        stanza << Group(name + lcb + content + rcb).setResultsName("stanza")
        return stanza


    return [x for x in BNF().scanString(config)]

上面的代码似乎锁定在一些无限循环中。如果“name”以“rule”开头,它也缺少我要求排除查找“节”的要求。

1 个答案:

答案 0 :(得分:3)

OneOrMore(ZeroOrMore(OneOrMore(Word(printables)))将始终匹配,从而导致无限循环。

此外,printables包含一个结束花括号,它被内容术语占用,并且不再适用于该节。 (如果您的内容可以包含结束括号,则需要定义要转义它的内容,以区分内容括号和节括号。)

要解决名称规则,您需要另一个内容定义,一个不包含节的内容,以及一个“规则规则”。

def parse(config):
    def BNF():
        lcb, rcb, slash, dot, underscore, dash = [c for c in '{}/._-']
        printables_no_rcb = Word(printables, excludeChars=rcb)
        name_word = Word(alphas + nums + dot + underscore + slash + dash)
        name = OneOrMore(name_word).setResultsName("name")
        rule = Group(Literal('rule') + name).setResultsName("name")
        rule_content = OneOrMore(printables_no_rcb).setResultsName("content")
        stanza = Forward()
        content = OneOrMore(stanza | OneOrMore(printables_no_rcb)).setResultsName("content")
        stanza << Group(rule + lcb + rule_content + rcb | name + lcb + content + rcb).setResultsName("stanza")
        return stanza
    return [x for x in BNF().scanString(config)]