使用python检查yaml配置文件是否符合要求
作者:wangjiadongge 时间:2021-06-23 05:27:53
我就废话不多说了,大家还是直接看代码吧!
# 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 "\033[31m Error:method not is pacsscp!!!\033[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"\033[32m 校验通过~\033[0m")
else:
print (u'\033[32m 校验未通过, 请检查配置!\033[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.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级别的提示.
根据以上结果, 返回返回值.
: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.4':
#判断received_start_timestamp" and "received_end_timestamp"两个参数是否出现在txpacs配置文件中
if "received_start_timestamp" and "received_end_timestamp" not in txpacs_file:
return True
else:
print "\033[31m Error:当前的txpacs版本号是txpacs-%s ,不应该存在received_start_timestamp和received_end_timestamp\033[0m"%txpacs_version
return False
#如果txpacs版本号大于等于1.4
else:
#判断DT版本是否大于1.3.8
if dt_version >= '1.3.8':
if txpacs_version >= '1.5.1':
if config['store']['clean_date'] > 0:
return True
else:
print "\033[31m Error:txpacs配置文件中clean_date应为正整数 \033[0m"
return False
else:
print "\033[31m Error:当前DT版本大于1.3.8,txpacs版本必须大于等于1.5.1\033[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"\033[32m 校验通过~\033[0m")
else:
print (u'\033[32m 校验未通过, 请检查配置! \033[0m')
来源:https://blog.csdn.net/wangjiadongge/article/details/100632770
标签:python,yaml,配置,文件
0
投稿
猜你喜欢
PDO::quote讲解
2023-06-03 21:14:37
10个杀手级应用的Python自动化脚本
2023-10-06 14:01:53
python分析作业提交情况
2023-07-29 20:59:31
SQL Server实现分布式数据库系统的终极目标
2010-08-05 14:57:00
手把手教你Python抓取数据并可视化
2022-01-08 02:11:55
Python递归函数实例讲解
2022-12-01 14:54:56
python网络编程之TCP通信实例和socketserver框架使用例子
2023-02-03 05:53:09
vue中el-table合并列的具体实现
2024-05-02 16:35:03
javascript阻止事件冒泡和浏览器的默认行为
2007-12-28 13:13:00
简单介绍Python中的decode()方法的使用
2021-01-05 23:47:31
MySQL数据库表空间回收的解决
2024-01-26 00:00:41
python可变对象,不可变对象详解
2023-10-18 05:14:25
Python将脚本程序转变为可执行程序的实现
2022-03-02 16:30:49
OpenCV-Python实现图像梯度与Sobel滤波器
2021-04-07 05:23:20
JavaScript数组方法-系统性总结详解
2024-04-28 09:38:49
python实现图片彩色转化为素描
2021-12-02 03:57:36
MySQL 与 Elasticsearch 数据不对称问题解决办法
2024-01-23 13:43:57
关于ORACLE通过file_id与block_id定位数据库对象遇到的问题引发的思考
2024-01-18 11:01:08
Python实现双向链表
2022-06-12 17:41:34
Vue系列之Element UI表单自定义校验规则
2024-04-30 08:41:17