使用python檢查yaml配置文件是否符合要求
我就廢話不多說(shuō)了,大家還是直接看代碼吧!
# coding=utf-8 import logging import yaml import os import sys reload(sys) sys.setdefaultencoding("utf-8") # 獲取當(dāng)前目錄的路徑 cur_dir = os.path.abspath('.') def check_dt_pacsscp(config): """ 用來(lái)檢查文件配置是否正確 """ #將配置config.yaml配置文件以字典方式讀取 dts_method = config['service']['method'] dts_dup_check = config['service']['pacsscp_dup_check_off'] dts_interval = config['scheduler']['interval'] #判斷對(duì)接方式是否是pacsscp if dts_method == 'pacsscp': if dts_dup_check == True and dts_interval == 3: return True else: return False else: #打印error級(jí)別的錯(cuò)誤 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相關(guān)路徑和配置 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 校驗(yàn)通過(guò)~\033[0m") else: print (u'\033[32m 校驗(yàn)未通過(guò), 請(qǐng)檢查配置!\033[0m ')
以下是用正則獲取不是yaml配置文件的
# coding=utf-8 """ 作業(yè)要求, 完善check_txpacs_version函數(shù) """ import logging import traceback import yaml import os import sys import re reload(sys) sys.setdefaultencoding("utf-8") # 獲取當(dāng)前目錄的路徑 cur_dir = os.path.abspath('.') def check_txpacs_version(config, constant): """ 高難度 校驗(yàn)txpacs版本, 若版本<1.4, 則回調(diào)中不能出現(xiàn)received_start_timestamp和received_end_timestamp這兩個(gè)參數(shù), 若不合法打印error級(jí)別的提示; 校驗(yàn)txpacs的自動(dòng)清理功能是否合法: 若dt版本<1.3.8, 則不做要求, 若dt版本>=1.3.8, 則要求txpacs版本必須為1.5.1及以上版本, 且txpacs配置的clean_date需配置為正整數(shù) (docking-toolbox的版本號(hào)可以從文件'docking-toolbox/toolbox/utils/constant.py'中讀取), 若不合法打印error級(jí)別的提示. 根據(jù)以上結(jié)果, 返回返回值. :param config: txpacs的配置 :param constant: docking-toolbox/toolbox/utils/constant.py文件內(nèi)容 :return: True: 通過(guò) False: 不通過(guò) """ # 獲取txpacs的版本號(hào) 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的版本號(hào) txpacs_version = jar_version.group(1) #docking-tools的版本號(hào) dt_version = re.search('DT_VERSIONS.*?"(.*?)"',constant).group(1) #打開(kāi)txpacs的配置文件 with open(txpacs_config_path, 'rt') as f: txpacs_file = f.read() #判斷txpacs的版本號(hào)是否小于1.4 if txpacs_version < '1.4': #判斷received_start_timestamp" and "received_end_timestamp"兩個(gè)參數(shù)是否出現(xiàn)在txpacs配置文件中 if "received_start_timestamp" and "received_end_timestamp" not in txpacs_file: return True else: print "\033[31m Error:當(dāng)前的txpacs版本號(hào)是txpacs-%s ,不應(yīng)該存在received_start_timestamp和received_end_timestamp\033[0m"%txpacs_version return False #如果txpacs版本號(hào)大于等于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應(yīng)為正整數(shù) \033[0m" return False else: print "\033[31m Error:當(dāng)前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相關(guān)路徑和配置 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相關(guān)路徑和配置 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 校驗(yàn)通過(guò)~\033[0m") else: print (u'\033[32m 校驗(yàn)未通過(guò), 請(qǐng)檢查配置! \033[0m')
以上這篇使用python檢查yaml配置文件是否符合要求就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python3制作一個(gè)帶GUI界面的小說(shuō)爬蟲(chóng)工具
這篇文章主要為大家介紹了一個(gè)通過(guò)Python3制作的帶GUI界面的小說(shuō)爬蟲(chóng)工具,用來(lái)從筆趣閣爬取小說(shuō)。感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-02-02python等差數(shù)列求和公式前 100 項(xiàng)的和實(shí)例
今天小編就為大家分享一篇python等差數(shù)列求和公式前 100 項(xiàng)的和實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02基于Python實(shí)現(xiàn)五子棋-(人機(jī)對(duì)戰(zhàn))
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)五子棋游戲(人機(jī)對(duì)戰(zhàn)版),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05Pytest?fixture及conftest相關(guān)詳解
這篇文章主要介紹了Pytest?fixture及conftest相關(guān)詳解,fixture是在測(cè)試函數(shù)運(yùn)行前后,由pytest執(zhí)行的外殼函數(shù),更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-09-09python使用tomorrow實(shí)現(xiàn)多線程的例子
今天小編就為大家分享一篇python使用tomorrow實(shí)現(xiàn)多線程的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07在Django的URLconf中進(jìn)行函數(shù)導(dǎo)入的方法
這篇文章主要介紹了在Django的URLconf中進(jìn)行函數(shù)導(dǎo)入的方法,Django是Python的最為著名的開(kāi)發(fā)框架,需要的朋友可以參考下2015-07-07Python+Django實(shí)現(xiàn)簡(jiǎn)單HelloWord網(wǎng)頁(yè)的示例代碼
本文主要介紹了Python+Django實(shí)現(xiàn)簡(jiǎn)單HelloWord網(wǎng)頁(yè)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04