温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python能不能检查yaml配置文件是否符合要求

发布时间:2021-03-10 15:20:25 来源:亿速云 阅读:262 作者:TREX 栏目:开发技术

本篇内容介绍了“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 "\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')

“python能不能检查yaml配置文件是否符合要求”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI