PyYaml解析Yaml配置文件中的环境变量

7
我需要读取以下 yaml 格式的配置文件:
version: 1
disable_existing_loggers: False
formatters:
  precise:
    format: "%(name)-15s # %(levelname)-8s # %(asctime)s # [Line: %(lineno)-3d]: %(message)s"
    datefmt: "%Y-%m-%d %H:%M:%S"
handlers:
  file:
    class:        logging.handlers.RotatingFileHandler
    filename:     <%= ENV['ENV_PROJECT'] %>/target/tracing.log
    encoding:     utf-8
    maxBytes :    10737418244
    backupCount:  7
    formatter:    precise
loggers:
  utility:
    handlers:     [file]
    level:        INFO
    propagate:    True
root:
  handlers:       [file]
  level:          INFO

然而,在结果字符串中,我需要得到相关路径,而不是<%= ENV['ENV_PROJECT'] %>。例如:

为了加载这个文件,我使用以下代码:

from yaml import load
with open('test.yml', 'rt') as stream:
    configuration = load(stream)

我该如何获得所需的结果? 谢谢。
1个回答

14

你需要使用“resolver”和“constructor”来实现这一点。以下是一个实现的方法。

import yaml, os, re

#define the regex pattern that the parser will use to 'implicitely' tag your node
pattern = re.compile( r'^\<%= ENV\[\'(.*)\'\] %\>(.*)$' )

#now define a custom tag ( say pathex ) and associate the regex pattern we defined
yaml.add_implicit_resolver ( "!pathex", pattern )

#at this point the parser will associate '!pathex' tag whenever the node matches the pattern

#you need to now define a constructor that the parser will invoke
#you can do whatever you want with the node value
def pathex_constructor(loader,node):
  value = loader.construct_scalar(node)
  envVar, remainingPath = pattern.match(value).groups()
  return os.environ[envVar] + remainingPath

#'register' the constructor so that the parser will invoke 'pathex_constructor' for each node '!pathex'
yaml.add_constructor('!pathex', pathex_constructor)

#that is it

data = """
version: 1
disable_existing_loggers: False
formatters:
  precise:
    format: "%(name)-15s # %(levelname)-8s # %(asctime)s # [Line: %(lineno)-3d]: %(message)s"
    datefmt: "%Y-%m-%d %H:%M:%S"
handlers:
  file:
    class:        logging.handlers.RotatingFileHandler
    filename:     <%= ENV['ENV_PROJECT'] %>/target/tracing.log 
    encoding:     utf-8
    maxBytes :    10737418244
    backupCount:  7
    formatter:    precise
loggers:
  utility:
    handlers:     [file]
    level:        INFO
    propagate:    True
root:
  handlers:       [file]
  level:          INFO
"""

deSerializedData = yaml.load(data)

print(deSerializedData [ 'handlers'] [ 'file' ] ['filename'] )

谢谢您的回答,尽管我在int方面遇到了问题,在上面的例子中,例如备份计数,它会显示为“7”,而不是7。您是如何解决这个问题的? @ganeshsamant - Itachi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接