微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用python检查yaml配置文件是否符合要求

这篇文章主要介绍了使用python检查yaml配置文件是否符合要求,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

我就废话不多说了,大家还是直接看代码吧!

# coding=utf-8 import logging import yaml import os import sys reload(sys) sys.setdefaultencoding("utf-8") # 获取当前目录的路径 cur_dir = os.path.abspath('.') def check_dt_pacsscp(config): """ 用来检查文件配置是否正确 """ #将配置config.yaml配置文件以字典方式读取 dts_method = config['service']['method'] dts_dup_check = config['service']['pacsscp_dup_check_off'] dts_interval = config['scheduler']['interval'] #判断对接方式是否是pacsscp if dts_method == 'pacsscp': if dts_dup_check == True and dts_interval == 3: return True else: return False else: #打印error级别的错误 print "33[31m Error:method not is pacsscp!!!33[0m" return False if __name__ == "__main__": # 加载yaml配置 config_yaml = os.path.join(cur_dir, 'config.yaml') with open(config_yaml, 'rt') as f: config = yaml.safe_load(f.read()) #dt相关路径和配置 dt_path = config['path']['docking-toolBox'] dt_config_path = os.path.join(dt_path, 'config.yaml') with open(dt_config_path, 'rt') as f: dt_config = yaml.safe_load(f.read()) if check_dt_pacsscp(dt_config): print (u"33[32m 校验通过~33[0m") else: print (u'33[32m 校验未通过, 请检查配置!33[0m ')

以下是用正则获取不是yaml配置文件

# coding=utf-8 """ 作业要求, 完善check_txpacs_version函数 """ import logging import traceback import yaml import os import sys import re reload(sys) sys.setdefaultencoding("utf-8") # 获取当前目录的路径 cur_dir = os.path.abspath('.') def check_txpacs_version(config, constant): """ 高难度 校验txpacs版本, 若版本=1.3.8, 则要求txpacs版本必须为1.5.1及以上版本, 且txpacs配置的clean_date需配置为正整数 (docking-toolBox的版本号可以从文件'docking-toolBox/toolBox/utils/constant.py'中读取), 若不合法打印error级别的提示. 根据以上结果, 返回返回值. :param config: txpacs的配置 :param constant: docking-toolBox/toolBox/utils/constant.py文件内容 :return: True: 通过 False: 不通过 """ # 获取txpacs的版本号 jar_file = os.listdir(txpacs_path) jar_file.sort(reverse=True) jar_ver = jar_file[0] jar_version = re.search('txpacs-(.*?).jar', jar_ver) #txpacs的版本号 txpacs_version = jar_version.group(1) #docking-tools的版本号 dt_version = re.search('DT_VERSIONS.*?"(.*?)"',constant).group(1) #打开txpacs的配置文件 with open(txpacs_config_path, 'rt') as f: txpacs_file = f.read() #判断txpacs的版本号是否小于1.4 if txpacs_version = '1.3.8': if txpacs_version >= '1.5.1': if config['store']['clean_date'] > 0: return True else: print "33[31m Error:txpacs配置文件中clean_date应为正整数 33[0m" return False else: print "33[31m Error:当前DT版本大于1.3.8,txpacs版本必须大于等于1.5.133[0m" return False else: return True if __name__ == "__main__": # 加载yaml配置 config_yaml = os.path.join(cur_dir, 'config.yaml') with open(config_yaml, 'rt') as f: config = yaml.safe_load(f.read()) # txpacs相关路径和配置 txpacs_path = config['path']['txpacs'] txpacs_config_path = os.path.join(txpacs_path, 'conf.yml') with open(txpacs_config_path, 'rt') as f: txpacs_config = yaml.safe_load(f.read()) # dt相关路径和配置 dt_path = config['path']['docking-toolBox'] dt_constant_path = os.path.join(dt_path, 'toolBox', 'utils', 'constant.py') with open(dt_constant_path, 'rt') as f: dt_constant = f.read() if check_txpacs_version(txpacs_config, dt_constant): print (u"33[32m 校验通过~33[0m") else: print (u'33[32m 校验未通过, 请检查配置! 33[0m')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐