使用python检查yaml配置文件是否符合要求
我就废话不多说了,大家还是直接看代码吧!
#coding=utf-8 importlogging importyaml importos importsys reload(sys) sys.setdefaultencoding("utf-8") #获取当前目录的路径 cur_dir=os.path.abspath('.') defcheck_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 ifdts_method=='pacsscp': ifdts_dup_check==Trueanddts_interval==3: returnTrue else: returnFalse else: #打印error级别的错误 print"\033[31mError:methodnotispacsscp!!!\033[0m" returnFalse if__name__=="__main__": #加载yaml配置 config_yaml=os.path.join(cur_dir,'config.yaml') withopen(config_yaml,'rt')asf: config=yaml.safe_load(f.read()) #dt相关路径和配置 dt_path=config['path']['docking-toolbox'] dt_config_path=os.path.join(dt_path,'config.yaml') withopen(dt_config_path,'rt')asf: dt_config=yaml.safe_load(f.read()) ifcheck_dt_pacsscp(dt_config): print(u"\033[32m校验通过~\033[0m") else: print(u'\033[32m校验未通过,请检查配置!\033[0m')
以下是用正则获取不是yaml配置文件的
#coding=utf-8 """ 作业要求,完善check_txpacs_version函数 """ importlogging importtraceback importyaml importos importsys importre reload(sys) sys.setdefaultencoding("utf-8") #获取当前目录的路径 cur_dir=os.path.abspath('.') defcheck_txpacs_version(config,constant): """ 高难度 校验txpacs版本,若版本<1.4,则回调中不能出现received_start_timestamp和received_end_timestamp这两个参数, 若不合法打印error级别的提示; 校验txpacs的自动清理功能是否合法:若dt版本<1.3.8,则不做要求, 若dt版本>=1.3.8,则要求txpacs版本必须为1.5.1及以上版本,且txpacs配置的clean_date需配置为正整数 (docking-toolbox的版本号可以从文件'docking-toolbox/toolbox/utils/constant.py'中读取), 若不合法打印error级别的提示. 根据以上结果,返回返回值. :paramconfig:txpacs的配置 :paramconstant: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的配置文件 withopen(txpacs_config_path,'rt')asf: txpacs_file=f.read() #判断txpacs的版本号是否小于1.4 iftxpacs_version<'1.4': #判断received_start_timestamp"and"received_end_timestamp"两个参数是否出现在txpacs配置文件中 if"received_start_timestamp"and"received_end_timestamp"notintxpacs_file: returnTrue else: print"\033[31mError:当前的txpacs版本号是txpacs-%s,不应该存在received_start_timestamp和received_end_timestamp\033[0m"%txpacs_version returnFalse #如果txpacs版本号大于等于1.4 else: #判断DT版本是否大于1.3.8 ifdt_version>='1.3.8': iftxpacs_version>='1.5.1': ifconfig['store']['clean_date']>0: returnTrue else: print"\033[31mError:txpacs配置文件中clean_date应为正整数\033[0m" returnFalse else: print"\033[31mError:当前DT版本大于1.3.8,txpacs版本必须大于等于1.5.1\033[0m" returnFalse else: returnTrue if__name__=="__main__": #加载yaml配置 config_yaml=os.path.join(cur_dir,'config.yaml') withopen(config_yaml,'rt')asf: config=yaml.safe_load(f.read()) #txpacs相关路径和配置 txpacs_path=config['path']['txpacs'] txpacs_config_path=os.path.join(txpacs_path,'conf.yml') withopen(txpacs_config_path,'rt')asf: 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') withopen(dt_constant_path,'rt')asf: dt_constant=f.read() ifcheck_txpacs_version(txpacs_config,dt_constant): print(u"\033[32m校验通过~\033[0m") else: print(u'\033[32m校验未通过,请检查配置!\033[0m')
以上这篇使用python检查yaml配置文件是否符合要求就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。