本篇內(nèi)容介紹了“python能不能檢查yaml配置文件是否符合要求”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為松北企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、做網(wǎng)站,松北網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。# 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): """ 用來檢查文件配置是否正確 """ #將配置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相關(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 校驗通過~\033[0m") else: print (u'\033[32m 校驗未通過, 請檢查配置!\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): """ 高難度 校驗txpacs版本, 若版本<1.4, 則回調(diào)中不能出現(xiàn)received_start_timestamp和received_end_timestamp這兩個參數(shù), 若不合法打印error級別的提示; 校驗txpacs的自動清理功能是否合法: 若dt版本<1.3.8, 則不做要求, 若dt版本>=1.3.8, 則要求txpacs版本必須為1.5.1及以上版本, 且txpacs配置的clean_date需配置為正整數(shù) (docking-toolbox的版本號可以從文件'docking-toolbox/toolbox/utils/constant.py'中讀取), 若不合法打印error級別的提示. 根據(jù)以上結(jié)果, 返回返回值. :param config: txpacs的配置 :param constant: docking-toolbox/toolbox/utils/constant.py文件內(nèi)容 :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"兩個參數(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版本號是txpacs-%s ,不應(yīng)該存在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應(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 校驗通過~\033[0m") else: print (u'\033[32m 校驗未通過, 請檢查配置! \033[0m')
“python能不能檢查yaml配置文件是否符合要求”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
文章標(biāo)題:python能不能檢查yaml配置文件是否符合要求-創(chuàng)新互聯(lián)
文章起源:http://m.rwnh.cn/article48/cedhhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、云服務(wù)器、靜態(tài)網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容