# -*- coding:utf-8 -*- 
"""
describe：读取测试数据yaml文件
Author：wangchao
Time: 2023/4/17
"""

import yaml, os
from Config import Config
from BuildInLibrary.BuildInLibrary import BuildInLibrary
from Utils.GetObjectPath import get_object_path


class ReadYaml(object):

	def __init__(self, filename):
		self.filename = filename

	# 读取TestDatas目录下的yaml文件
	def read(self):
		with open(file=self.filename, mode="r", encoding='utf8', ) as f:
			bilibrary = BuildInLibrary()
			data = f.read()
			# 调用内置库的方法替换参数,可以替换多个,满足{{$参数}}规则的参数会被替换
			data1 = bilibrary.repalce_parameter(data)
		data_yaml = yaml.load(data1, Loader=yaml.FullLoader)
		for value in data_yaml:
			# 拼接URL地址
			if value.get("url地址") is not None:
				value["url地址"] = Config.url + value["url地址"]
			# 拼接上传文件的地址
			if value.get("files") is not None:
				value["files"] = Config.test_files_dir + os.path.sep + value["files"]

		return data_yaml

	# 读取log_config.yml文件
	def read_config_file(self, one_node, two_node):
		with open(Config.root_dir + os.path.sep + self.filename, encoding='utf-8') as f:
			value = yaml.load(stream=f, Loader=yaml.FullLoader)
			return value[one_node][two_node]


if __name__ == '__main__':
	ry = ReadYaml("log_config.yml")
	read = ry.read_config_file('log', 'log_level')
	print(read)
