使用 pyparsing 将 lvm.conf 转换为 python dict

Convert lvm.conf to python dict using pyparsing

我正在尝试将 lvm.conf 转换为 python(类似于 JSON)对象。 LVM(逻辑卷管理)配置文件如下所示:

# Configuration section config.
# How LVM configuration settings are handled.
config {

    # Configuration option config/checks.
    # If enabled, any LVM configuration mismatch is reported.
    # This implies checking that the configuration key is understood by
    # LVM and that the value of the key is the proper type. If disabled,
    # any configuration mismatch is ignored and the default value is used
    # without any warning (a message about the configuration key not being
    # found is issued in verbose mode only).
    checks = 1

    # Configuration option config/abort_on_errors.
    # Abort the LVM process if a configuration mismatch is found.
    abort_on_errors = 0

    # Configuration option config/profile_dir.
    # Directory where LVM looks for configuration profiles.
    profile_dir = "/etc/lvm/profile"
}


local {
}
log {
    verbose=0
    silent=0
    syslog=1
    overwrite=0
    level=0
    indent=1
    command_names=0
    prefix="  "
    activation=0
    debug_classes=["memory","devices","activation","allocation","lvmetad","metadata","cache","locking","lvmpolld","dbus"]
}

我想要 Python 字典,像这样:

{ "section_name"": 
{"value1" : 1,
 "value2" : "some_string",
 "value3" : [list, of, strings]}... and so on.}

解析器函数:

def parseLvmConfig2(path="/etc/lvm/lvm.conf"):
    try:
        EQ, LBRACE, RBRACE, LQ, RQ = map(pp.Suppress, "={}[]")
        comment = pp.Suppress("#") + pp.Suppress(pp.restOfLine)
        configSection = pp.Word(pp.alphas + "_") + LBRACE
        sectionKey = pp.Word(pp.alphas + "_")
        sectionValue = pp.Forward()
        entry = pp.Group(sectionKey + EQ + sectionValue)
        real = pp.Regex(r"[+-]?\d+\.\d*").setParseAction(lambda x: float(x[0]))
        integer = pp.Regex(r"[+-]?\d+").setParseAction(lambda x: int(x[0]))
        listval = pp.Regex(r'(?:\[)(.*)?(?:\])').setParseAction(lambda x: eval(x[0]))

        pp.dblQuotedString.setParseAction(pp.removeQuotes)

        struct = pp.Group(pp.ZeroOrMore(entry) + RBRACE)
        sectionValue << (pp.dblQuotedString | real | integer | listval)
        parser = pp.ZeroOrMore(configSection + pp.Dict(struct))
        res = parser.parseFile(path)
        print(res)
    except (pp.ParseBaseException, ) as e:
        print("lvm.conf bad format {0}".format(e))

结果很乱,问题是,如何在没有额外逻辑的情况下让 pyparsing 完成这项工作?

更新(已解决):

对于任何想更好地理解 pyparsing 的人,请查看下面的@PaulMcG 解释。 (感谢 pyparsing,Paul!)

import pyparsing as pp
def parseLvmConf(conf="/etc/lvm/lvm.conf", res_type="dict"):
    EQ, LBRACE, RBRACE, LQ, RQ = map(pp.Suppress, "={}[]")
    comment = "#" + pp.restOfLine
    integer = pp.nums
    real = pp.Word(pp.nums + "." + pp.nums)
    pp.dblQuotedString.setParseAction(pp.removeQuotes)
    scalar_value = real | integer | pp.dblQuotedString
    list_value = pp.Group(LQ + pp.delimitedList(scalar_value) + RQ)
    key = pp.Word(pp.alphas + "_", pp.alphanums + '_')
    key_value = pp.Group(key + EQ + (scalar_value | list_value))
    struct = pp.Forward()
    entry = key_value | pp.Group(key + struct)
    struct <<= pp.Dict(LBRACE + pp.ZeroOrMore(entry) + RBRACE)
    parser = pp.Dict(pp.ZeroOrMore(entry))
    parser.ignore(comment)
    try:
        #return lvm.conf as dict
        if res_type == "dict":
            return parser.parseFile(conf).asDict()
        # return lvm.conf as list
        elif res_type == "list":
            return parser.parseFile(conf).asList()
        else:
            #return lvm.conf as ParseResults
            return parser.parseFile(conf)
    except (pp.ParseBaseException,) as e:
        print("lvm.conf bad format {0}".format(e))

第 1 步应该始终至少为您要解析的格式粗略制定一个 BNF。这确实有助于组织您的想法,并让您在开始编写实际代码之前考虑要解析的结构和数据。

这是我为这个配置想出的一个 BNF(它看起来像一个 Python 字符串,因为它可以很容易地粘贴到您的代码中以供将来参考 - 但 pyparsing 不能使用或要求这样的字符串,它们纯粹是一种设计工具):

BNF = '''
    key_struct ::= key struct
    struct ::= '{' (key_value | key_struct)... '}'
    key_value ::= key '=' (scalar_value | list_value)
    key ::= word composed of alphas and '_'
    list_value ::= '[' scalar_value [',' scalar_value]... ']'
    scalar_value ::= real | integer | double-quoted-string
    comment ::= '#' rest-of-line
'''

请注意,开始和结束 {} 和 [] 处于同一级别,而不是在一个表达式中有开场白而在另一个表达式中有闭幕词。

此 BNF 还将允许结构嵌套在结构中,这在您发布的示例文本中并不是严格要求的,但由于您的代码看起来支持它,所以我将其包括在内。

从这里转换为 pyparsing 非常简单,通过 BNF 自下而上地工作:

EQ, LBRACE, RBRACE, LQ, RQ = map(pp.Suppress, "={}[]")
comment = "#" + pp.restOfLine

integer = ppc.integer  #pp.Regex(r"[+-]?\d+").setParseAction(lambda x: int(x[0]))
real = ppc.real  #pp.Regex(r"[+-]?\d+\.\d*").setParseAction(lambda x: float(x[0]))
pp.dblQuotedString.setParseAction(pp.removeQuotes)
scalar_value = real | integer | pp.dblQuotedString

# `delimitedList(expr)` is a shortcut for `expr + ZeroOrMore(',' + expr)`
list_value = pp.Group(LQ + pp.delimitedList(scalar_value) + RQ)

key = pp.Word(pp.alphas + "_", pp.alphanums + '_')
key_value = pp.Group(key + EQ + (scalar_value | list_value))

struct = pp.Forward()
entry = key_value | pp.Group(key + struct)
struct <<= (LBRACE + pp.ZeroOrMore(entry) + RBRACE)
parser = pp.ZeroOrMore(entry)
parser.ignore(comment)

运行 这段代码:

try:
    res = parser.parseString(lvm_source)
    # print(res.dump())
    res.pprint()
    return res
except (pp.ParseBaseException, ) as e:
    print("lvm.conf bad format {0}".format(e))

给出这个嵌套列表:

[['config',
  ['checks', 1],
  ['abort_on_errors', 0],
  ['profile_dir', '/etc/lvm/profile']],
 ['local'],
 ['log',
  ['verbose', 0],
  ['silent', 0],
  ['syslog', 1],
  ['overwrite', 0],
  ['level', 0],
  ['indent', 1],
  ['command_names', 0],
  ['prefix', '  '],
  ['activation', 0],
  ['debug_classes',
   ['memory',
    'devices',
    'activation',
    'allocation',
    'lvmetad',
    'metadata',
    'cache',
    'locking',
    'lvmpolld',
    'dbus']]]]

我认为您更喜欢的格式是您可以在嵌套字典或分层对象中将值作为键访问的格式。 Pyparsing 有一个名为 Dict 的 class 将在解析时执行此操作,以便为嵌套子组自动分配结果名称。更改这两行以使其子条目自动字典化:

struct <<= pp.Dict(LBRACE + pp.ZeroOrMore(entry) + RBRACE)
parser = pp.Dict(pp.ZeroOrMore(entry))

现在如果我们调用 dump() 而不是 pprint(),我们将看到分层命名:

[['config', ['checks', 1], ['abort_on_errors', 0], ['profile_dir', '/etc/lvm/profile']], ['local'], ['log', ['verbose', 0], ['silent', 0], ['syslog', 1], ['overwrite', 0], ['level', 0], ['indent', 1], ['command_names', 0], ['prefix', '  '], ['activation', 0], ['debug_classes', ['memory', 'devices', 'activation', 'allocation', 'lvmetad', 'metadata', 'cache', 'locking', 'lvmpolld', 'dbus']]]]
- config: [['checks', 1], ['abort_on_errors', 0], ['profile_dir', '/etc/lvm/profile']]
  - abort_on_errors: 0
  - checks: 1
  - profile_dir: '/etc/lvm/profile'
- local: ''
- log: [['verbose', 0], ['silent', 0], ['syslog', 1], ['overwrite', 0], ['level', 0], ['indent', 1], ['command_names', 0], ['prefix', '  '], ['activation', 0], ['debug_classes', ['memory', 'devices', 'activation', 'allocation', 'lvmetad', 'metadata', 'cache', 'locking', 'lvmpolld', 'dbus']]]
  - activation: 0
  - command_names: 0
  - debug_classes: ['memory', 'devices', 'activation', 'allocation', 'lvmetad', 'metadata', 'cache', 'locking', 'lvmpolld', 'dbus']
  - indent: 1
  - level: 0
  - overwrite: 0
  - prefix: '  '
  - silent: 0
  - syslog: 1
  - verbose: 0

然后您可以以 res['config']['checks']res.log.indent 的形式访问字段。