config.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:OVERWATCH 作者: raymondEhlers 项目源码 文件源码
def readConfig(configType):
    if configType in configurationType:
        # The earliest config files are given the _most_ precedence.
        # ie. A value in the config in the local directory will override the same variable
        #     defined in the config in the package base directory.
        # For more on pkg_resources, see: https://stackoverflow.com/a/5601839
        fileList = [
                    # Config file in the local directory where it is run
                    "config.yaml",
                    # Config in the home directory
                    # Ensures that we have "WebApp" here.
                    os.path.expandvars("~/.overwatch{0}").format(configType.name[0].upper() + configType.name[1:]),
                    # Config type specific directory in the package (ex: "processing")
                    # TODO: There is a problem when loading the shared configuration with the processing configuration
                    #       because the shared configuration can have options which are defined in the web app config
                    #       and therefore undefined when the web app config is not loaded!
                    #       To resolve it temporarily, both configuration files will be included
                    pkg_resources.resource_filename("overwatch.webApp", "config.yaml"),
                    pkg_resources.resource_filename("overwatch.processing", "config.yaml"),
                    pkg_resources.resource_filename("overwatch.receiver", "config.yaml"),
                    pkg_resources.resource_filename("overwatch.api", "config.yaml"),
                    #       Below is the line that should be used when the above issue is resolved
                    #pkg_resources.resource_filename("overwatch.{0}".format(configType.name), "config.yaml"),
                    # Shared config in the package base
                    pkg_resources.resource_filename("overwatch.base", "config.yaml")
                    ]
    else:
        # Cannot just be the logger because the logger many not yet be initialized
        print("CRITICAL: Unrecognized configuration type {0}!".format(configType.name))
        logger.critical("Unrecognized configuration type {0}!".format(configType.name))
        sys.exit(1)

    # Suppressed for cleaning up start up messages
    #logger.debug("Config filenames: {0}".format(fileList))

    (configs, filesRead) = readConfigFiles(fileList)
    # Suppressed for cleaning up start up messages
    #logger.debug("Read config files: {0}".format(filesRead))

    # Merge the configurations together
    # List is reversed so the earlier listed config will always override settings from lower listed files
    configs = "\n".join(reversed(configs))
    #print("configs: {0}".format(configs))
    # Handle warnings
    # See: https://stackoverflow.com/a/40376576
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        globalConfig = yaml.load(configs, Loader=yaml.SafeLoader)

    return (globalConfig, filesRead)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号