2017-11-08 6 views
0

で「拡張」テンプレートベースのINIファイルの解析は、私がパース(およびデータベースへの供給)この1のような設定ファイルをする必要があります(実際にはAsteriskさんsip.confです):は、Python

[client-template](!,natted-template) 
foo=foovalue 
moo=moovalue 

[client](client-template) 
bar=barvalue 

この構文手段そのclient-templatenatted-template(別の場所で定義されている)に基づくテンプレート自体です(括弧内に!があるため)。 clientは、client-templateに基づくオブジェクト定義です。

私はConfigParserを使うことができましたが、もっと強力なものやカスタマイズされたものが必要なようです。

答えて

0

さて、今私はpyparsingを試してみた:

import pyparsing as pp 

filename = 'client.conf' 

nametag = pp.Word(pp.alphanums + "-_") 

variable = pp.Word(pp.alphanums) 
value = pp.Word(pp.alphanums + "=") 

vardef = pp.Group(variable('variable') + pp.Literal("=").suppress() + value('value')) 
vardefs = pp.Group(pp.ZeroOrMore(vardef))('vardefs') 

section = pp.Group(pp.Literal("[").suppress() \ 
     + nametag('objectname') \ 
     + pp.Literal("]").suppress() \ 
     + pp.Optional(
       pp.Literal("(").suppress() \ 
       + pp.Optional("!")('istemplate') 
       + pp.ZeroOrMore(pp.Optional(",").suppress() + nametag)('parenttemplates') \ 
       + pp.Literal(")").suppress() 
      ) \ 
     + vardefs) 

section = section + pp.Optional(pp.SkipTo(section)).suppress() 

section_group = pp.Group(section + pp.ZeroOrMore(section))('sections') 

config = (pp.SkipTo(section_group).suppress() \ 
     + section_group) 


# res = config.parseString(open(filename).read()) 

これは、ソリューションの一部である(これはこれまでのところコメントは認識していない)が、私はそれを進めることができます。

より洗練された解決策がある場合は、教えてください。